Hid Dencum
Hid Dencum

Reputation: 592

Replace parentheses and spaces in filenames with underscore

I use this line to remove blank spaces on folder name

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

Is there a way to remove SPACES and PARENTHESES from filename and put underscore?

For example, I have:

|-- a_dir
|   `-- (1) file with spaces and parentheses.pdf
`-- b_dir
    |-- (43) another file with spaces (1).pdf
    `-- (132) yet another file with spaces (3343).pdf

Must become

|-- a_dir
|   `-- 1_file_with_spaces_and_parentheses.pdf
`-- b_dir
    |-- 43_another_file_with_spaces_1.pdf
    `-- 132_yet_another_file_with_spaces_3343.pdf

Upvotes: 4

Views: 4783

Answers (1)

anubhava
anubhava

Reputation: 785108

You can use:

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

Upvotes: 6

Related Questions