Linux 将文件夹中的所有文件重命名为编号列表 1.jpg 2.jpg
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18686832/
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
rename all files in folder to numbered list 1.jpg 2.jpg
提问by Yamaha32088
I have a folder full of images with several different random file names to help organize this mess I would like to, in one command rename all of them to a sequential order so if I have 100 files it starts off naming the first file file-1.jpg
file-2.jpg
etc. Is this possible in one command?
我有一个文件夹,里面有几个不同的随机文件名来帮助组织这个混乱的图像,我想在一个命令中将它们全部重命名为一个顺序顺序,所以如果我有 100 个文件,它会开始命名第一个文件file-1.jpg
file-2.jpg
等。是这可能在一个命令中?
采纳答案by Yamaha32088
I was able to solve my problem by writing a bash script
我能够通过编写 bash 脚本来解决我的问题
#!/bin/sh
num=1
for file in *.jpg; do
mv "$file" "$(printf "%u" $num).jpg"
let num=$num+1
done
回答by Sven Marnach
The most concise command line to do this I can think of is
我能想到的最简洁的命令行是
ls | cat -n | while read n f; do mv "$f" "file-$n.jpg"; done
ls
lists the files in the current directory and cat -n
adds line numbers. The while
loop reads the resulting numbered list of files line by line, stores the line number in the variable n
and the filename in the variable f
and performs the rename.
ls
列出当前目录中的文件并cat -n
添加行号。所述while
环路逐行读取文件线,存储在变量中的行号的所得到的编号的列表n
和在可变的文件名f
并进行重命名。