加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – Bash以递归方式替换名称上的许多空格

发布时间:2020-12-14 00:34:46 所属栏目:Linux 来源:网络整理
导读:任何人都可以推荐一个安全的解决方案,从给定的根目录开始递归替换文件和目录名称中带下划线的空格?例如, $tree.|-- a dir| `-- file with spaces.txt`-- b dir |-- another file with spaces.txt `-- yet another file with spaces.pdf 变为: $tree.|-- a_
任何人都可以推荐一个安全的解决方案,从给定的根目录开始递归替换文件和目录名称中带下划线的空格?例如,

$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

我已经复制了另一个用户的问题,这是主要问题,但我需要添加另一个问题:

我正在使用以下解决方案:

$find -depth -name '* *' -execdir rename " " "_" {} +;

它工作,但只替换项目(目录或文件)上找到的第一个空格.有关如何制作循环以寻找空间并在它们全部消失时停止的任何想法?

解决方法

如果您没有可用的重命名,或者您不确定您拥有哪个版本,则可以通过以下方式实现:

find . -depth -name '* *' -execdir bash -c 'for i; do mv "$i" "${i// /_}"; done' _ {} +

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读