且构网

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

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

更新时间:2022-05-10 08:14:49

使用 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 的回答,并且能够使用Revision 1.5 1998/12/18 16:16:31 rmb1"在单个边界中处理多层文件和目录;/usr/bin/rename 版本(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' "{}" ;