2014-10-02 143 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