Skip to content
Advertisement

Copy bulk files to another destination using a given prefix in bash

I want to copy all the files newer than another one from their original location /lib/ into another destination /dest/lib/

To identify these files I simply use:

$ cd /lib/
$ find . -newer foobar
./foo/bar.py
./bar/foo/bar/foo.py
...

Unfortunately I cannot simply use this command because cp does not know how to create folders on the fly.

So I used this command:

find /foo -type f -newer foobar | 
    xargs -n1 -I% sh -c 'mkdir -vp `dirname /dest/%` && cp -v % /dest/%'

This looks very complicated and is incredibly slow. Is there a better solution?

Advertisement

Answer

You can use tar for this, for example, let’s you have directory ‘1’, and you want copy something to directory ‘2’, then:

cd 1
tar -c a.txt 3/b.txt | tar -x -C ../2

also you can use rsync it works with local paths

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement