[英]renaming series of files using xargs
I would like to rename several files picked by find
in some directory, then use xargs
and mv
to rename the files, with parameter expansion . 我想重命名在某个目录中的find
选择的几个文件,然后使用xargs
和mv
重命名文件,并带有参数expand 。 However, it did not work... 但是,它没有用...
example : 例如 :
mkdir test
touch abc.txt
touch def.txt
find . -type f -print0 | \
xargs -I {} -n 1 -0 mv {} "${{}/.txt/.tx}"
Result : 结果 :
bad substitution
[1] 134 broken pipe find . -type f -print0
Working Solution : 工作方案 :
for i in ./*.txt ; do mv "$i" "${i/.txt/.tx}" ; done
Although I finally got a way to fix the problem, I still want to know why the first find
+ xargs
way doesn't work, since I don't think the second way is very general for similar tasks. 尽管我终于找到了解决问题的方法,但是我仍然想知道为什么第一个find
+ xargs
方法不起作用,因为我认为第二种方法对于类似的任务不是很通用。
Thanks! 谢谢!
Remember that shell variable substitution happens before your command runs. 请记住,shell变量替换是在命令运行之前发生的。 So when you run: 因此,当您运行时:
find . -type f -print0 | \
xargs -I {} -n 1 -0 mv {} "${{}/.txt/.tx}"
The shell tries to expan that ${...}
construct before xargs
even runs...and since that contents of that expression aren't a valid shell variable reference, you get an error. Shell尝试在xargs
甚至运行之前扩展${...}
构造...并且由于该表达式的内容不是有效的Shell变量引用,因此会出现错误。 A better solution would be to use the rename
command: 更好的解决方案是使用rename
命令:
find . -type f -print0 | \
xargs -I {} -0 rename .txt .tx {}
And since rename
can operate on multiple files, you can simplify that to: 由于rename
可以对多个文件进行操作,因此您可以将其简化为:
find . -type f -print0 | \
xargs -0 rename .txt .tx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.