开发者

Unix batch rename folders (underscores to spaces)

I have a collection of folders, and I would like to replace all underscores in the开发者_开发技巧 folder names with spaces.

Any ideas?

Thanks


(bash) This will find all folders in or beneath current directory with an underscore in the name and rename them as you mention:

for d in $(find . -name '*_*' -type d) ; do
    new=$(echo $d | sed -e 's/_/ /g')
    mv $d $new
done


you could try something like

#> ls -l | grep '^d' | awk '{oldname = $9 ; gsub(/_/, " " ,$9);  print "mv " oldname " "  $9 }' > temp.script
#> chmod 744 temp.script
#> ./temp.script

Test it first of course :)


Does your system have a rename command that does what you need? On some systems, it exists and can use some species of regular expression to make the changes. I use a version which uses Perl regular expressions (a Perl script, in other words):

find . -type f -name '*_*' -print0 | xargs -0 rename 's/_+/ /g'

On the other hand, the standard version on Linux tends to be rather feeble by comparison (and wouldn't do what you need).

Note that if you want to rename directories too, then do them before renaming any files, or after renaming all the files. Doing them mixed up will mean that your renaming runs into problems with accessing file names that were in one directory but the directory has been renamed since the file name was generated. Even using -depth as an option to find is not guaranteed to be safe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜