2

i have many directories like this:

$ ls 
  1_true 1_false 2_true 2_false 3_true 3_false ...

i want to copy files in "*_true/" to other directory.

when i run command like below

find . -name "*_true" -exec cp "{}"/* "../out_true" ";"

this give me error messages like this:

cp: cannot stat './1_true/*': No such file or directory
cp: cannot stat './2_true/*': No such file or directory
cp: cannot stat './3_true/*': No such file or directory
...

how to fix this error? thanks!

NOTE: my 'true' directories count is more than 100000

1
  • Try ... -exec echo cp ... to see what's happening Oct 17, 2019 at 15:32

3 Answers 3

2

You can simplify it quite a bit

cp ?_true/* ../out_true/

will work as well.

This will, however, not work when you have a very large number of files.

2
1

found answer, using xargs instead of -exec

find . -name "*_true" -print0 | xargs --null -I{} sh -c 'cp "$1"/* "../out_true"' -- {}
1

You can limit find to descend not more than n levels with -maxdepth. This will match *_true and copy the content using rsync. Trailing slash on the source directory will change the behavior of rsync and only copy the content of the directory and not the directory it self.

find -maxdepth 1 -type d -name '*_true' -exec rsync -a "{}/" ../out_true \;

or

for i in *_true; do
    rsync -a "$i/" ../out_true
done
0

You must log in to answer this question.

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