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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:46:34  来源:igfitidea点击:

rename all files in folder to numbered list 1.jpg 2.jpg

linuxcommand-line

提问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.jpgfile-2.jpgetc. Is this possible in one command?

我有一个文件夹,里面有几个不同的随机文件名来帮助组织这个混乱的图像,我想在一个命令中将它们全部重命名为一个顺序顺序,所以如果我有 100 个文件,它会开始命名第一个文件file-1.jpgfile-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

lslists the files in the current directory and cat -nadds line numbers. The whileloop reads the resulting numbered list of files line by line, stores the line number in the variable nand the filename in the variable fand performs the rename.

ls列出当前目录中的文件并cat -n添加行号。所述while环路逐行读取文件线,存储在变量中的行号的所得到的编号的列表n和在可变的文件名f并进行重命名。