---asked already on unix.StackExchange, but shut down (too subjective)---

Rewritten for clarity.

  • I want to write scrips for my own use
  • I want to be able to back them
  • I haven't yet established the backup routine - gonna write a script.
  • it will be my 4th script, and my first serious approach to backing up (so be gentle).

What is the most effective strategy, given that two factors seem to be in tradeoff:

a) among several options, /usr/local/bin is widely advocated as most advantageous location for custom scripts, (but not uniquely; see ideas in 1, 2, 3);

b) /usr/local/bin is full of other, automatically generated files, files (e.g. Dropbox, etc) that I would not want to take into a backup and hence would have to figure out a way to filter (not a trivial task for my skill level, though I'm willing to learn).

Do I attack this from the "directory end" (e.g. 'don't use that directory, use this one!') or from the "backup script end" (e.g. an effective filter I could use with tar or cp or whatever to extract my script files from the crowd in /usr/local/bin).

I can't find anyone talking about both of these things in the one place (but "backup" and "scripts" swamps results with "backup scripts" so maybe I've missed it.)

Cheers - thanks for the constructive answers so far.

PS - see also an earlier version of this question elsewhere for a couple of helpful replies before shutdown.

  • 2
    What's wrong with creating /home/$USER/bin folder as the .profile looks to that folder and add it to the path if it exists? Then all your personal scripts can go in there. That's one way I do it. If you do create it, run . ~/.profile to source the change to the path. – Terrance Jan 3 '18 at 23:37
  • 2
    Possible duplicate of Best directory to place my bash scripts? – WinEunuuchs2Unix Jan 4 '18 at 0:55
  • Are you asking how to back up a script or asking where to store scripts in their own directory? It's not clear to me and you shouldn't ask two questions at once. Besides once you have your scripts in their own directory you simply backup the whole directory. – WinEunuuchs2Unix Jan 4 '18 at 1:15
  • @Terrance - thanks. I basically hadn't heard of .profile, (yup, I'm that green), so I'll do some reading. – j doe will do just fine Jan 4 '18 at 11:57

You mentioned two things, which I don't believe are good solutions:

"Created by":

Standard Linux file attributes include an owner user and group. I strongly recommend you make sure all files in /usr/local/bin (or actually almost all directories that contain stuff to be accessed by all users) are owned by root as user and group, and also only writable by the owner. This is a security measure that should prevent unprivileged modifications to the file, which might later be executed by someone else or with elevated privileges using sudo.

So as the owner of all those scripts in /usr/local/bin should be root, we can't use that to distinguish the scripts you want to back up.


Executables inside subdirectories of /usr/local/bin would not be accepted by the system as commands which you can run directly without a path. The reason is that for this, an executable needs to be directly inside a directory listed in the $PATH environment variable. Subdirectories are not automatically included here, each location needs to be explicitly specified - again, as a security improvement to lower the risk of accidentally executing untrusted code.

Although it would be possible to add that subdirectory in all necessary locations, I'd refrain from changing all users' $PATH (and not to forget, the /etc/sudoers configuration's secure_path must include the new directory too, if you want it accessible when using sudo).

Now what could you do instead? Here are a few ideas:

User-specific bin directory:

If you don't need those scripts system-wide for all users, but only for your own account, simply put them into ~/bin (~ being your home directory). This location is only on your own user's $PATH, not of anyone else. For your personal scripts, this should be the simplest way.

Keep in mind that you need to restart your current shell once (close and reopen the terminal window, type source ~/.profile or log out and back in) if you just created the directory during your current session. This is because the directory is added only if it exists when the shell initializes itself. You don't ever have to do this again later on.

Keep a list of your scripts:

Simply have a text file somewhere listing the paths of all of your scripts/files which you want to back up. Then you can simply process this list with another script/command like the one below to copy them all to your backup directory:

while read line ; do
    cp -- "$line" "$backupdir"
done < "$listfile"

You just have to keep the list file (here ~/backup/script-list.txt) updated and well formatted. In this simple version, you must make sure that each line contains exactly one absolute path to a file you want to back up, with no leading or trailing spaces and no empty lines. Of course the script could be improved to be able to handle such cases, but this is just a quick prototype.

Add a "signature" to your own scripts, and only back up those containing it:

Just think of a unique string you add to all of your own scripts, e.g. in a comment. That could look like *** CREATED BY BYTECOMMANDER *** or so, maybe even more unique. Then you can make a backup script that scans /usr/local/bin for files containing your signature line and only copies those, leaving others alone:

signature="*** CREATED BY BYTECOMMANDER ***"
for scriptfile in /usr/local/bin/* ; do
    if grep -qF "$signature" "$scriptfile" ; then
        cp -- "$scriptfile" "$backupdir"
  • That is simply brilliant. I salute, Commander! – j doe will do just fine Jan 4 '18 at 12:07
  • @jdoewilldojustfine Nice. If this answer solves your problem, you might please consider accepting it by clicking the grey round check button on its right. Thanks and welcome to Ask Ubuntu. – Byte Commander Jan 4 '18 at 15:47

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.