我有以下目錄結構:
Main_Dir
|
-----------------------------------
Subdir1 Subdir2 Subdir3
| | |
----------- ----------- -----------
| | | | | | | | |
fo1 fo2 f03 fo1 fo2 f03 fo1 fo2 f03
我想將所有子目錄(Subdir1、Subdir2、Subdir3)複製到新資料夾。但是我如何只將 fo1 和 fo2 資料夾複製到新位置呢?
答案1
使用rsync
:
rsync -av --exclude="f03" /path/to/Main_Dir/ /pth/to/destination
答案2
最好的選擇是使用 glob 及其cp
選項--parents
。在你的情況下:
cp -nvr --parents -- Main_Dir/*/fo{1,2} Destination/
看:
gniourf@somewhere$ mkdir -pv Main_Dir/Subdir{1..3}/f{o{1,2},03}
mkdir: created directory `Main_Dir'
mkdir: created directory `Main_Dir/Subdir1'
mkdir: created directory `Main_Dir/Subdir1/fo1'
mkdir: created directory `Main_Dir/Subdir1/fo2'
mkdir: created directory `Main_Dir/Subdir1/f03'
mkdir: created directory `Main_Dir/Subdir2'
mkdir: created directory `Main_Dir/Subdir2/fo1'
mkdir: created directory `Main_Dir/Subdir2/fo2'
mkdir: created directory `Main_Dir/Subdir2/f03'
mkdir: created directory `Main_Dir/Subdir3'
mkdir: created directory `Main_Dir/Subdir3/fo1'
mkdir: created directory `Main_Dir/Subdir3/fo2'
mkdir: created directory `Main_Dir/Subdir3/f03'
gniourf@somewhere$ tree Main_Dir/
Main_Dir/
|-- Subdir1
| |-- f03
| |-- fo1
| `-- fo2
|-- Subdir2
| |-- f03
| |-- fo1
| `-- fo2
`-- Subdir3
|-- f03
|-- fo1
`-- fo2
12 directories, 0 files
gniourf@somewhere$ mkdir Destination
gniourf@somewhere$ cp -nvr --parents -- Main_Dir/*/fo{1,2} Destination/
Main_Dir -> Destination/Main_Dir
Main_Dir/Subdir1 -> Destination/Main_Dir/Subdir1
`Main_Dir/Subdir1/fo1' -> `Destination/Main_Dir/Subdir1/fo1'
Main_Dir/Subdir2 -> Destination/Main_Dir/Subdir2
`Main_Dir/Subdir2/fo1' -> `Destination/Main_Dir/Subdir2/fo1'
Main_Dir/Subdir3 -> Destination/Main_Dir/Subdir3
`Main_Dir/Subdir3/fo1' -> `Destination/Main_Dir/Subdir3/fo1'
`Main_Dir/Subdir1/fo2' -> `Destination/Main_Dir/Subdir1/fo2'
`Main_Dir/Subdir2/fo2' -> `Destination/Main_Dir/Subdir2/fo2'
`Main_Dir/Subdir3/fo2' -> `Destination/Main_Dir/Subdir3/fo2'
gniourf@somewhere$ tree Destination
Destination
`-- Main_Dir
|-- Subdir1
| |-- fo1
| `-- fo2
|-- Subdir2
| |-- fo1
| `-- fo2
`-- Subdir3
|-- fo1
`-- fo2
10 directories, 0 files
gniourf@somewhere$ # Done!
確保使用--parent
帶-r
開關的選項。我使用的選項-v
是讓命令很詳細(它顯示了它正在做什麼)並且該-n
選項用於沒有破壞以免覆蓋其他現有文件(我經常使用這個文件!)。
如果您不希望Main_Dir
在目錄中,請直接從 Main_Dir 執行此操作:
gniourf@somewhere$ mkdir -p Main_Dir/Subdir{1..3}/f{o{1,2},03} Destination
gniourf@somewhere$ cd Main_Dir/
gniourf@somewhere$ cp -nvr --parents -- */fo{1,2} ../Destination
Subdir1 -> ../Destination/Subdir1
`Subdir1/fo1' -> `../Destination/Subdir1/fo1'
Subdir2 -> ../Destination/Subdir2
`Subdir2/fo1' -> `../Destination/Subdir2/fo1'
Subdir3 -> ../Destination/Subdir3
`Subdir3/fo1' -> `../Destination/Subdir3/fo1'
`Subdir1/fo2' -> `../Destination/Subdir1/fo2'
`Subdir2/fo2' -> `../Destination/Subdir2/fo2'
`Subdir3/fo2' -> `../Destination/Subdir3/fo2'
gniourf@somewhere$ cd ..
gniourf@somewhere$ tree Destination/
Destination/
|-- Subdir1
| |-- fo1
| `-- fo2
|-- Subdir2
| |-- fo1
| `-- fo2
`-- Subdir3
|-- fo1
`-- fo2
9 directories, 0 files
gniourf@somewhere$
答案3
在您的情況下,如果資料夾 fo1、fo2、fo3 有不同的名稱,這是可能的,因為一個資料夾中不能有 3 個同名的資料夾。所以如果它看起來像這樣:
user@host:~/test/1# find
.
./main
./main/sub1
./main/sub1/fo3
./main/sub1/fo1
./main/sub1/fo2
./main/sub2
./main/sub2/fo6
./main/sub2/fo4
./main/sub2/fo5
./main/sub3
./main/sub3/fo9
./main/sub3/fo8
./main/sub3/fo7
....您可以透過以下方式複製(僅子資料夾):
user@host:~/test/1# cp -R main/*/fo* ../2/
現在../2/
看起來像這樣:
user@host:~/test/1# cd ../2/
user@host:~/test/2# ls
fo1 fo2 fo3 fo4 fo5 fo6 fo7 fo8 fo9