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.
"Subdirectory":
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:
#!/bin/bash
backupdir=~/backup/scripts
listfile=~/backup/script-list.txt
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:
#!/bin/bash
backupdir=~/backup/scripts
signature="*** CREATED BY BYTECOMMANDER ***"
for scriptfile in /usr/local/bin/* ; do
if grep -qF "$signature" "$scriptfile" ; then
cp -- "$scriptfile" "$backupdir"
fi
done
/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 03 '18 at 23:37.profile
, (yup, I'm that green), so I'll do some reading. – j doe will do just fine Jan 04 '18 at 11:57