将文件夹中的文件重命名为连续编号
我想将目录中的文件重命名为连续编号。基于文件的创建日期。
例如 sadf.jpg
到 0001.jpg
、wrjr3.jpg
到 0002.jpg
等等,前导零的数量取决于文件的总量(如果不需要,则无需额外的零)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(29)
使用“重命名”命令
或
with "rename" command
or
在 OSX 上使用 Pero 的解决方案需要进行一些修改。我使用:
注意:反斜杠用于行继续
2015 年 7 月 20 日编辑:
合并了 @klaustopher 的反馈来引用
mv
命令的\"%s\"
参数,以支持带有空格的文件名。using Pero's solution on OSX required some modification. I used:
note: the backslashes are there for line continuation
edit July 20, 2015:
incorporated @klaustopher's feedback to quote the
\"%s\"
argument of themv
command in order to support filenames with spaces.要在所有情况下工作,请为名称中包含空格的文件添加 \"
To work in all situations, put a \" for files that have space in the name
在 OSX 上,从 Homebrew 安装重命名脚本:
然后你就可以非常轻松地做到这一点:
或者添加一个漂亮的前缀:
On OSX, install the rename script from Homebrew:
Then you can do it really ridiculously easily:
Or to add a nice prefix:
注意 此处的重命名命令包括用于预览重命名的
-n
。要实际执行重命名,请删除-n
如果您的
rename
不支持-N
,您可以执行类似的操作this:注意 此处的重命名命令包括用于预览重命名的
-n
。要实际执行重命名,请删除-n
编辑 要从给定的数字开始,您可以使用下面的(看起来有点难看)代码,只需将 123 替换为您想要的数字:
这将按创建时间顺序列出文件(最新的在前,将
-r
添加到 ls 进行反向排序),然后发送此文件列表进行重命名。重命名使用正则表达式中的 perl 代码来格式化和递增计数器。但是,如果您要处理带有 EXIF 信息的 JPEG 图像,我建议您使用
exiftool
这是来自 exiftool 文档,位于“重命名示例”下
NOTE The rename commands here include
-n
which previews the rename. To actually perform the renaming, remove the-n
If your
rename
doesn't support-N
, you can do something like this:NOTE The rename commands here includes
-n
which previews the rename. To actually perform the renaming, remove the-n
Edit To start with a given number, you can use the (somewhat ugly-looking) code below, just replace 123 with the number you want:
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.However, if you're dealing with JPEG images with EXIF information, I'd recommend
exiftool
This is from the exiftool documentation, under "Renaming Examples"
find .
将显示文件夹和子文件夹中的所有文件。grep 'avi'
将过滤所有带有 avi 扩展名的文件。nl -nrz -w3 -v1
将显示以 001 002 等开头的序列号,后跟文件名。读取 nf 时;做 mv "$f" "$n.avi"; done
会将文件名更改为序列号。find .
will display all file in folder and subfolders.grep 'avi'
will filter all files with avi extension.nl -nrz -w3 -v1
will display sequence number starting 001 002 etc following by file name.while read n f; do mv "$f" "$n.avi"; done
will change file name to sequence numbers.按照命令将所有文件重命名为序列并小写扩展名:
Follow command rename all files to sequence and also lowercase extension:
假设我们将这些文件放在一个目录中,并按创建顺序列出,第一个是最旧的:
然后 ls -1cr 输出的正是上面的列表。然后,您可以使用
rename
:输出
对于没有扩展名的文件,将显示警告“use of uninitialized value [...]”;你可以忽略它。
从
rename
命令中删除-n
以实际应用重命名。这个答案的灵感来自Luke 在 2014 年 4 月的回答。它忽略了 Gnutt 根据文件总量设置前导零数量的要求。
Let us assume we have these files in a directory, listed in order of creation, the first being the oldest:
then
ls -1cr
outputs exactly the list above. You can then userename
:which outputs
The warning ”use of uninitialized value […]” is displayed for files without an extension; you can ignore it.
Remove
-n
from therename
command to actually apply the renaming.This answer is inspired by Luke’s answer of April 2014. It ignores Gnutt’s requirement of setting the number of leading zeroes depending on the total amount of files.
我花了 3-4 个小时开发这个解决方案,写了一篇相关文章:
https:// /www.cloudsavvyit.com/8254/how-to-bulk-rename-files-to-numeric-file-names-in-linux/
这适用于任何类型的文件名(空格、特殊字符)通过
find
和xargs
纠正\0
转义,并且您可以通过增加echo 1
来设置起始文件命名偏移量> 如果您愿意,可以拨打任何其他号码。在开始处设置扩展名(此处示例中的
pdf
)。它也不会覆盖任何现有文件。I spent 3-4 hours developing this solution for an article on this:
https://www.cloudsavvyit.com/8254/how-to-bulk-rename-files-to-numeric-file-names-in-linux/
This works for any type of filename (spaces, special chars) by using correct
\0
escaping by bothfind
andxargs
, and you can set a start file naming offset by increasingecho 1
to any other number if you like.Set extension at start (
pdf
in example here). It will also not overwrite any existing files.与创建日期无关,但根据排序名称进行编号:
Not related to creation date but numbered based on sorted names:
我遇到了类似的问题,因此编写了一个 shell 脚本。尽管已经发布了许多好的答案,我还是决定发布它,因为我认为这对某人有帮助。请随意改进它!
numerate
@Gnutt 您想要的行为可以通过键入以下内容来实现:
如果省略选项
-r
,则仅模拟铰孔(应该对测试有帮助)。选项 L 描述目标数字的长度(将用前导零填充)
还可以使用选项
-p
-s
添加前缀/后缀。如果有人希望在对文件进行编号之前对文件进行数字排序,只需删除
-o modtime
选项即可。I had a similar issue and wrote a shell script for that reason. I've decided to post it regardless that many good answers were already posted because I think it can be helpful for someone. Feel free to improve it!
numerate
@Gnutt The behavior you want can be achieved by typing the following:
If the option
-r
is left out the reaming will be only simulated (Should be helpful for testing).The otion L describes the length of the target number (which will be filled with leading zeros)
it is also possible to add a prefix/suffix with the options
-p <prefix>
-s <suffix>
.In case somebody wants the files to be sorted numerically before they get numbered, just remove the
-o modtime
option.再次使用 Pero 的解决方案,几乎不需要修改,因为
find
将按照项目存储在目录条目中的顺序遍历目录树。这(大部分)在同一台机器上的运行之间保持一致,并且如果没有删除,则本质上是“文件/目录创建顺序”。但是,在某些情况下,您需要获取一些逻辑顺序,例如按名称,这在本示例中使用。
Again using Pero's solution with little modifying, because
find
will be traversing the directory tree in the order items are stored within the directory entries. This will (mostly) be consistent from run to run, on the same machine and will essentially be "file/directory creation order" if there have been no deletes.However, in some case you need to get some logical order, say, by name, which is used in this example.
大多数其他解决方案将覆盖已命名为数字的现有文件。如果运行脚本、添加更多文件,然后再次运行脚本,这尤其是一个问题。
该脚本首先重命名现有的数字文件:
The majority of the other solutions will overwrite existing files already named as a number. This is particularly a problem if running the script, adding more files, and then running the script again.
This script renames existing numerical files first:
按时间排序,仅限 jpg、前导零和基本名称(如果您可能需要一个):(
全部在一行上,没有
\
)Sorted by time, limited to jpg, leading zeroes and a basename (in case you likely want one):
(all on one line, without the
\
)以下内容对我有用。
The following worked for me.
该脚本将在 Mac OS bash 上按创建日期对文件进行排序。我用它来批量重命名视频。只需更改扩展名和名称的第一部分即可。
This script will sort the files by creation date on Mac OS bash. I use it to mass rename videos. Just change the extension and the first part of the name.
重命名 -vn - 删除 n 以关闭测试模式
{$i=1;} - 控制起始编号
"%04d.jpg " - 控制计数为零04并设置输出扩展名.jpg
rename -vn - remove n for off test mode
{$i=1;} - control start number
"%04d.jpg" - control count zero 04 and set output extension .jpg
对我来说,这个答案组合完美地工作:
ls -v
有助于以正确的顺序排序 1 10 9:1 9 10 顺序,避免 jpg JPG jpeg 的文件名扩展问题gawk 'BEGIN{ a= 1 }{ printf "mv %s %04d.jpg\n", $0, a++ }'
用 4 个字符和前导零重新编号。通过避免 mv,我不会意外地尝试通过意外地拥有相同的数字来覆盖已经存在的任何内容。bash
执行请注意 @xhienne 所说的,将未知内容通过管道传输到 bash 存在安全风险。但对我来说情况并非如此,因为我使用的是扫描的照片。
To me this combination of answers worked perfectly:
ls -v
helps with ordering 1 10 9 in correct: 1 9 10 order, avoiding filename extension problems with jpg JPG jpeggawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }'
renumbers with 4 characters and leading zeros. By avoiding mv I do not accidentally try to overwrite anything that is there already by accidentally having the same number.bash
executesBe aware of what @xhienne said, piping unknown content to bash is a security risk. But this was not the case for me as I was using my scanned photos.
这对我有用。
我使用了 rename 命令,这样如果任何文件的名称中包含空格,mv 命令就不会混淆空格和实际文件。
Here is what worked for me.
I Have used rename command so that if any file contains spaces in name of it then , mv command dont get confused between spaces and actual file.
使用 sed :
这样您可以在执行脚本之前检查脚本以避免出现大错误
Using sed :
This way you can check the script before executing it to avoid big mistakes
这是使用“重命名”命令的另一个解决方案:
Here a another solution with "rename" command:
如今,选择多个文件进行重命名后有一个选项(我在 2020 年的 thunar 文件管理器中看到并使用 XFCE Ubuntu)。
该特定目录中的所有文件都会出现提示
只需检查类别部分
Nowadays there is an option after you select multiple files for renaming (I have seen in thunar file manager as of in Year 2020 and using XFCE Ubuntu).
A prompt comes with all files in that particular dir
just check with the category section
Pero 的回答让我想到这里:)
我想相对于时间重命名文件,因为图像查看器没有按时间顺序显示图像。
Pero's answer got me here :)
I wanted to rename files relative to time as the image viewers did not display images in time order.
要对一个文件夹中的 6000 个文件重新编号,您可以使用 ACDsee 程序的“重命名”选项。
要定义前缀,请使用以下格式:
####"*"
然后设置起始编号并按重命名,程序将使用连续编号重命名所有 6000 个文件。
To renumber 6000, files in one folder you could use the 'Rename' option of the ACDsee program.
For defining a prefix use this format:
####"*"
Then set the start number and press Rename and the program will rename all 6000 files with sequential numbers.
一行之美:
您可以将
.ext
更改为.png
、.jpg
等。Beauty in one line:
You can change
.ext
with.png
,.jpg
, etc.尝试使用循环、
let
和printf
进行填充:使用
-i
标志可防止自动覆盖现有文件,并使用>--
防止mv
将带有破折号的文件名解释为选项。Try to use a loop,
let
, andprintf
for the padding:using the
-i
flag prevents automatically overwriting existing files, and using--
preventsmv
from interpreting filenames with dashes as options.我喜欢 gaueth 的解决方案,因为它很简单,但它有一个重要的缺点。当运行数千个文件时,您可能会收到“参数列表太长”消息(更多信息 ),其次,脚本可能会变得非常慢。就我而言,在大约 36.000 个文件上运行它,脚本移动了大约。每秒一个项目!我不太确定为什么会发生这种情况,但我从同事那里得到的规则是“
find
是你的朋友”。为了计算项目和构建命令,使用了 gawk。但请注意主要区别。默认情况下,
find
搜索当前目录及其子目录中的文件,因此如有必要,请务必将搜索限制在当前目录(使用man find
查看操作方法)。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".To count items and build command, gawk was 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 (useman find
to see how).一个非常简单的 bash one liner,保留原始扩展名,添加前导零,并且也适用于 OSX:
http:// /ubuntuforums.org/showthread.php?t=1355021
A very simple bash one liner that keeps the original extensions, adds leading zeros, and also works in OSX:
Simplified version of http://ubuntuforums.org/showthread.php?t=1355021