简体   繁体   中英

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

I want to be able to do that in Linux Fedora but I you can only do that in 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).

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

That in mind, to rename all of the files to 1.jpg, 2.jpg, and so on:

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

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-

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.

I wrote a perl script a long time ago to do pretty much what you want:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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