当我运行命令时
find dir_1 -type f -name 'f*'
我在那里找到了 2 个文件“file_11”和“file_22”。现在我想使用 -exec 和“find”命令将这些文件移动到另一个目录“dir_2”。所以我使用这个命令。
find dir_1 -type f -name 'f*' -exec mv {} ../dir2 \;
此命令有效。
现在,目录“dir_1”中也有一些“.txt”文件。我想将这些文本文件移动到“dir2”并重命名这些文件。所以我使用这个命令,
find dir_1 -type f -name '*.txt' -exec mv {} ../dir2/new_{} \;
但我收到错误,
mv: cannot move 'dir_1/file1.txt' to '../dir2/new_dir_1/file1.txt': No such file or directory
mv: cannot move 'dir_1/file2.txt' to '../dir2/new_dir_1/file2.txt': No such file or directory
谁能帮我。
find dir_1 -type f -name '*.txt' -exec \
sh -c 'echo mv "$1" "/path/to/dir_2/new_${1##*/}"' sh_mv {} \;
语法${parameter##word}
是shell 参数扩展。和从其给定参数中指定的单词指定的 cut-up-to-last-prefix ;这里它从文件名中去除了路径部分。so变得只是后来添加到文件名中。./dir_1/sub_directory/file1.txt
file1.txt
new_
该sh -c '...'
构造被称为内联 shell(此处为sh
shell),我们打开它是为了提供/构建mv
命令移动和重命名所需的参数,并为此使用其参数扩展功能。
with sh -c '...' sh_mv {}
,它接受两个参数,一个是sh_mv
(用作我们打开的内联外壳的标签(参数$0
));该{}
参数将是文件名的取代发现find
命令,将通过作为下一个参数(参数$1
)。
注意:删除echo
以执行我们用于试运行测试的实际 mv。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句