我正在尝试使用find
命令重命名文件。
我正在尝试将文件-a重命名为文件10。
为此,我首先尝试使用以下命令:
sps@sps-Inspiron-N5110:~$ find ~ -type f -name test-a -exec mv test-10 '{}' ';'
mv: cannot stat `test-10': No such file or directory
sps@sps-Inspiron-N5110:~$
然后我尝试如下:
sps@sps-Inspiron-N5110:~$ find ~ -type f -name test-a -exec mv test-a test-10 '{}' ';'
mv: target `/home/sps/test-a' is not a directory
sps@sps-Inspiron-N5110:~$
现在我想不出该怎么做find
。我正在尝试使用进行此操作find
,因为我将有许多具有相同文件名的目录,并且我想在一个命令中将所有更改test-a
为test-10
。任何人请提出建议。
谢谢。
的语法mv
为mv <source> <target>
,因此find
执行的最终命令应类似于:
mv test-a test-10
因此,第一个猜测是尝试:
find ~ -type f -name test-a -exec mv {} test-10 \;
但是,这将失败,因为{}
将其扩展到完整路径并mv
仍在当前目录中运行,从而导致所有文件都移至您的当前目录并被覆盖。为了避免这种情况,可以使用,-execdir
以便mv
在找到文件的目录中执行该命令:
find ~ -type f -name test-a -execdir mv {} test-10 \;
或者,由于文件名始终相同:
find ~ -type f -name test-a -execdir mv test-a test-10 \;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句