bash 将文件夹中的文件重命名为序列号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3211595/
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
Renaming files in a folder to sequential numbers
提问by Gnutt
I want to rename the files in a directory to sequential numbers. Based on creation date of the files.
我想将目录中的文件重命名为序列号。基于文件的创建日期。
For Example sadf.jpg
to 0001.jpg
, wrjr3.jpg
to 0002.jpg
and so on, the number of leading zeroes depending on the total amount of files (no need for extra zeroes if not needed).
例如sadf.jpg
to 0001.jpg
、wrjr3.jpg
to0002.jpg
等,前导零的数量取决于文件总数(如果不需要,则不需要额外的零)。
回答by gauteh
Try to use a loop, let
, and printf
for the padding:
尝试使用循环, let
, 和printf
作为填充:
a=1
for i in *.jpg; do
new=$(printf "%04d.jpg" "$a") #04 pad to length of 4
mv -i -- "$i" "$new"
let a=a+1
done
using the -i
flag prevents automatically overwriting existing files.
使用该-i
标志可防止自动覆盖现有文件。
回答by gauteh
Beauty in one line:
美在一行:
ls -v | cat -n | while read n f; do mv -n "$f" "$n.ext"; done
You can change .ext
with .png
, .jpg
, etc.
您可以更改.ext
用.png
,.jpg
等等。
回答by Pero
I like gauteh's solution for its simplicity, but it has an important drawback. When running on thousands of files, you can get "argument list too long" message (more on this), and second, the script can get really slow. In my case, running it on roughly 36.000 files, script moved approx. one item per second! I'm not really sure why this happens, but the rule I got from colleagues was "find
is your friend".
我喜欢 gauteh 的解决方案,因为它很简单,但它有一个重要的缺点。当在数千个文件上运行时,您可能会收到“参数列表太长”消息(更多内容),其次,脚本可能会变得非常慢。就我而言,在大约 36.000 个文件上运行它,脚本移动了大约。每秒一项!我不太确定为什么会发生这种情况,但我从同事那里得到的规则是“find
是你的朋友”。
find -name '*.jpg' | # find jpegs
gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", find . -name '*.jpg' \
| awk 'BEGIN{ a=0 }{ printf "mv \"%s\" %04d.jpg\n", rename -N 0001 -X 's/.*/$N/' *.jpg
, a++ }' \
| bash
, a++ }' | # build mv command
bash # run that command
To count items and build command, gawkwas used. Note the main difference, though. By default find
searches for files in current directory and its subdirectories, so be sure to limit the search on current directory only, if necessary (use man find
to see how).
为了计算项目和构建命令,使用了gawk。不过,请注意主要区别。默认情况下find
搜索当前目录及其子目录中的文件,因此请确保仅在必要时限制对当前目录的搜索(用于man find
查看如何)。
回答by beibei2
using Pero's solution on OSX required some modification. I used:
在 OSX 上使用 Pero 的解决方案需要一些修改。我用了:
rename -N 0001 's/.*/$N.jpg/' *.jpg
note: the backslashes are there for line continuation
注意:反斜杠用于续行
edit July 20, 2015:incorporated @klaustopher's feedback to quote the \"%s\"
argument of the mv
command in order to support filenames with spaces.
2015 年 7 月 20 日编辑:合并 @klaustopher 的反馈以引用命令的\"%s\"
参数,mv
以支持带空格的文件名。
回答by Roman Rhrn Nesterov
with "rename" command
使用“重命名”命令
num=0; for i in *; do mv "$i" "$(printf '%04d' $num).${i#*.}"; ((num++)); done
or
或者
find . -name '*.jpg' | gawk 'BEGIN{ a=1 }{ printf "mv \"%s\" %04d.jpg\n", ls -1 -c | xargs rename -n 's/.*/our $i; sprintf("%04d.jpg", $i++)/e'
, a++ }' | bash
回答by Roy Shilkrot
A very simple bash one liner that keeps the original extensions, adds leading zeros, and also works in OSX:
一个非常简单的 bash one liner,它保留了原始扩展名,添加了前导零,并且也适用于 OSX:
ls -1 -c | xargs rename -n 's/.*/our $i; if(!$i) { $i=123; } sprintf("%04d.jpg", $i++)/e'
Simplified version of http://ubuntuforums.org/showthread.php?t=1355021
回答by Abdala Cerqueira
To work in all situations, put a \" for files that have space in the name
要在所有情况下工作,请为名称中有空格的文件添加 \"
exiftool '-FileName<CreateDate' -d %Y%m%d_%H%M%S%%-c.%%e dir
Rename all images in "dir" according to the "CreateDate" date and time, adding a copy number with leading '-' if the file already exists ("%-c"), and
preserving the original file extension (%e). Note the extra '%' necessary to escape the filename codes (%c and %e) in the date format string.
回答by Luke H
If your rename
doesn't support -N
, you can do something like this:
如果您rename
不支持-N
,您可以执行以下操作:
brew install rename
EditTo start with a given number, you can use the (somewhat ugly-looking) code below, just replace 123 with the number you want:
编辑要从给定的数字开始,您可以使用下面的(看起来有些丑陋)代码,只需将 123 替换为您想要的数字:
rename -e 's/.*/$N.jpg/' *.jpg
This lists files in order by creation time (newest first, add -r
to ls to reverse sort), then sends this list of files to rename. Rename uses perl code in the regex to format and increment counter.
这会按创建时间顺序列出文件(最新的,添加-r
到 ls 以进行反向排序),然后发送此文件列表以进行重命名。重命名使用正则表达式中的 perl 代码来格式化和递增计数器。
However, if you're dealing with JPEG images with EXIF information, I'd recommend exiftool
但是,如果您正在处理带有 EXIF 信息的 JPEG 图像,我建议您 exiftool
This is from the exiftool documentation, under "Renaming Examples"
这是来自exiftool 文档,在“重命名示例”下
rename -e 's/.*/photo-$N.jpg/' *.jpg
回答by trisweb
On OSX, install the rename script from Homebrew:
在 OSX 上,从 Homebrew 安装重命名脚本:
rename --counter-format 000001 --lower-case --keep-extension --expr='$_ = "$N" if @EXT' *
Then you can do it really ridiculously easily:
然后你可以很容易地做到这一点:
##代码##Or to add a nice prefix:
或者添加一个不错的前缀:
##代码##回答by OzzyCzech
Follow command rename all files to sequence and also lowercase extension:
按照命令将所有文件重命名为序列以及小写扩展名:
##代码##