如何从带有 bash 的文件扩展名中删除尾随空格?
How to remove trailing whitespace from file extensions with bash?
我首先在 How to remove trailing whitespace of all files recursively? and https://superuser.com/questions/402647/how-to-remove-trailing-whitespace-from-file-extensions-and-folders-snow 尝试了所有方法,但没有用。
例如文件名是"image.jpg "
,我想把它转换成"image.jpg"
。
请帮忙,它也应该是递归的。 example
试一试。 (先备份你的数据)
find /tmp/ -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;
将 /tmp/ 替换为您的文件夹。
苹果,这个怎么样:
for oldname in *
do
newname=`echo $oldname | sed -e 's/ //g'`
mv "$oldname" "$newname"
done
find . -depth ...
是最佳答案,但遗憾的是您无法使用它(除非您安装自制软件)
for
解决方案的一个难点是它没有进入目录层次结构 depth-first。因此,您可能会先重命名一个目录,然后再重命名该目录下的任何文件。
要确保在任何父目录之前重命名文件,请先找到文件然后反向排序:
shopt -s globstar nullglob extglob
printf "%s\n" **/*[[:space:]] | sort -r | while IFS= read -r filename; do
newname=${filename/%+([[:space:]])}
mv "$filename" "$newname"
done
我首先在 How to remove trailing whitespace of all files recursively? and https://superuser.com/questions/402647/how-to-remove-trailing-whitespace-from-file-extensions-and-folders-snow 尝试了所有方法,但没有用。
例如文件名是"image.jpg "
,我想把它转换成"image.jpg"
。
请帮忙,它也应该是递归的。 example
试一试。 (先备份你的数据)
find /tmp/ -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;
将 /tmp/ 替换为您的文件夹。
苹果,这个怎么样:
for oldname in *
do
newname=`echo $oldname | sed -e 's/ //g'`
mv "$oldname" "$newname"
done
find . -depth ...
是最佳答案,但遗憾的是您无法使用它(除非您安装自制软件)
for
解决方案的一个难点是它没有进入目录层次结构 depth-first。因此,您可能会先重命名一个目录,然后再重命名该目录下的任何文件。
要确保在任何父目录之前重命名文件,请先找到文件然后反向排序:
shopt -s globstar nullglob extglob
printf "%s\n" **/*[[:space:]] | sort -r | while IFS= read -r filename; do
newname=${filename/%+([[:space:]])}
mv "$filename" "$newname"
done