将文件夹中的文件重命名为连续编号

发布于 2024-09-09 03:09:03 字数 173 浏览 9 评论 0原文

我想将目录中的文件重命名为连续编号。基于文件的创建日期。

例如 sadf.jpg0001.jpgwrjr3.jpg0002.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(29

家住魔仙堡 2024-09-16 03:09:04

使用“重命名”命令

rename -N 0001 -X 's/.*/$N/' *.jpg

rename -N 0001 's/.*/$N.jpg/' *.jpg

with "rename" command

rename -N 0001 -X 's/.*/$N/' *.jpg

or

rename -N 0001 's/.*/$N.jpg/' *.jpg
眼眸里的快感 2024-09-16 03:09:04

在 OSX 上使用 Pero 的解决方案需要进行一些修改。我使用:

find . -name '*.jpg' \
| awk 'BEGIN{ a=0 }{ printf "mv \"%s\" %04d.jpg\n", $0, a++ }' \
| bash

注意:反斜杠用于行继续

2015 年 7 月 20 日编辑:
合并了 @klaustopher 的反馈来引用 mv 命令的 \"%s\" 参数,以支持带有空格的文件名。

using Pero's solution on OSX required some modification. I used:

find . -name '*.jpg' \
| awk 'BEGIN{ a=0 }{ printf "mv \"%s\" %04d.jpg\n", $0, a++ }' \
| bash

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.

遥远的绿洲 2024-09-16 03:09:04

要在所有情况下工作,请为名称中包含空格的文件添加 \"

find . -name '*.jpg' | gawk 'BEGIN{ a=1 }{ printf "mv \"%s\" %04d.jpg\n", $0, a++ }' | bash

To work in all situations, put a \" for files that have space in the name

find . -name '*.jpg' | gawk 'BEGIN{ a=1 }{ printf "mv \"%s\" %04d.jpg\n", $0, a++ }' | bash
幸福%小乖 2024-09-16 03:09:04

在 OSX 上,从 Homebrew 安装重命名脚本:

brew install rename

然后你就可以非常轻松地做到这一点:

rename -e 's/.*/$N.jpg/' *.jpg

或者添加一个漂亮的前缀:

rename -e 's/.*/photo-$N.jpg/' *.jpg

On OSX, install the rename script from Homebrew:

brew install rename

Then you can do it really ridiculously easily:

rename -e 's/.*/$N.jpg/' *.jpg

Or to add a nice prefix:

rename -e 's/.*/photo-$N.jpg/' *.jpg
分分钟 2024-09-16 03:09:04

注意 此处的重命名命令包括用于预览重命名的 -n。要实际执行重命名,请删除 -n

如果您的 rename 不支持 -N,您可以执行类似的操作this:

ls -1 --color=never -c | xargs rename -n 's/.*/our $i; sprintf("%04d.jpg", $i++)/e'

注意 此处的重命名命令包括用于预览重命名的 -n 。要实际执行重命名,请删除 -n

编辑 要从给定的数字开始,您可以使用下面的(看起来有点难看)代码,只需将 123 替换为您想要的数字:

ls -1 --color=never  -c | xargs rename -n 's/.*/our $i; if(!$i) { $i=123; } sprintf("%04d.jpg", $i++)/e'

这将按创建时间顺序列出文件(最新的在前,将 -r 添加到 ls 进行反向排序),然后发送此文件列表进行重命名。重命名使用正则表达式中的 perl 代码来格式化和递增计数器。

但是,如果您要处理带有 EXIF 信息的 JPEG 图像,我建议您使用 exiftool

这是来自 exiftool 文档,位于“重命名示例”下

   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.

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:

ls -1 --color=never -c | xargs rename -n 's/.*/our $i; sprintf("%04d.jpg", $i++)/e'

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:

ls -1 --color=never  -c | xargs rename -n 's/.*/our $i; if(!$i) { $i=123; } sprintf("%04d.jpg", $i++)/e'

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"

   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.
青萝楚歌 2024-09-16 03:09:04
find .  | grep 'avi' | nl -nrz -w3 -v1 | while read n f; do mv "$f" "$n.avi"; done

find . 将显示文件夹和子文件夹中的所有文件。

grep 'avi' 将过滤所有带有 avi 扩展名的文件。

nl -nrz -w3 -v1 将显示以 001 002 等开头的序列号,后跟文件名。

读取 nf 时;做 mv "$f" "$n.avi"; done 会将文件名更改为序列号。

find .  | grep 'avi' | nl -nrz -w3 -v1 | while read n f; do 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.

沒落の蓅哖 2024-09-16 03:09:04

按照命令将所有文件重命名为序列并小写扩展名:

rename --counter-format 000001 --lower-case --keep-extension --expr='$_ = "$N" if @EXT' *

Follow command rename all files to sequence and also lowercase extension:

rename --counter-format 000001 --lower-case --keep-extension --expr='$_ = "$N" if @EXT' *
寂寞美少年 2024-09-16 03:09:04

假设我们将这些文件放在一个目录中,并按创建顺序列出,第一个是最旧的:

a.jpg
b.JPG
c.jpeg
d.tar.gz
e

然后 ls -1cr 输出的正是上面的列表。然后,您可以使用 rename:

ls -1cr | xargs rename -n 's/^[^\.]*(\..*)?$/our $i; sprintf("%03d$1", $i++)/e'

输出

rename(a.jpg, 000.jpg)
rename(b.JPG, 001.JPG)
rename(c.jpeg, 002.jpeg)
rename(d.tar.gz, 003.tar.gz)
Use of uninitialized value $1 in concatenation (.) or string at (eval 4) line 1.
rename(e, 004)

对于没有扩展名的文件,将显示警告“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:

a.jpg
b.JPG
c.jpeg
d.tar.gz
e

then ls -1cr outputs exactly the list above. You can then use rename:

ls -1cr | xargs rename -n 's/^[^\.]*(\..*)?$/our $i; sprintf("%03d$1", $i++)/e'

which outputs

rename(a.jpg, 000.jpg)
rename(b.JPG, 001.JPG)
rename(c.jpeg, 002.jpeg)
rename(d.tar.gz, 003.tar.gz)
Use of uninitialized value $1 in concatenation (.) or string at (eval 4) line 1.
rename(e, 004)

The warning ”use of uninitialized value […]” is displayed for files without an extension; you can ignore it.

Remove -n from the rename 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.

ぃ弥猫深巷。 2024-09-16 03:09:04

我花了 3-4 个小时开发这个解决方案,写了一篇相关文章:
https:// /www.cloudsavvyit.com/8254/how-to-bulk-rename-files-to-numeric-file-names-in-linux/

if [ ! -r _e -a ! -r _c ]; then echo 'pdf' > _e; echo 1 > _c ;find . -name "*.$(cat _e)" -print0 | xargs -0 -t -I{} bash -c 'mv -n "{}" $(cat _c).$(cat _e);echo $[ $(cat _c) + 1 ] > _c'; rm -f _e _c; fi

这适用于任何类型的文件名(空格、特殊字符)通过 findxargs 纠正 \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/

if [ ! -r _e -a ! -r _c ]; then echo 'pdf' > _e; echo 1 > _c ;find . -name "*.$(cat _e)" -print0 | xargs -0 -t -I{} bash -c 'mv -n "{}" $(cat _c).$(cat _e);echo $[ $(cat _c) + 1 ] > _c'; rm -f _e _c; fi

This works for any type of filename (spaces, special chars) by using correct \0 escaping by both find and xargs, and you can set a start file naming offset by increasing echo 1 to any other number if you like.

Set extension at start (pdf in example here). It will also not overwrite any existing files.

音盲 2024-09-16 03:09:04

与创建日期无关,但根据排序名称进行编号:

python3 -c \
'ext="jpg"
start_num=0
pad=4
import os,glob
files=glob.glob(f"*.{ext}")
files.sort()
renames=list(zip(files,range(start_num,len(files)+start_num)))
for r in renames:
  oname=r[0]
  nname=f"{r[1]:0{pad}}.{ext}"
  print(oname,"->",nname)
  os.rename(oname,nname)
'

Not related to creation date but numbered based on sorted names:

python3 -c \
'ext="jpg"
start_num=0
pad=4
import os,glob
files=glob.glob(f"*.{ext}")
files.sort()
renames=list(zip(files,range(start_num,len(files)+start_num)))
for r in renames:
  oname=r[0]
  nname=f"{r[1]:0{pad}}.{ext}"
  print(oname,"->",nname)
  os.rename(oname,nname)
'
暮年 2024-09-16 03:09:04

我遇到了类似的问题,因此编写了一个 shell 脚本。尽管已经发布了许多好的答案,我还是决定发布它,因为我认为这对某人有帮助。请随意改进它!

numerate

@Gnutt 您想要的行为可以通过键入以下内容来实现:

./numerate.sh -d <path to directory> -o modtime -L 4 -b <startnumber> -r

如果省略选项-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:

./numerate.sh -d <path to directory> -o modtime -L 4 -b <startnumber> -r

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.

纸短情长 2024-09-16 03:09:04
a=1

for i in *.jpg; do
 mv -- "$i" "$a.jpg"
 a=`expr $a + 1`
done
a=1

for i in *.jpg; do
 mv -- "$i" "$a.jpg"
 a=`expr $a + 1`
done
蹲在坟头点根烟 2024-09-16 03:09:04

再次使用 Pero 的解决方案,几乎不需要修改,因为 find 将按照项目存储在目录条目中的顺序遍历目录树。这(大部分)在同一台机器上的运行之间保持一致,并且如果没有删除,则本质上是“文件/目录创建顺序”。

但是,在某些情况下,您需要获取一些逻辑顺序,例如按名称,这在本示例中使用。

find -name '*.jpg' | sort -n | # find jpegs
gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | # build mv command
bash # run that command 

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.

find -name '*.jpg' | sort -n | # find jpegs
gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | # build mv command
bash # run that command 
鱼窥荷 2024-09-16 03:09:04

大多数其他解决方案将覆盖已命名为数字的现有文件。如果运行脚本、添加更多文件,然后再次运行脚本,这尤其是一个问题。

该脚本首先重命名现有的数字文件:

#!/usr/bin/perl

use strict;
use warnings;

use File::Temp qw/tempfile/;

my $dir = $ARGV[0]
    or die "Please specify directory as first argument";

opendir(my $dh, $dir) or die "can't opendir $dir: $!";

# First rename any files that are already numeric
while (my @files = grep { /^[0-9]+(\..*)?$/ } readdir($dh))
{
    for my $old (@files) {
        my $ext = $old =~ /(\.[^.]+)$/ ? $1 : '';
        my ($fh, $new) = tempfile(DIR => $dir, SUFFIX => $ext);
        close $fh;
        rename "$dir/$old", $new;
    }
}

rewinddir $dh;
my $i;
while (my $file = readdir($dh))
{
    next if $file =~ /\A\.\.?\z/;
    my $ext = $file =~ /(\.[^.]+)$/ ? $1 : '';
    rename "$dir/$file", sprintf("%s/%04d%s", $dir, ++$i, $ext); 
}

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:

#!/usr/bin/perl

use strict;
use warnings;

use File::Temp qw/tempfile/;

my $dir = $ARGV[0]
    or die "Please specify directory as first argument";

opendir(my $dh, $dir) or die "can't opendir $dir: $!";

# First rename any files that are already numeric
while (my @files = grep { /^[0-9]+(\..*)?$/ } readdir($dh))
{
    for my $old (@files) {
        my $ext = $old =~ /(\.[^.]+)$/ ? $1 : '';
        my ($fh, $new) = tempfile(DIR => $dir, SUFFIX => $ext);
        close $fh;
        rename "$dir/$old", $new;
    }
}

rewinddir $dh;
my $i;
while (my $file = readdir($dh))
{
    next if $file =~ /\A\.\.?\z/;
    my $ext = $file =~ /(\.[^.]+)$/ ? $1 : '';
    rename "$dir/$file", sprintf("%s/%04d%s", $dir, ++$i, $ext); 
}
那些过往 2024-09-16 03:09:04

按时间排序,仅限 jpg、前导零和基本名称(如果您可能需要一个):(

ls -t *.jpg | cat -n |                                           \
while read n f; do mv "$f" "$(printf thumb_%04d.jpg $n)"; done

全部在一行上,没有 \

Sorted by time, limited to jpg, leading zeroes and a basename (in case you likely want one):

ls -t *.jpg | cat -n |                                           \
while read n f; do mv "$f" "$(printf thumb_%04d.jpg $n)"; done

(all on one line, without the \)

因为看清所以看轻 2024-09-16 03:09:04

以下内容对我有用。

count=1 && for file in *.png; do [ -e "$file" ] && mv "$file" "$count.png" && count=$((count + 1)); done

The following worked for me.

count=1 && for file in *.png; do [ -e "$file" ] && mv "$file" "$count.png" && count=$((count + 1)); done
轻许诺言 2024-09-16 03:09:04

该脚本将在 Mac OS bash 上按创建日期对文件进行排序。我用它来批量重命名视频。只需更改扩展名和名称的第一部分即可。

ls -trU *.mp4| awk 'BEGIN{ a=0 }{ printf "mv %s lecture_%03d.mp4\n", $0, a++ }' | 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.

ls -trU *.mp4| awk 'BEGIN{ a=0 }{ printf "mv %s lecture_%03d.mp4\n", $0, a++ }' | bash
握住我的手 2024-09-16 03:09:04
ls -1tr | rename -vn 's/.*/our $i;if(!$i){$i=1;} sprintf("%04d.jpg", $i++)/e'

重命名 -vn - 删除 n 以关闭测试模式

{$i=1;} - 控制起始编号

"%04d.jpg " - 控制计数为零04并设置输出扩展名.jpg

ls -1tr | rename -vn 's/.*/our $i;if(!$i){$i=1;} sprintf("%04d.jpg", $i++)/e'

rename -vn - remove n for off test mode

{$i=1;} - control start number

"%04d.jpg" - control count zero 04 and set output extension .jpg

熟人话多 2024-09-16 03:09:04

对我来说,这个答案组合完美地工作:

ls -v | gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | bash
  • 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 | gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | bash
  • ls -v helps with ordering 1 10 9 in correct: 1 9 10 order, avoiding filename extension problems with jpg JPG jpeg
  • gawk '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 executes

Be 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.

懒的傷心 2024-09-16 03:09:04

这对我有用。
我使用了 rename 命令,这样如果任何文件的名称中包含空格,mv 命令就不会混淆空格和实际文件。

Here i replaced spaces , ' ' in a file name with '_' for all jpg files
#! /bin/bash
rename 'y/ /_/' *jpg         #replacing spaces with _
let x=0;
for i in *.jpg;do
    let x=(x+1)
    mv $i $x.jpg
done

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.

Here i replaced spaces , ' ' in a file name with '_' for all jpg files
#! /bin/bash
rename 'y/ /_/' *jpg         #replacing spaces with _
let x=0;
for i in *.jpg;do
    let x=(x+1)
    mv $i $x.jpg
done
深白境迁sunset 2024-09-16 03:09:04

使用 sed :

ls -tr | sed "s/(.*)/mv '\1' \=printf('%04s',line('.').jpg)/" > rename.sh
bash rename.sh

这样您可以在执行脚本之前检查脚本以避免出现大错误

Using sed :

ls -tr | sed "s/(.*)/mv '\1' \=printf('%04s',line('.').jpg)/" > rename.sh
bash rename.sh

This way you can check the script before executing it to avoid big mistakes

三生路 2024-09-16 03:09:04

这是使用“重命名”命令的另一个解决方案:

find -name 'access.log.*.gz' | sort -Vr | rename 's/(\d+)/$1+1/ge'

Here a another solution with "rename" command:

find -name 'access.log.*.gz' | sort -Vr | rename 's/(\d+)/$1+1/ge'
来世叙缘 2024-09-16 03:09:04

如今,选择多个文件进行重命名后有一个选项(我在 2020 年的 thunar 文件管理器中看到并使用 XFCE Ubuntu)。

  1. 选择多个文件
  2. 检查选项
  3. 选择重命名

该特定目录中的所有文件都会出现提示
只需检查类别部分

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).

  1. select multiple files
  2. check options
  3. select rename

A prompt comes with all files in that particular dir
just check with the category section

所谓喜欢 2024-09-16 03:09:04

Pero 的回答让我想到这里:)

我想相对于时间重命名文件,因为图像查看器没有按时间顺序显示图像。

ls -tr *.jpg | # list jpegs relative to time
gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | # build mv command
bash # run that command

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.

ls -tr *.jpg | # list jpegs relative to time
gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | # build mv command
bash # run that command
携余温的黄昏 2024-09-16 03:09:04

要对一个文件夹中的 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.

薯片软お妹 2024-09-16 03:09:03

一行之美:

ls -v | cat -n | while read n f; do mv -n "$f" "$n.ext"; done 

您可以将 .ext 更改为 .png.jpg 等。

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.

牵你的手,一向走下去 2024-09-16 03:09:03

尝试使用循环、letprintf 进行填充:

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

使用 -i 标志可防止自动覆盖现有文件,并使用 >-- 防止 mv 将带有破折号的文件名解释为选项。

Try to use a loop, let, and printf for the padding:

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, and using -- prevents mv from interpreting filenames with dashes as options.

好听的两个字的网名 2024-09-16 03:09:03

我喜欢 gaueth 的解决方案,因为它很简单,但它有一个重要的缺点。当运行数千个文件时,您可能会收到“参数列表太长”消息(更多信息 ),其次,脚本可能会变得非常慢。就我而言,在大约 36.000 个文件上运行它,脚本移动了大约。每秒一个项目!我不太确定为什么会发生这种情况,但我从同事那里得到的规则是“find是你的朋友”。

find -name '*.jpg' | # find jpegs
gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | # build mv command
bash # run that command

为了计算项目和构建命令,使用了 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".

find -name '*.jpg' | # find jpegs
gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }' | # build mv command
bash # run that command

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 (use man find to see how).

疏忽 2024-09-16 03:09:03

一个非常简单的 bash one liner,保留原始扩展名,添加前导零,并且也适用于 OSX:

num=0; for i in *; do mv "$i" "$(printf '%04d' $num).${i#*.}"; ((num++)); done

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:

num=0; for i in *; do mv "$i" "$(printf '%04d' $num).${i#*.}"; ((num++)); done

Simplified version of http://ubuntuforums.org/showthread.php?t=1355021

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文