bash - Easily renaming multiple files

Subject:

Renaming multiple files seems to be a problem which many newcomers to shell scripting, or administration, have problems with. But once you've done it a few times the actual solutions are very simple.

There are many cases where you might have a large number of files to rename en masse, for example files which are output from a given script or tool. Or files that must be renamed to be uploaded to a web-host.

How you rename the files mostly depends on which tools you have available, and which shell you're using.

The way I tend to rename large numbers of files is the same way that I tend do any job which requires running the same command on a number of files - I use the looping facility within the bash shell.

For example lets assume you have several files in a directory a.JPG, b.JPG, c.JPG and d.JPG then you can work with each one as follows:

skx@lappy:~$ for i in *.JPG; do echo $i; done a.JPG b.JPG c.JPG d.JPG

This command has worked with each file which matches the pattern "*.JPG", and runs a command on each file - in this case it's "echo $i".

Obviously the command which we're running, here it is echo, can be replaced with other commands.

If we used to use this style of command to rename each of the files to lowercase their extensions we have two possible approaches:

  • Take advantage of bash's variable substituion.
  • Use the standard unix command basename.

The bash shell has a notion of text substitutions which is very powerful, and something that can be very useful to know about so we'll cover that first.

The simplest example I can think of is to strip the extension. For a filename which is stored in the shell variable $i you can strip a known extension with this:

skx@lappy:~$ i=a.JPG skx@lappy:~$ echo $i a.JPG skx@lappy:~$ echo ${i/.JPG/} a

Here we've used the substitution operation to strip the ".JPG" from the end of our variable - we can plug this into the loop we showed earlier to force a rename:

skx@lappy:~$ ls a.JPG b.JPG c.JPG d.JPG skx@lappy:~$ for i in *.JPG; do mv "$i" "${i/.JPG}".jpg; done skx@lappy:~$ ls a.jpg b.jpg c.jpg d.jpg

(The "quotes" around the arguments to the mv command are useful if you're ever working with files which contain strange characters, or spaces, in their names).

This is the basic way that you can rename files with bash - use a loop to work on each file, then use the built-in substituion operations to strip or add to each name in turn.

The other approach is to use basename to manipulate filenames. basename is a standard command which allows you to strip suffixes from filenames:

skx@lappy:~$ basename c.jpg .jpg c skx@lappy:~$ basename c.jpg pg c.j

Here you can see that the command just returns the filename you've specified with the second argument removed from it.

This can be plugged into our loop like this:

skx@lappy:~$ ls a.jpg b.jpg c.jpg d.jpg skx@lappy:~$ for i in *.jpg; do mv "$i" "`basename $i .jpg`.JPG"; done skx@lappy:~$ ls a.JPG b.JPG c.JPG d.JPG

 

Using Specialised Commands

Instead of remembering how to run these loops over files with bash, you can instead rename files en masse with specialised renaming commands.

A simple one is the rename command which is included with the Debian perl package and almost certainly available to you already.

The rename command allows you to rename files with full perl expressions, in a simple manner.

For example our preceeding example of changing the extension can be achieved with:

skx@lappy:~$ rename 's/\.JPG/\.jpg/' *.JPG

We can also, for example, strip spaces from filenames with this:

skx@lappy:~$ rename 's/ //' *.JPG

Renaming files from upper to lower case can be a simple job:

skx@lappy:~$ rename 'y/A-Z/a-z/' *

There are also other specialised commands which you can locate with the apt-cache command, such as mmv and mrename.

 

2015-10-25 21:52:10gstlouis

references: http://unix.stackexchange.com/questions/77007/mv-cannot-stat-no-such-file-or-directory-in-shell-script

gstlouis
vote
2015-10-25 21:52:35
[url=https://www.viagrasansordonnancefr.com/]viagrasansordonnancefr.com[/url] viagrasansordonnancefrRonaldsnuri
vote
2017-05-02 20:38:27
[url=https://www.viagrapascherfr.com/]Click here!..[/url]CaseyNak
vote
2017-08-29 09:15:34
Do you feel like you have tried everything possible in order to lose weight? You are not alone--many people have the same problem. The following article is designed to give you tips that you may not have even known existed. By following these tips, you will reach your weight loss goal in no time.AngeloBeath
vote
2017-10-01 07:18:31
Doctor Who is now considered a British Institute and has come a long way since it first aired on November 23rd 1963. The very first show saw the Doctor travel 100,00 years into the past to help some dim cavemen discover light. After 26 seasons and seven Doctors later the series came off our screens in 1989 much to the disappointment of the huge devoted fanbase. In 1996 an attempt was made to revive Doctor Who but it wasnt until June 2005 when it came back with a vengeance with Christopher Eccleston as the ninth Doctor that put the series back on the map as it were. It then went on for 5 years with David Tenant portraying the Doctor until 2010 when Matt Smith took over the role. Today it is still a great family show and has attracted many new fans. If youre a new or old fan of the show there are Tours and museums you can go and see some of the locations and memorabilia of this classic show. The Doctor Who Tour of London will take you on over 15 locations from the show, some from the new series and some from old sites like the location of The Invasion and Resurrection of the Darleks. The tour also takes you to the TV museum in London where you will get to see some of the cosumes worn in the show and props used. Also you can buy gifts and memorabilia from the shop. You will learn all about how the shows were made so the tour is also educational. If you want to take pictures of the locations thats not a problem. Remember the front door of 10 Downing Street in Aliens of London? Well you can get up and close to this and get your picture taken in front of the door. Rose Tyler fans will love the tour as you get to drop by her home in the show. Why not go that extra mile and actually meet a Doctor Who star. Well this is possible with private or group tours. You will get the general tour but included will be a pre-arranged meeting or lunch with a celebrity from the show. This will obviously depend on availability of the celebrity and the cost will reflect the popularity of that celebrity. There are tours in London and also Wales. The Wales tours take you to Cardiff where you will see lots of location which were featured in shows since 2005. You can leave from London or at Leigh Delamere services station on the M4. There is a Doctor Who exhibition in Cardiff which you get to see. At the end of the Doctor Who tour you get a souvenir group picture sent you by email which is a nice touch. For seriously devoted Doctor Who fans there is a 3 day tour which takes you to all the locations in both London and Cardiff. You will see locations from the past 45 years as well as recent sites from the lasted Doctor Who series. Day one is based in London where you get to see 15 sites. Day two takes you to Cardiff where you get to mean the real life owner of the to see we have an Gothic property used as the location of the school in Human Nature. The final day is partly spent in Cardiff with a walking tour at Cardiff Bay, then you head back to London but a stop at Stonehenge to see the site of the Pandoica. Then its dinner at The Cloven Hoof pub in Devils End b efore you taken back to central London.  TerryDrero
vote
2017-10-03 03:39:57
Some people, especially those running on busy daily schedules tend to use the pills to help maintain weight since they can not afford to follow all the diet programs. This is not advised. It is recommended that one seek advice from a professional in this field before using the pills. This can save one from many dangers associated with the misuse. The diet pills should always be taken whole. Some people tend to divide the pills to serve a longer period of time. This is not advised and can lead to ineffectiveness. If it is required that one takes a complete tablet, it means that a certain amount of the ingredients are required to achieve the desired goal. It is also recommended that one does not crush the pill and dissolve it in beverages. Chemicals found in beverages have the potential of neutralizing the desired nutrients in the pill thereby leading to ineffectiveness. The best way to take the tablets is swallowing them whole with a glass of water. The diet pills speed up the metabolic processes. This is the key factor that leads to the burning of all the fats in the body. This means that one passes out lots of urine, which subsequently leads to dehydration. It is imperative that the user take lots of water round the clock. This will help curb dehydration, which can lead to health problems. In addition to that, water offers the required medium for the function of the nutrients and elimination of the fats. When buying the review of diet pills, it is imperative that one gets the most recommended dose. People tend to compromise the quality and effectiveness of the tablets due to the variation in cost. The low priced pills depict poor quality, which means their effectiveness is not reliable. Some have also been found to cause health problems. The dose should also be taken as recommended. Over dose will not speed up the process but rather lead to complication. This will increase risk of side effects. If the taking of the pill is forgotten, do not take more to compensate for the lost time. The diet plan enclosed with the diet pills has also to be followed. According to the requirements, the termination of the diet must be done even with no results. This means your body is irresponsive.CaseyNak
vote
2017-10-08 00:04:10
Doctor Who is now considered a British Institute and has come a long way since it first aired on November 23rd 1963. The very first show saw the Doctor travel 100,00 years into the past to help some dim cavemen discover light. After 26 seasons and seven Doctors later the series came off our screens in 1989 much to the disappointment of the huge devoted fanbase. In 1996 an attempt was made to revive Doctor Who but it wasnt until June 2005 when it came back with a vengeance with Christopher Eccleston as the ninth Doctor that put the series back on the map as it were. It then went on for 5 years with David Tenant portraying the Doctor until 2010 when Matt Smith took over the role. Today it is still a great family show and has attracted many new fans. [url=https://www.cialissansordonnancefr24.com/achat-cialis-au-canada/]https://www.cialissansordonnancefr24.com/achat-cialis-au-canada/[/url]Robertler
vote
2017-11-30 22:12:40