2014-10-02 142 views
3

替换括号和空格我用这条线来对文件夹的名称在文件名中带有下划线

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 

回答

4

您可以使用:

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

我想你的意思是在第二个表达式闭幕括号。这将文件保留在之前的最后一个字符。从一个有趣的尾随下划线延伸 – 2014-10-02 21:21:25

+1

TKS,工作得很好。由于'rename', – 2014-10-02 21:29:21

+0

在mac上不起作用;我认为它需要积极安装,至少是 – 2017-06-16 03:35:57