简体   繁体   English

如何在终端(LINUX)中重命名多个文件?

[英]How to rename multiple files in terminal (LINUX)?

I have bunch of files with no pattern in their name at all in a directory. 我在目录中有一堆完全没有名称的文件。 all I know is that they are all Jpg files. 我所知道的是它们都是Jpg文件。 How do I rename them, so that they will have some sort of sequence in their name. 如何重命名它们,以便它们的名称具有某种顺序。

I know in Windows all you do is select all the files and rename them all to a same name and Windows OS automatically adds sequence numbers to compensate for the same file name. 我知道在Windows中,您要做的就是选择所有文件并将它们重命名为相同的名称,Windows操作系统会自动添加序列号以补偿相同的文件名。

I want to be able to do that in Linux Fedora but I you can only do that in Terminal. 我希望能够在Linux Fedora中做到这一点,但我只能在Terminal中做到这一点。 Please, help. 请帮忙。 I am lost. 我搞不清楚了。

What is the command for doing this? 这样做的命令是什么?

The best way to do this is to run a loop in the terminal going from picture to picture and renaming them with a number that gets bigger by one with every loop. 最好的方法是在终端中运行从图片到图片的循环,并将其重命名为一个数字,每个循环的数字将增加一个。

You can do this with: 您可以执行以下操作:

n=1
for i in *.jpg; do
    p=$(printf "%04d.jpg" ${n})
    mv ${i} ${p}
    let n=n+1
done

Just enter it into the terminal line by line. 只需将其逐行输入到终端中即可。

If you want to put a custom name in front of the numbers, you can put it before the percent sign in the third line. 如果要在数字前放置自定义名称,则可以在第三行的百分号之前放置自定义名称。

If you want to change the number of digits in the names' number, just replace the '4' in the third line (don't change the '0', though). 如果要更改名称数字中的位数,则只需在第三行中替换“ 4”(但是不要更改“ 0”)。

I will assume that: 我将假设:

  • There are no spaces or other weird control characters in the file names 文件名中没有空格或其他奇怪的控制字符
  • All of the files in a given directory are jpeg files 给定目录中的所有文件均为jpeg文件

That in mind, to rename all of the files to 1.jpg, 2.jpg, and so on: 请记住,要将所有文件重命名为1.jpg,2.jpg等:

N=1
for a in ./* ; do
    mv $a ${N}.jpg
    N=$(( $N + 1 ))
done

If there are spaces in the file names: 如果文件名中有空格:

find . -type f | awk 'BEGIN{N=1}
    {print "mv \"" $0 "\" " N ".jpg"
     N++}' | sh

Should be able to rename them. 应该可以重命名它们。

The point being, Linux/UNIX does have a lot of tools which can automate a task like this, but they have a bit of a learning curve to them 关键是,Linux / UNIX确实有很多工具可以使诸如此类的任务自动化,但是它们具有一些学习上的困难。

Create a script containing: 创建一个包含以下内容的脚本:

#!/bin/sh

filePrefix="$1"
sequence=1

for file in $(ls -tr *.jpg) ; do
    renamedFile="$filePrefix$sequence.jpg"
    echo $renamedFile
    currentFile="$(echo $file)"
    echo "renaming \"$currentFile\" to $renamedFile"
    mv "$currentFile" "$renamedFile"
    sequence=$(($sequence+1))
done
exit 0

If you named the script, say, RenameSequentially then you could issue the command: 如果为脚本命名,请说“重命名”,则可以发出以下命令:

./RenameSequentially Images- ./RenameSequentially Images-

This would rename all *.jpg files in the directory to Image-1.jpg, Image-2.jpg, etc... in order of oldest to newest... tested in OS X command shell. 这会将目录中的所有* .jpg文件重命名为Image-1.jpg,Image-2.jpg等,以便按照从旧到新的顺序在OS X命令外壳中进行测试。

I wrote a perl script a long time ago to do pretty much what you want: 很久以前,我写了一个perl脚本来完成您想要做的事情:

#
# reseq.pl    renames files to a new named sequence of filesnames
#
# Usage: reseq.pl newname [-n seq] [-p pad] fileglob
#
use strict;
my $newname = $ARGV[0];
my $seqstr = "01";
my $seq = 1;
my $pad = 2;

shift @ARGV;

if ($ARGV[0] eq "-n") {
    $seqstr = $ARGV[1];
    $seq = int $seqstr;
    shift @ARGV;
    shift @ARGV;
}

if ($ARGV[0] eq "-p") {
    $pad = $ARGV[1];
    shift @ARGV;
    shift @ARGV;
}

my $filename;
my $suffix;

for (@ARGV) {
    $filename = sprintf("${newname}_%0${pad}d", $seq);
    if (($suffix) = m/.*\.(.*)/) {
        $filename = "$filename.$suffix";
    }
    print "$_ -> $filename\n";
    rename ($_, $filename);
    $seq++;
}

You specify a common prefix for the files, a beginning sequence number and a padding factor. 您可以为文件指定通用前缀,起始序号和填充因子。

For exmaple: 例如:

# reseq.pl abc 1 2 *.jpg

Will rename all matching files to abc_01.jpg, abc_02.jpg, abc_03.jpg... 将所有匹配的文件重命名为abc_01.jpg,abc_02.jpg,abc_03.jpg ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM