Linux - 替换文件名中的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1806868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:56:31  来源:igfitidea点击:

Linux - Replacing spaces in the file names

linuxreplacecommandfilenamesspaces

提问by Mithun Sreedharan

I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?

我在一个文件夹中有许多文件,我想用下划线替换所有文件名中的每个空格字符。我怎样才能做到这一点?

采纳答案by neesh

This should do it:

这应该这样做:

for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done

回答by Arthur Frankel

I believe your answer is in Replace spaces in filenames with underscores.

我相信你的答案是用下划线替换文件名中的空格

回答by Amir Afghani

Try something like this, assuming all of your files were .txt's:

尝试这样的事情,假设您的所有文件都是 .txt 的:

for files in *.txt; do mv “$files” `echo $files | tr ‘ ‘ ‘_'`; done

回答by Murali VP

If you use bash:

如果您使用 bash:

for file in *; do mv "$file" ${file// /_}; done

回答by DigitalRoss

Use sh...

使用...

for i in *' '*; do   mv "$i" `echo $i | sed -e 's/ /_/g'`; done

If you want to try this out before pulling the trigger just change mvto echo mv.

如果您想在扣动扳机之前尝试一下,只需更改mvecho mv

回答by ghostdog74

Quote your variables:

引用你的变量:

for file in *; do echo mv "'$file'" "${file// /_}"; done

Remove the "echo" to do the actual rename.

删除“回声”以进行实际重命名。

回答by DF.

I prefer to use the command 'rename', which takes Perl-style regexes:

我更喜欢使用命令“重命名”,它采用 Perl 风格的正则表达式:

rename "s/ /_/g" *

You can do a dry run with the -n flag:

您可以使用 -n 标志进行试运行:

rename -n "s/ /_/g" *

回答by javipas

What if you want to apply the replace task recursively? How would you do that?

如果您想递归地应用替换任务怎么办?你会怎么做?

Well, I just found the answer myself. Not the most elegant solution, (also tries to rename files that do not comply with the condition) but it works. (BTW, in my case I needed to rename the files with '%20', not with an underscore)

好吧,我只是自己找到了答案。不是最优雅的解决方案,(也尝试重命名不符合条件的文件)但它有效。(顺便说一句,在我的情况下,我需要用“%20”重命名文件,而不是用下划线)

#!/bin/bash
find . -type d | while read N
do
     (
           cd "$N"
           if test "$?" = "0"
           then
               for file in *; do mv "$file" ${file// /%20}; done
           fi
     )
done

回答by Desta Haileselassie Hagos

The easiest way to replace a string (space character in your case) with another string in Linuxis using sed. You can do it as follows

用另一个字符串替换字符串(在您的情况下为空格字符)的最简单方法Linux是使用sed. 你可以这样做

sed -i 's/\s/_/g' *

Hope this helps.

希望这可以帮助。

回答by akilesh raj

To rename all the files with a .pyextension use, find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"

.py使用扩展名重命名所有文件, find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"

Sample output,

样本输出,

$ find . -iname "*.py" -type f                                                     
./Sample File.py
./Sample/Sample File.py
$ find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
$ find . -iname "*.py" -type f                                                     
./Sample/Sample_File.py
./Sample_File.py