Copy files that don’t exist in higher directory

Posted on

Problem :

I currently know how to copy files that don’t exist in the target directory. This allows me to copy from src to dst just as I would like.

However, now I want to copy from src to dst/2016-01-05 BUT only the files in src that do not exist anywhere inside dst.

Example

Suppose the start situation is

src/f1.txt
src/f2.txt
src/f3.txt

dst/2016-01-04/f1.txt
dst/2016-01-05/f0.txt

Then, after doing the copy, the end situation should be:

src/f1.txt
src/f2.txt
src/f3.txt

dst/2016-01-04/f1.txt
dst/2016-01-05/f0.txt

dst/2016-01-05/f2.txt
dst/2016-01-05/f3.txt

In general I would not like to overwrite existing files. Even if the source is updated.

Solution :

The following should do the trick:-

today=`date +%Y-%m-%d`
ls -A src/ | while f=`line`; do if [ ! -f "dst/*/$f" ]; 
                 then mkdir -p "dst/$today"; cp "src/$f" "dst/$today/$f"; fi; done

Notes:-

  1. Compared with the alternative of for f in src/*; ..., using ls strips the directory from the source name, and -A includes file names beginning with ., .
  2. If there are subdirectories in src/ you will need to to use find in the source directory and strip src/ from the name with -printf %P\n.
  3. If you don’t have the line command you can use while read f; ..., but this does not work for file names with leading and trailing white space (even line fails if the file name contains a new-line character – for this you would need to use find -print0 and xargs -0).

Leave a Reply

Your email address will not be published.