使用 xargs 重命名一系列文件
renaming series of files using xargs
我想重命名find
在某个目录中选择的几个文件,然后使用xargs
和mv
重命名文件,并使用参数扩展。然而,它并没有起作用...
示例:
mkdir test
touch abc.txt
touch def.txt
find . -type f -print0 | \
xargs -I {} -n 1 -0 mv {} "${{}/.txt/.tx}"
结果:
bad substitution
[1] 134 broken pipe find . -type f -print0
工作解决方案:
for i in ./*.txt ; do mv "$i" "${i/.txt/.tx}" ; done
虽然我终于找到了解决问题的方法,但我仍然想知道为什么第一种 find
+ xargs
方法不起作用,因为我不认为第二种方法对于类似的任务来说非常通用。
谢谢!
请记住,shell 变量替换发生在 之前 您的命令 运行。所以当你 运行:
find . -type f -print0 | \
xargs -I {} -n 1 -0 mv {} "${{}/.txt/.tx}"
shell 试图在 xargs
之前扩展 ${...}
结构
运行s...并且由于该表达式的内容不是有效的 shell 变量引用,您会收到错误消息。更好的解决方案是使用 rename
命令:
find . -type f -print0 | \
xargs -I {} -0 rename .txt .tx {}
并且由于 rename
可以对多个文件进行操作,您可以简化
那就是:
find . -type f -print0 | \
xargs -0 rename .txt .tx
我想重命名find
在某个目录中选择的几个文件,然后使用xargs
和mv
重命名文件,并使用参数扩展。然而,它并没有起作用...
示例:
mkdir test
touch abc.txt
touch def.txt
find . -type f -print0 | \
xargs -I {} -n 1 -0 mv {} "${{}/.txt/.tx}"
结果:
bad substitution
[1] 134 broken pipe find . -type f -print0
工作解决方案:
for i in ./*.txt ; do mv "$i" "${i/.txt/.tx}" ; done
虽然我终于找到了解决问题的方法,但我仍然想知道为什么第一种 find
+ xargs
方法不起作用,因为我不认为第二种方法对于类似的任务来说非常通用。
谢谢!
请记住,shell 变量替换发生在 之前 您的命令 运行。所以当你 运行:
find . -type f -print0 | \
xargs -I {} -n 1 -0 mv {} "${{}/.txt/.tx}"
shell 试图在 xargs
之前扩展 ${...}
结构
运行s...并且由于该表达式的内容不是有效的 shell 变量引用,您会收到错误消息。更好的解决方案是使用 rename
命令:
find . -type f -print0 | \
xargs -I {} -0 rename .txt .tx {}
并且由于 rename
可以对多个文件进行操作,您可以简化
那就是:
find . -type f -print0 | \
xargs -0 rename .txt .tx