How To: Renaming files on the command line

Posted by ardchoille on Aug 20, 2009 9:59 PM EDT
Ian's Thoughts; By Ian MacGregor
Mail this story
Print this story

Suppose you have a directory full of files with filenames such as oldfile, my-new-file, some_document. Suppose you would like to rename these files to File1, File2, file3.

Suppose you have a directory full of files with filenames such as oldfile, my-new-file, some_document. Suppose you would like to rename these files to File1, File2, file3. Among the choices you have are opening a GUI application, renaming each file manually using a file manager or renaming the files all at one time right from the command line. GUI applications are great and can help you get the needed work done and manually renaming a batch of files in a file manager can take some time. However, if you are already working on the command line, in a console or just want to get the work done fast, there is a quick and easy method of renaming files.

The method I am going to show you today is as follows:

j=1; for i in *; do mv "$i" File$j; j=$((j+1)); done

This combination of commands renames a specified group of files such as oldmemo, my-file, new_doc to File1, File2, File3 or car-pic.jpg, old-house.jpg, my_friends_wedding.jpg to photo1.jpg, photo2.jpg and photo3.jpg.

Let's break this down and see what's happening in this combination of commands:

j=1
This assigns the number 1 to the variable j. You could use any number you want but you would probably want the resulting files to be incremented by 1.

for i in *
This performs the work on all files (*) in the directory one-at-a-time. The wildcard "*" can be changed to perform the work only on files specified by a certain file extension, such as .jpg or .txt files, by by changing the wildcard to something such as *.jpg or *.txt. This would change the above portion of the command to for i in *.jpg or for i in *.txt respectively.

do mv "$i" File$j
This begins a loop renaming each file to File$j where the variable j is a number. You could also change the "File" portion of the command to something such as "Photo" or "text-file".

j=$((j+1))
This increments the number stored in j by 1. Remember we started by assigning the number 1 to the variable j so the j=$((j+1)) portion of the command increments the number stored in j by 1 for each iteration of the loop. This results in assigning a unique number to each file.

done
This tells the system to stop the "do" loop, we don't need to go any further once the for i in * portion of the command has been satisfied, we're done.

The variables i and j can be anything such as x and p but they cannot be identical - you must use two different variables.

Below is the result of this tutorial applied to files on my system:

[downloads @ 08:53:00]$ ls
[downloads @ 08:53:01]$ touch oldfile my-file some_old_document text-file-1 document_2
[downloads @ 08:53:06]$ ls
document_2 my-file oldfile some_old_document text-file-1
[downloads @ 08:53:08]$ j=1; for i in *; do mv "$i" File$j; j=$((j+1)); done
[downloads @ 08:53:15]$ ls
File1 File2 File3 File4 File5
[downloads @ 08:53:16]$

My command prompt is set up to display the current time, this allows me to see the time it took to perform any command. As you can see, this method of renaming files is quite fast. I once used the above command combination to rename 1,066 files in under a minute.

Shell commands are easy to write and can perform work on any number of files very quickly. Anyone interested in writing their own shell commands can begin by reading the BASH Programming - Introduction HOW-TO. The BASH Programming tutorial, and many other excellent tutorials, can be found at the Linux Documentation Project.

Full Story

  Nav
» Read more about: Story Type: News Story, Tutorial; Groups: Linux

« Return to the newswire homepage

This topic does not have any threads posted yet!

You cannot post until you login.