4

I am searching files who have either been created or modified for the last 60 minuts. I find these via

find ~/data/ -cmin -60 -mmin -60 -type f

~ the home directory /usr/wg/

After that I want to copy these files and preserve the main folder structure... The results of the find command are for instance...

/usr/wg/data/foo1/file1.txt
/usr/wg/data/foo2/bar2/file2.txt
...

Now when I use

rsync -a `find ~/data/ -cmin -60 -mmin -60 -type f` ~/vee/

In the folder ~/vee/ I get

/usr/wg/vee/usr/wg/data/foo1/file1.txt
/usr/wg/vee/usr/wg/data/foo2/bar2/file2.txt
...

While I want

/usr/wg/vee/foo1/file1.txt
/usr/wg/vee/foo2/bar2/file2.txt
...

How do I achieve this? I looked at

  1. How to copy modified files while preserving folder structure
  2. https://serverfault.com/questions/180853/how-to-copy-file-preserving-directory-path-in-linux
  3. https://stackoverflow.com/questions/1650164/bash-copy-named-files-recursively-preserving-folder-structure

and several other answers, but I do not seem to get it right.

2 Answers 2

1

You should rewrite your command on this way:

cd ~/data; find . -cmin -60 -mmin -60 -type f

to be able to get from find relative paths

And maybe something like

find ...  -exec cp -r "{}" ../vee/ \; 

will do the work with copy the files with subdirectory structure

0

You can use --parents switch:

cd ~/data; find . -cmin -60 -mmin -60 -type f -exec cp --parents "{}" ~/vee/

From man pages:

--parents
use full source file name under DIRECTORY

that is, when you get in the path result:

./foo1/file1.txt
./foo2/bar2/file2.txt

cp will use the full path of the input file and add it to the target path as follows:

~/vee/./foo1/file1.txt
~/vee/./foo2/bar2/file2.txt

and recreates this directory structure

3
  • cp(1) tells me --parents - use full source file name under DIRECTORY. Please explain what is the change in behavior of the cp command in this case? Thanks!
    – Rich
    Dec 10, 2020 at 15:29
  • 1
    @Rich I also needed such a solution (copying the whole path), and this switch helped me. That is why I gave this my answer.
    – Gander
    Dec 10, 2020 at 16:19
  • Your answer would be much better if you could add a bit of an explanation of what exactly --parents does (and how that depends on the exact form of the source path, if at all), instead of an one-liner like this.
    – TooTea
    Dec 11, 2020 at 8:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .