Sha*_*han 8 cp recursive file-copy
我有以下目录结构:
Main_Dir
|
-----------------------------------
Subdir1 Subdir2 Subdir3
| | |
--------- --------- ---------
| | | | | | | | |
fo1 fo2 f03 fo1 fo2 f03 fo1 fo2 f03
Run Code Online (Sandbox Code Playgroud)
我想将所有子目录 ( Subdir1
, Subdir2
, Subdir3
)复制到一个新文件夹中。但我只想在新地方复制fo1
和fo2
文件夹。
不知道怎么做。
如果目录树不仅仅是目录树,..../f03
您可以使用此rsync
命令复制每个fo1
&fo2
并排除所有其他名称为 的目录fo*
。
$ rsync -avz --include='fo[12]/' --exclude='fo*/' \
Main_Dir/ new_Main_Dir/.
Run Code Online (Sandbox Code Playgroud)
在处理这些类型的复制场景时,我总是使用rsync
它的--dry-run
&--verbose
开关,这样我就可以看到它要做什么,而无需实际复制文件。
$ rsync -avz --dry-run --verbose --include='fo[12]/' --exclude='fo*/' \
Main_Dir/ new_Main_Dir/.
Run Code Online (Sandbox Code Playgroud)
试运行。
$ rsync -avz --dry-run --include='fo[12]/' --exclude='fo*/' \
Main_Dir/ new_Main_Dir/.
sending incremental file list
./
Subdir1/
Subdir1/fo1/
Subdir1/fo2/
Subdir2/
Subdir2/fo1/
Subdir2/fo2/
Subdir3/
Subdir3/fo1/
Subdir3/fo2/
sent 201 bytes received 51 bytes 504.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
Run Code Online (Sandbox Code Playgroud)
如果您想查看有关rsync
包含/排除内容的某些内部逻辑,请使用该--verbose
开关。
$ rsync -avz --dry-run --verbose --include='fo[12]/' --exclude='fo*/' \
Main_Dir/ new_Main_Dir/.
sending incremental file list
[sender] showing directory Subdir1/fo2 because of pattern fo[12]/
[sender] showing directory Subdir1/fo1 because of pattern fo[12]/
[sender] hiding directory Subdir1/fo3 because of pattern fo*/
[sender] showing directory Subdir2/fo2 because of pattern fo[12]/
[sender] showing directory Subdir2/fo1 because of pattern fo[12]/
[sender] hiding directory Subdir2/fo3 because of pattern fo*/
[sender] showing directory Subdir3/fo2 because of pattern fo[12]/
[sender] showing directory Subdir3/fo1 because of pattern fo[12]/
[sender] hiding directory Subdir3/fo3 because of pattern fo*/
delta-transmission disabled for local transfer or --whole-file
./
Subdir1/
Subdir1/fo1/
Subdir1/fo2/
Subdir2/
Subdir2/fo1/
Subdir2/fo2/
Subdir3/
Subdir3/fo1/
Subdir3/fo2/
total: matches=0 hash_hits=0 false_alarms=0 data=0
sent 201 bytes received 51 bytes 504.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
Run Code Online (Sandbox Code Playgroud)
如果您需要排除其他形式的目录,您可以添加多个排除项。
你可以尝试这样的事情:
find Main_Dir -maxdepth 1 -mindepth 1 -type d | while IFS= read -r subdir; do
mkdir -p new_dir/"$(basename $subdir)" &&
cp -r "$subdir"/{fo1,fo2} new_dir/"$(basename $subdir)"/;
done
Run Code Online (Sandbox Code Playgroud)
该find
命令返回 Main_Dir 的所有直接子目录。basename
将返回找到的子目录的名称(例如basename Main_Dir/Subdir1
returns Subdir1
)。然后,您可以使用 shell 的大括号扩展来避免多次键入fo1
和,并将它们复制到新创建的目录中。fo2
new_dir/$(basename $subdir)
在您提到的特定情况下,只有下面的目录Main_Dir
并且名称中没有空格或奇怪的字符,您可以将上面的内容简化为
cd Main_Dir; for subdir in *; do
mkdir -p ../new_dir/$subdir && cp -rv $subdir/{fo1,fo2} ../new_dir/$subdir;
done
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11216 次 |
最近记录: |