【问题标题】:Replace white space with underscore and make lower case - file name用下划线替换空格并小写 - 文件名
【发布时间】:2014-11-30 05:06:19
【问题描述】:

我正在重命名文件和目录。基本上,我要做的就是去掉空格并用下划线替换它们,最后变成小写。我可以一次执行一个命令:$ rename "s/ /_/g" *,然后是小写命令。但我正试图在一条线上完成这一切。下面我能做到的就是去掉空格并用_替换,但它不会变成小写。怎么会?

find /temp/ -depth -name "* *" -execdir rename 's/ /_/g; s,,?; ‘

原文件名:

test FILE   .txt

结果:(如果末尾有空格,取出)

test_file.txt

【问题讨论】:

  • 如果您想将所有 ' ' 翻译成 '_' 并将所有字母翻译成对应的小写字母,那么 rename -n 'y/A-Z /a-z_/' 似乎适合我。

标签: linux bash ubuntu


【解决方案1】:
rename 's/ +\././; y/A-Z /a-z_/'

或者,结合find

find /temp/ -depth -name "* *" -exec rename 's/ +\././; y/A-Z /a-z_/' {} +

要仅定位文件而不是目录,请添加 -type f:

find /temp/ -depth -name "* *" -type f -exec rename 's/ +\././; y/A-Z /a-z_/' {} +

缩短名称

是否可以用最后三个字符重命名文件 例如从 big Dog.txt 到 dog.txt 的原始文件?

是的。使用这个rename 命令:

rename 's/ +\././; y/A-Z /a-z_/; s/[^.]*([^.]{3})/$1/'

【讨论】:

【解决方案2】:

试试这行:
echo "test FILE .txt" | tr A-Z a-z | tr -s ' ' | tr ' ' '<em>'| sed 's/</em>././'

【讨论】:

    猜你喜欢
    • 2018-11-25
    • 1970-01-01
    • 2011-07-12
    • 2014-11-27
    • 2020-05-27
    • 2013-01-01
    • 2020-02-03
    • 1970-01-01
    相关资源
    最近更新 更多