Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there a way to rename all files in a directory to lowercase|uppercase?

I am looking for a oneliner command.

I loved TotalCommander's alt+f7, now I need that functionality in the Terminal

share|improve this question

2 Answers 2

up vote 18 down vote accepted

For each file a_file in current directory rename a_file to lower case.

for a_file in *;do mv -v $a_file `echo $a_file | tr [:upper:] [:lower:]` ;done;

For upper case reverse the arguments to [:lower:] [:upper:]

tr command reference link

Update

For even more control * can be replaced with ls.

For example in a directory containing 1.txt, 2.txt, 3.txt, 1.jpg, 2.jpg and 3.jpg in order to filter only *.jpg files, ls can be used:

for a_file in $(ls *.jpg);do mv -v $a_file `echo $a_file | tr [:upper:] [:lower:]` ;done;

The above code will assign to a_file variable all files with .jpg extension.

Update added -v option to mv command as per sds suggested.

share|improve this answer
    
+1 very slick answer! –  BryceAtNetwork23 Apr 18 '13 at 13:05
4  
you might want to pass "-v" to mv so that you see what is being done; you might not want to pass "-f" because this may clobber existing files –  sds Apr 18 '13 at 14:33

There's a more elegant and general utility called prename.

Written by Larry Wall, it comes with perl so it is most likely already available on your system as /usr/bin/prename (if you have setup alternatives is may also be available as /usr/bin/rename symlinked via /etc/alternatives to prename)

Using it you can rename multiple files in one command by providing any perl expression (including but not limited to substitution s/// or char transliteration tr///):

Examples:

# Lowercase all *.JPG filenames:
prename tr/A-Z/a-z/ *.JPG

# Change any 'No' in a filename to a 'Yes':
prename s/No/Yes/g *No*

# increment first sequence of digits in a filename by 3:
prename 's/([0-9]+)/$1+3/e' *[0-9]*

# If a file contains 'aaa', append '.bak' to its name
prename 'if (/aaa/) {$_ .= ".bak"}'  *

And so on.

Another nice thing about prename is that it protects you, in the case of renaming a file to an existing file name.

man prename for more details.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.