Geuis
Geuis

Reputation: 42267

Mass replace characters in filenames from terminal?

I have about 50 files in a directory that contain spaces, apostrophes, etc. How can I go about mass-renaming them to remove the apostrophes and replaces spaces with underscores?

I can do

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

but I'm not sure what to do in the xargs bit

Upvotes: 1

Views: 1952

Answers (1)

Marc Abramowitz
Marc Abramowitz

Reputation: 3577

I use ren-regexp, which is a Perl script that lets you mass-rename files very easily.

You'd do something like 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

Upvotes: 2

Related Questions