Reputation: 59
I need to copy files from directories and subdirectories. Copy files to a destination directory and preserve same source file/directory structure. For some source files and directories i get "cannot access" or "No such file or directory" and the cp get stuck. I would like to skip those files/dir with error. Is there a single command i can use for it? Or it is best to write up a bash script?
Background: I am performing recovery of a dead vm, some files are corrupted, hence having those error message.
Upvotes: -1
Views: 480
Reputation: 6074
Use find and xargs to run a single cp per file. This should prevent hangs from cp getting internally confused.
I made a directory with 2 bad files. I made them root and forced a permission denied to demonstrate.
% ls -l old
old:
total 668
---------- 1 root staff 0 Sep 7 08:38 bad1.txt
---------- 1 root staff 0 Sep 7 08:38 bad2.txt
-rw-r--r-- 1 risner staff 588 Sep 7 08:39 good1.txt
-rw-r--r-- 1 risner staff 677977 Sep 7 08:39 good2.txt
I ran this command:
% (cd old; \
find . -type f -print0 | \
xargs -0 -I% cp -f -p % ../new/% \
)
cp: cannot open './bad1.txt' for reading: Permission denied
cp: cannot open './bad2.txt' for reading: Permission denied
% ls -l new
total 668
-rw-r--r-- 1 risner risner 588 Sep 7 08:39 good1.txt
-rw-r--r-- 1 risner risner 677977 Sep 7 08:39 good2.txt
Upvotes: 1