linux批量修改空格文件名,如何在包含空格的Linux中批量重命名文件?
本问题已经有最佳答案,请猛点这里访问。我想通过用下划线替换所有包含空格的pdf文件。所以我调用命令:ls *.pdf | sed -e 'p; s/""/_/' | xargs -n2 mv我在终端上遇到错误:mv: cannot stat ‘Beginning’: No such file or directorymv: cannot stat ‘Linux’: No such file or
本问题已经有最佳答案,请猛点这里访问。
我想通过用下划线替换所有包含空格的pdf文件。
所以我调用命令:
ls *.pdf | sed -e 'p; s/""/_/' | xargs -n2 mv
我在终端上遇到错误:
mv: cannot stat ‘Beginning’: No such file or directory
mv: cannot stat ‘Linux’: No such file or directory
mv: cannot stat ‘Line.pdf’: No such file or directory
mv: cannot stat ‘The’: No such file or directory
mv: cannot stat ‘Command’: No such file or directory
mv: cannot stat ‘Head’: No such file or directory
mv: cannot stat ‘C.pdf’: No such file or directory
mv: cannot stat ‘First’: No such file or directory
mv: cannot stat ‘Head’: No such file or directory
mv: cannot stat ‘PHP’: No such file or directory
mv: cannot stat ‘MySQL.pdf’: No such file or directory
mv: cannot stat ‘First’: No such file or directory
mv: cannot stat ‘and’: No such file or directory
mv: cannot stat ‘Linux’: No such file or directory
mv: cannot stat ‘Guide.pdf’: No such file or directory
mv: cannot stat ‘Pocket’: No such file or directory
mv: cannot stat ‘The’: No such file or directory
mv: cannot stat ‘C.pdf’: No such file or directory
mv: cannot stat ‘ANSI’: No such file or directory
mv: cannot stat ‘The’: No such file or directory
mv: cannot stat ‘Command’: No such file or directory
mv: cannot stat ‘The’: No such file or directory
mv: cannot stat ‘Command’: No such file or directory
mv: cannot stat ‘The’: No such file or directory
mv: cannot stat ‘Programming’: No such file or directory
mv: cannot stat ‘The’: No such file or directory
mv: cannot stat ‘Programming’: No such file or directory
那么命令中有什么问题呢?
获取基于Perl的rename命令(有时称为prename)来完成这项工作:
prename 's/ /_/g' *.pdf
外壳程序将每个文件名分开; prename命令进行批量重命名; s/ /_/g操作在文件名中的每个位置都将每个空格替换为_。 (原始代码仅用下划线替换第一个空格,然后在空白处(空格,制表符以及换行符)运行xargs犯规。)
我的交易工具非常多(也,哈哈@重复:))
您可以尝试引用文件名:
ls *.pdf | sed -e 's/.*/"&"/; p; s/ /_/g' | xargs -n2 mv
同样,这也可能是您手工操作的方式:
mv"File with spaces""File_with_spaces"
依靠纯bash,您必须使用for循环和bash替换:
for file in $(ls *.pdf); do echo"$file""${file// /_}"; done
首先,您的sed命令不匹配任何文件,因为您尝试匹配quote-space-quote而不是仅匹配space。除非您有一个字面名为A""file.pdf的文件,否则它将不会与sed匹配。
假设您已将其固定为sed -e 'p; 's/ /_/g'。如果您有一个名为A file.pdf的文件,则输出将为
A file.pdf
A_file.pdf
并将这些作为参数传递给xargs。但是,指示xargs接受前两个参数。在这种情况下,它们将是A和file.pdf,都不存在,因此mv无法对其进行统计!
我认为这是一种更简单的方法:
for file in *.pdf; do mv"$file""$(echo $file | sed 's/ /_/g')"; done
您可以不用所有虚假的sed过程
正如@sehe指出的那样,如@Noctua的答案所示,您可以通过使用Bashs自己的替代方法mv"$file""${file _}"来放弃使用sed。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)