Renaming Files using the terminal

Asked by Matt B

I need to rename a whole bunch of files, removing spaces and changing to lower case (for consistency and to ensure compatibility with current web server environment).

I'd like to do this via the command line in the terminal. Please can you point in the right direction for information on how to do this with a simple command or, if possible advise me on how to do this.

Many thanks, in advance, for any help that can be offered.

Matt

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu gnome-terminal Edit question
Assignee:
No assignee Edit question
Solved by:
Matt B
Solved:
Last query:
Last reply:
Revision history for this message
Witold Krakowski (wkrakowski-gmail) said :
#1

The following is a bash script which will convert all uppercase characters in all filenames in current directory to lowercaseç

 #!/bin/bash
 for file in *
  do newfile=`echo $file|tr [A-Z] [a-z]`
  echo "Moving $file to $newfile"
  mv $file $newfile
 done

An alternative is to use the command line rename utilityç

rename 'y/A-Z/a-z/' *

You can remove spaces in filenames in the following way:

rename 's/ //' *.*
find . -name CVS | xargs rm -r

Revision history for this message
Matt B (admin-mdbwebdata) said :
#2

Wiltold - many, many thanks for your help.

The: -

rename 'y/A-Z/a-z/' *

option worked very well for changing from upper case to lower case.

I wasn't able to remove the spaces using: -

rename 's/ //' *.*
find . -name CVS | xargs rm -r

But this may well be to do with me not fully understanding the various arguments and using them correctly for my particular case.

What I did was replace the spaces with an underscore using this: -

rename 'y/ /_/' *

Which seemed to work very well. (I hope that is the correct way to use it!).

So, thanks again.

Revision history for this message
Witold Krakowski (wkrakowski-gmail) said :
#3

Hi Matt, you're welcome.

You correctly used rename to replace spaces with underscore.
Do You need further help or is your problem solved?

Revision history for this message
Matt B (admin-mdbwebdata) said :
#4

Thanks Wiltold.

I think you've solved the problem. Thanks again.

Revision history for this message
Jim Hutchinson (jphutch) said :
#5

Just to second the cool "find" command for this type of job. There are a lot of ways it can be used. It basically finds files that match a pattern and then does something to them. For example,

~/wallpaper$ find -name '*.png' -exec cp {} ~/photos/ \;

Finds all the files in the wallpaper directory that are a png file and copies them to a new folder called photos. There are a ton of options. I have an O'Reilly book and there are about 4 pages of options. Anyway, something to file away for another time.

Revision history for this message
Matt B (admin-mdbwebdata) said :
#6

Thanks Jim. That looks a good option - I'll have to check out O'Reilly, or similar, for more info. on this.

Many thanks.

Revision history for this message
Jim Hutchinson (jphutch) said :
#7

This is the book I was referring to if you are interested. They are others too, but this one is fairly comprehensive.

http://www.oreilly.com/catalog/9780596100797/

Revision history for this message
Matt B (admin-mdbwebdata) said :
#8

Thanks again Jim - that's very helpful. Much appreciated.