cp dir recursivly不包括2个子目录

时间:2021-09-16 01:07:53

I have 1 directory with 9 subdirectories and 10 files. Subdirectory have next level subdirectories and files.

我有1个目录,包含9个子目录和10个文件。子目录具有下一级子目录和文件。

/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?

最好的方法是什么?

7 个解决方案

#1


8  

Maybe the find command will help you:

也许find命令可以帮助你:

$ find /home/directory -mindepth 1 -maxdepth 1 -name 'subdirectory[57]' -or -exec cp -r {} /path/to/dir \;

#2


28  

rsync -avz --exclude subdirectory5 --exclude subdirectory7 /home/directory/ target-path

#3


10  

I don't know a good way of doing it with cp, but it's fairly easy using rsync and the --exclude switch.

我不知道用cp做这件事的好方法,但使用rsync和--exclude开关相当容易。

#4


2  

use rsync with --exclude is better

使用rsync和--exclude更好

#5


2  

You can use the --exclude option of tar:

您可以使用tar的--exclude选项:

{ cd /home/directory; tar -c --exclude=subdirectory5 --exclude=subdirectory7 .; } | { cd _destination_ ; tar -x; }

#6


2  

Why not just use the cp command like this:

为什么不直接使用这样的cp命令:

cp -r /home/directory/!(subdirectory5|subdirectory7) /destination

#7


1  

Kev's way is better, but this would also work:

Kev的方式更好,但这也有效:

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 to | sh to execute the command.

改变|猫到| sh执行命令。

<A big disclaimer goes here>

<一个很大的免责声明就在这里< p>

You should Kev's solution is better

Kev的解决方案应该更好

#1


8  

Maybe the find command will help you:

也许find命令可以帮助你:

$ find /home/directory -mindepth 1 -maxdepth 1 -name 'subdirectory[57]' -or -exec cp -r {} /path/to/dir \;

#2


28  

rsync -avz --exclude subdirectory5 --exclude subdirectory7 /home/directory/ target-path

#3


10  

I don't know a good way of doing it with cp, but it's fairly easy using rsync and the --exclude switch.

我不知道用cp做这件事的好方法,但使用rsync和--exclude开关相当容易。

#4


2  

use rsync with --exclude is better

使用rsync和--exclude更好

#5


2  

You can use the --exclude option of tar:

您可以使用tar的--exclude选项:

{ cd /home/directory; tar -c --exclude=subdirectory5 --exclude=subdirectory7 .; } | { cd _destination_ ; tar -x; }

#6


2  

Why not just use the cp command like this:

为什么不直接使用这样的cp命令:

cp -r /home/directory/!(subdirectory5|subdirectory7) /destination

#7


1  

Kev's way is better, but this would also work:

Kev的方式更好,但这也有效:

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 to | sh to execute the command.

改变|猫到| sh执行命令。

<A big disclaimer goes here>

<一个很大的免责声明就在这里< p>

You should Kev's solution is better

Kev的解决方案应该更好