批量替换终端文件名中的字符?

时间:2011-04-12 04:08:24

标签: macos unix

我在目录中有大约50个文件,其中包含空格,撇号等。如何对它们进行批量重命名以删除撇号并用下划线替换空格?

我能做到

ls | grep '*.txt' | xargs ....

但我不知道该怎么做xargs位

1 个答案:

答案 0 :(得分:2)

我使用ren-regexp,这是一个Perl脚本,可让您轻松地批量重命名文件。

你会做ren-regexp 's/ /_/g' *.txt之类的事情。

$ ls -l
total 16
-rw-r--r--  1 marc  marc  7 Apr 11 21:18 That's a wrap.txt
-rw-r--r--  1 marc  marc  6 Apr 11 21:18 What's the time.txt

$ ren-regexp "s/\'//g" "s/ /_/g" *.txt

  That's a wrap.txt
1 Thats a wrap.txt
2 Thats_a_wrap.txt

  What's the time.txt
1 Whats the time.txt
2 Whats_the_time.txt


$ ls -l
total 16
-rw-r--r--  1 marc  marc  7 Apr 11 21:18 Thats_a_wrap.txt
-rw-r--r--  1 marc  marc  6 Apr 11 21:18 Whats_the_time.txt