bash 用递增的数字重命名文件夹中的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8618853/
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-09-18 01:12:50  来源:igfitidea点击:

Rename all the files in the folder with increasing numbers

linuxbashshellterminal

提问by Lukap

I have a folder and inside that folder I have 10-15 files with arbitrary names. The filenames may include spaces in them. For example: wWw.page.com __ (576)_002. In a terminal, when I press wand then tabthe file name appears like this: wWw.page.com\ \ __\ \(576\)_0.txt.

我有一个文件夹,在该文件夹中我有 10-15 个任意名称的文件。文件名中可能包含空格。例如:wWw.page.com __ (576)_002。在终端中,当我按下w然后tab文件名显示如下:wWw.page.com\ \ __\ \(576\)_0.txt.

I want some script that will rename all my files like this 0.txt, 1.txt, 2.txtand so on.

我想一些脚本,将我所有的文件重命名这样0.txt1.txt2.txt等等。

My problem is: wWw.page.com __ (576)_002.txt file not found.

我的问题是:wWw.page.com __ (576)_002.txt file not found

index=0;
for i in $(ls *.txt)
do
    cp "${i}"  $index".txt" 
done

回答by cnicutar

Instead of lstry to glob:

而不是ls尝试glob

index=0;
for name in *.txt
do
    cp "${name}" "${index}.txt"
    index=$((index+1))
done