我想重命名所有包含空格的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那么这个命令有什么问题呢?
发布于 2014-05-26 06:59:19
获取基于Perl的rename命令(有时称为prename)来完成以下工作:
prename 's/ /_/g' *.pdfshell全局化使每个文件名保持独立;prename命令进行批量重命名;s/ /_/g操作将每个空白替换为文件名中的任何位置的_。(原始代码只用下划线替换第一个空白,然后在空白(空格和制表符以及换行符)上运行xargs中断。)
发布于 2014-05-26 06:44:43
首先,您的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发布于 2014-05-26 06:52:05
您可以尝试引用文件名:
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// /_}"; donehttps://stackoverflow.com/questions/23864135
复制相似问题