如何使用bash从文件扩展名中删除尾随空格?

时间:2015-12-30 21:01:09

标签: macos bash sed osx-yosemite

我首先尝试了documentationHow to remove trailing whitespace of all files recursively?处的所有内容,但这些内容无效。

例如,文件名为"image.jpg ",我想将其转换为"image.jpg"

请帮助,它也应该是递归的。 https://superuser.com/questions/402647/how-to-remove-trailing-whitespace-from-file-extensions-and-folders-snow

2 个答案:

答案 0 :(得分:1)

试一试。 (先备份你的数据)

find /tmp/ -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;

将/ tmp /替换为您的文件夹。

对于Apple来说,这是怎么回事:

for oldname in *
do
  newname=`echo $oldname | sed -e 's/ //g'`
  mv "$oldname" "$newname"
done

答案 1 :(得分:0)

find . -depth ...是最好的答案,但不幸的是,你无法使用(除非你安装自制软件)

for解决方案的一个难点是它不会深入到目录层次结构深度优先级。因此,您首先要重命名目录,然后无法重命名其下的任何文件。

要确保在任何父目录找到文件之前重命名文件,然后反向排序:

shopt -s globstar nullglob extglob
printf "%s\n" **/*[[:space:]] | sort -r | while IFS= read -r filename; do
    newname=${filename/%+([[:space:]])}
    mv "$filename" "$newname"
done
相关问题