Linux 用下划线替换文件名中的括号和空格

Linux 用下划线替换文件名中的括号和空格,linux,shell,Linux,Shell,我使用此行删除文件夹名称上的空格 find /tmp/ -depth -name "* *" -execdir rename 's/ /_/g' "{}" \; 有没有办法从文件名中删除空格和括号并加下划线 例如,我有: |-- a_dir | `-- (1) file with spaces and parentheses.pdf `-- b_dir |-- (43) another file with spaces (1).pdf `-- (132) yet anoth

我使用此行删除文件夹名称上的空格

find /tmp/ -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;
有没有办法从文件名中删除空格和括号并加下划线

例如,我有:

|-- 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
必须成为

|-- 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
您可以使用:

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

我想你的意思是在第二个表达式中有结尾部分。这将保留文件中的最后一个字符。由于
重命名
,因此在mac上无法使用具有有趣尾随下划线的扩展;我认为它需要主动安装,至少需要单独安装。我也在OSX上,使用rename这只适用于ubuntu[rename package]()CentOS也有一个rename package,但非常适用于此,因此此命令在CentOS中不起作用这需要
rename
perl实用程序。