我有一堆文件的文件名遵循模式'filename.ext'。 例如:
filename .ext
我想重新命名所有这些以删除.ext之前的空格。 例如:
filename.ext
我可以find他们全部使用
find * -type f -name'* .*'
但我怎么才能重命名所有这些文件?
创build一个名为“renamethis.sh”的文件。 其内容应该是:
#!/bin/bash mv "$1" "$(echo $1 | sed 's/ \././')"
设置可执行位: chmod a+x renamethis.sh 。 然后,运行如下所示:
find /path/to/dir -name '* .*' -type f -print0 | xargs -0L 1 /path/to/renamethis.sh
YMMV,不作任何明示或暗示的保证
FWIW,空间是什么让这个奇怪的; 只要你在文件名中没有其他古怪的字符,你就可以用这种方法。 如果你这样做的话,你可能会想在Perl或PHP中考虑像scandir / readdir循环,但是上面的脚本是首先想到的。
这应该为你做。
#!/bin/bash OLDIFS=${IFS} IFS=$'\n' for file in `find * -type f -name '* .*'`; do _ext=`echo ${file} | cut -d '.' -f 2-` _filename=`echo ${file} | cut -d ' ' -f 1` mv "${file}" ${_filename}.${_ext} done IFS=${OLDIFS}
在一行中:
find -depth -name '* .*' -print0 | perl -wn0e '$orig = $_; s/\s+\././g; rename($orig, $_) or warn "$orig: $!\n"'
强调:
我看到你已经解决了你的眼前的问题,但将来你可能会考虑使用mmv 。