Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How do I rename all files in the current directory with an extention of .tgz to .tar.gz? I tried

find . -iname *.tgz --exec mv {} {}.tar.gz \;

but it doesn't work as expected.

share|improve this question
add comment

6 Answers

Handy one is rename .tgz .tar.gz *.tgz

http://linux.die.net/man/1/rename

share|improve this answer
add comment
ls *.tgz | sed 's/\(.*\).tgz$/mv "&" "\1.tar.gz"/' | sh
share|improve this answer
add comment
for i in *.tgz; do mv "$i" "${i/.tgz}".tar.gz; done
share|improve this answer
    
With that, you will run in problem with filenames with spaces –  f4m8 Oct 21 '11 at 6:16
    
For filenames with spaces, simply prepend IFS=$'\t\n'; to the start of the command. This will remove the space as a "Internal Field Separator". Do a search on linux ifs for more tricks with this variable. –  slillibri Oct 21 '11 at 7:00
    
Both expansions are quoted; there will be no problems with spaces. –  adaptr Oct 21 '11 at 7:03
add comment

rename is a nice one. But you should watch out for Debian-derivatives - they don't provide the same version as other distros.

Debian/Ubuntu/friends :

# rename 's/\.tgz$/\.tar.gz/ *.tgz

Everyone else as far as I know - http://linux.die.net/man/1/rename

# rename .tgz .tar.gz *.tgz
share|improve this answer
add comment

You may need to do something like this:

[rilindo@kvm0001 bar]$ ls -la
total 8
drwxr-xr-x. 2 rilindo cgred 4096 Oct 20 23:16 .
drwxr-xr-x. 8 rilindo cgred 4096 Oct 20 23:10 ..
-rw-r--r--. 1 rilindo cgred    0 Oct 20 23:15 bar.txt
-rw-r--r--. 1 rilindo cgred    0 Oct 20 23:15 baz.txt
-rw-r--r--. 1 rilindo cgred    0 Oct 20 23:16 foo.txt
[rilindo@kvm0001 bar]$ for i in `find . -type f -iname "*.txt"`; do j=`echo $i | sed 's/.txt$/.doc/'`; mv $i $j; done
[rilindo@kvm0001 bar]$ ls -la
total 8
drwxr-xr-x. 2 rilindo cgred 4096 Oct 20 23:16 .
drwxr-xr-x. 8 rilindo cgred 4096 Oct 20 23:10 ..
-rw-r--r--. 1 rilindo cgred    0 Oct 20 23:15 bar.doc
-rw-r--r--. 1 rilindo cgred    0 Oct 20 23:15 baz.doc
-rw-r--r--. 1 rilindo cgred    0 Oct 20 23:16 foo.doc
[rilindo@kvm0001 bar]$ 

So with this command string:

for i in `find . -type f -iname "*.txt"`; do j=`echo $i | sed 's/.txt$/.doc/'`; mv $i $j; done

I get the list of files and for each element, assign the file name with a new extension to a variable and rename each file to that variable.

This is, of course, probably not a perfect script, but I think you get the idea.

share|improve this answer
    
It didn't work if file name including spaces. –  quanta Oct 21 '11 at 3:21
    
That is true. Hence, my caveat. :) –  Rilindo Oct 21 '11 at 3:27
add comment

I recommend 'mmv'. Pretty simple and less error-prone than 'for' expressions.

share|improve this answer
    
Hi Peter, welcome to Serverfault. Can you provide either more information about mmv and perhaps a link to this utility? I think many of us are unfamiliar with this utility. –  Stefan Lasiewski Oct 21 '11 at 22:49
    
@StefanLasiewski, see link @ serverfault.com/questions/323527/rename-all-the-files/… — it mentions mvv in "See also" section. –  poige Oct 22 '11 at 12:51
    
mmv can be found in the ubuntu universe repository. After enabling the repo, one can install it in the standard way by issuing 'sudo apt-get install mmv' –  Péter Bócz Nov 15 '11 at 12:59
add comment

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.