Ich habe folgende Verzeichnisstruktur:
Main_Dir
|
-----------------------------------
Subdir1 Subdir2 Subdir3
| | |
----------- ----------- -----------
| | | | | | | | |
fo1 fo2 f03 fo1 fo2 f03 fo1 fo2 f03
Ich möchte alle Unterverzeichnisse (Subdir1, Subdir2, Subdir3) in einen neuen Ordner kopieren. Aber wie kopiere ich nur die Ordner fo1 und fo2 an den neuen Ort?
Antwort1
Verwenden rsync
:
rsync -av --exclude="f03" /path/to/Main_Dir/ /pth/to/destination
Antwort2
Die beste Option ist die Verwendung von Globs und cp
mit der entsprechenden --parents
Option. In Ihrem Fall:
cp -nvr --parents -- Main_Dir/*/fo{1,2} Destination/
Sehen:
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!
Stellen Sie sicher, dass Sie die --parent
Option mit dem -r
Schalter verwenden. Die -v
Option, die ich verwendet habe, ist so, dass der Befehl ausführlich ist (er zeigt, was er tut) und die -n
Option ist fürkein Klatschum ansonsten vorhandene Dateien nicht zu überschreiben (das verwende ich sehr oft!).
Wenn Sie nicht möchten, Main_Dir
dass es im Verzeichnis ist, tun Sie es direkt aus dem Main_Dir heraus:
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$
Antwort3
In deinem Fall ist es nur möglich, wenn die Ordner fo1, fo2, fo3 unterschiedliche Namen haben, da du nicht drei Ordner mit dem gleichen Namen in einem Ordner haben kannst. Sieht es also so aus:
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
... Sie können (nur die Unterordner) kopieren, indem Sie:
user@host:~/test/1# cp -R main/*/fo* ../2/
Sieht jetzt ../2/
so aus:
user@host:~/test/1# cd ../2/
user@host:~/test/2# ls
fo1 fo2 fo3 fo4 fo5 fo6 fo7 fo8 fo9