0

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.

  • Are there subdirectories in `src/`? If so, how are they handled in `dst/`? Is the date-coded directory immediately under `dst/` or between the source directory and the target file? – AFH Jan 05 '16 at 13:02
  • @AFH There are no subdirectories in `src`. In the `dst` directory, I will create a folder with the current date, for example `dst/2016-01-04`, and put all files in there that did not exist anywhere in `dst` yet. (I do not want to depend on how long the files have existed in `src`) – Dennis Jaheruddin Jan 05 '16 at 14:39

1 Answers1

1

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).
AFH
  • 17,300
  • 3
  • 32
  • 48