Replace parentheses and spaces in file names with underscores

I am using this line to remove spaces in folder name

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

      

Is there a way to remove SPACES and PARENTHESES from the filename and put an 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

      

Should 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

      

+3


source to share


1 answer


You can use:



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

      

+4


source







All Articles