且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用bash脚本替换文件名中的空格

更新时间:2021-09-05 08:46:27

使用rename(又名prename),这是一个Perl脚本,可能已经在您的系统上.分两个步骤进行:

Use rename (aka prename) which is a Perl script which may be on your system already. Do it in two steps:

find -name "* *" -type d | rename 's/ /_/g'    # do the directories first
find -name "* *" -type f | rename 's/ /_/g'

基于Jürgen的答案,并且能够使用/usr/bin/rename版本的"Revision 1.5 1998/12/18 16:16:31 rmb1"在一个绑定中处理多层文件和目录(Perl脚本):

Based on Jürgen's answer and able to handle multiple layers of files and directories in a single bound using the "Revision 1.5 1998/12/18 16:16:31 rmb1" version of /usr/bin/rename (a Perl script):

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