I have 1 directory with 9 subdirectories and 10 files. Subdirectory have next level subdirectories and files.
/home/directory/
/home/directory/subdirectory1
/home/directory/subdirectory2
...
/home/directory/subdirectory9
/home/directory/file1
...
/home/directory/file10
I want to copy all subdirectories and files recursivly excluding:
/home/directory/subdirectory5
/home/directory/subdirectory7
What is the best way for it?
rsync -avz --exclude subdirectory5 --exclude subdirectory7 /home/directory/ target-path
我不知道用cp
做这件事的好方法,但使用rsync
和--exclude
开关相当容易。
也许find
命令可以帮助你:
$ find /home/directory -mindepth 1 -maxdepth 1 -name 'subdirectory[57]' -or -exec cp -r {} /path/to/dir \;
为什么不像这样使用cp
命令:
cp -r /home/directory/!(subdirectory5|subdirectory7) /destination
使用rsync
和--exclude
更好
您可以使用tar
的--exclude
选项:
{ cd /home/directory; tar -c --exclude=subdirectory5 --exclude=subdirectory7 .; } | { cd _destination_ ; tar -x; }
Kev's way is better, but this would also work:
find "/home/folder" -maxdepth 1 | sed -e "/^\/home\/folder$/d" -e "/^\/home\/folder\/subfolder5$/d" -e "/^\/home\/folder\/subfolder7$/d" -e "s/^/cp \-r /" -e "s/$/ \/home\/target/" | cat
Explained :
find "/home/folder" -maxdepth 1 |
// get all files and dirs under /home/folder, pipe output
sed -e "/^\/home\/folder$/d"
// have sed strip the path being searched, or the cp -r we prepend later will pickup the excluded dirs again.
-e "/^\/home\/folder\/subfolder5$/d"
// have sed strip subfolder5
-e "/^\/home\/folder\/subfolder7$/d"
// have sed strip subfolder7
-e "s/^/cp \-r /"
// have sed prepend "cp -r " to each line
-e "s/$/ \/home\/target/" | cat
// have sed append targetdir to each line.
Outputs:
cp -r /home/folder/subfolder9 /home/target
cp -r /home/folder/subfolder1 /home/target
cp -r /home/folder/file10 /home/target
cp -r /home/folder/subfolder2 /home/target
cp -r /home/folder/file1 /home/target
cp -r /home/folder/subfolder3 /home/target
Change | cat
| cat
to | sh
| sh
to execute the command.
<A big disclaimer goes here>
You should Kev's solution is better
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.