为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用bash脚本替换文件名中的空格

如何使用bash脚本替换文件名中的空格

宝慕林4294392 2019-12-07 14:20:26
谁能推荐一种安全的解决方案,从给定的根目录开始,用文件名和目录名中的下划线递归替换空格?例如:$ tree.|-- a dir|   `-- file with spaces.txt`-- b dir    |-- another file with spaces.txt    `-- yet another file with spaces.pdf变成:$ tree.|-- a_dir|   `-- file_with_spaces.txt`-- b_dir    |-- another_file_with_spaces.txt    `-- yet_another_file_with_spaces.pdf
查看完整描述

3 回答

?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

使用rename(aka prename)这是一个Perl脚本,可能已经在您的系统上。分两个步骤进行:


find -name "* *" -type d | rename 's/ /_/g'    # do the directories first

find -name "* *" -type f | rename 's/ /_/g'

基于Jürgen的回答,并且能够使用/usr/bin/rename(Perl脚本)的“ Revision 1.5 1998/12/18 16:16:31 rmb1”版本在一个绑定中处理多层文件和目录:


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


查看完整回答
反对 回复 2019-12-07
  • 3 回答
  • 0 关注
  • 702 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信