I have 3 distinct folders: history, inbox, backup.
I need to copy all the files from 'history' to 'inbox', only if they are not present in 'backup'.
How to do this?
Just an example, is there any subfolder in history ?
This script would loop through all possible files in history folder, and extract the basename of it (e.g the basename of /bin/ls is ls), and check if the file exists in backup folder; if not, do the copy procedure.
This simple script works only if the three directories don't contain any subdirectories (a more complicated recursive algorithm would be required to handle that case).
Warlock's answer is good, but the advantage of a script like this over Warlock's one-liner is that by using xargs we don't fork an instance of cp for every file that we copy. We only fork one instance of cp for every time xargs generates a command line that fills the shell's command-line length limit. That tends to be huge on modern systems, so it's quite likely we'd only run one instance of cp total, even for hundreds or possibly thousands of files in history/
FYI, the long-form of the grep options used here are:
and an even more compact form is:
Most of the options are commonly used or self-explanatory, but three may require some explanation: