简体   繁体   English

如何在Linux中将所有包含下划线的文件和文件夹重命名为连字符

[英]How to rename all files and folder containing underscore to hyphen in Linux

I want to rename all files and folder containing underscore in name and replace underscore with hyphen. 我想重命名名称中包含下划线的所有文件和文件夹,并用连字符替换下划线。

Currently I am using following code, 目前,我正在使用以下代码,

rename '_' '-' */*/*

It was working but now it is showing me "Argument list too long" 它正在工作,但现在显示的是“参数列表过长”

You can try this: 您可以尝试以下方法:

$ tree foo
foo
├── dir_1
│   └── foo_file_2
└── file_1

1 directory, 2 files
$ for ft in d f; do find foo -type $ft -execdir sh -c 'mv "$0" "${0//_/-}"' {} \; ; done 2>/dev/null
$ tree foo
foo
├── dir-1
│   └── foo-file-2
└── file-1

1 directory, 2 files

This renames all directories and then all files (the for loop over df ) because I haven't been able to make it do all renaming in one iteration. 这将重命名所有目录,然后重命名所有文件( dffor循环),因为我无法使其在一次迭代中全部重命名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM