【问题标题】:Replace parentheses and spaces in filenames with underscore用下划线替换文件名中的括号和空格
【发布时间】:2014-11-27 23:05:51
【问题描述】:

我使用这一行来删除文件夹名称上的空格

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

【问题讨论】:

    标签: linux shell


    【解决方案1】:

    你可以使用:

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

    【讨论】:

    • 我认为您的意思是在第二个表达式中包含右括号。这将文件保留在 . 之前的最后一个字符。从有一个有趣的尾随下划线扩展
    • 因为rename而不能在mac上工作;我认为至少需要主动安装它
    • 是的,需要单独安装。我也在 OSX 上并使用重命名
    • 这仅适用于 ubuntu [rename package] (salsa.debian.org/perl-team/modules/packages/rename) CentOS 也有一个 rename 包但非常适合这个,因此这个命令在 CentOS 中不起作用
    • 这需要rename perl 实用程序。
    猜你喜欢
    • 2018-11-25
    • 2018-01-28
    • 2014-11-30
    • 2013-01-01
    • 2021-01-10
    • 2018-06-11
    • 1970-01-01
    • 2011-07-12
    相关资源
    最近更新 更多