2

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.

6 Answers 6

3

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

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

1
ls *.tgz | sed 's/\(.*\).tgz$/mv "&" "\1.tar.gz"/' | sh
1
for i in *.tgz; do mv "$i" "${i/.tgz}".tar.gz; done
3
  • With that, you will run in problem with filenames with spaces
    – f4m8
    Oct 21, 2011 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, 2011 at 7:00
  • Both expansions are quoted; there will be no problems with spaces.
    – adaptr
    Oct 21, 2011 at 7:03
1

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
0

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.

2
  • It didn't work if file name including spaces.
    – quanta
    Oct 21, 2011 at 3:21
  • That is true. Hence, my caveat. :)
    – Rilindo
    Oct 21, 2011 at 3:27
0

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

3
  • 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. Oct 21, 2011 at 22:49
  • @StefanLasiewski, see link @ serverfault.com/questions/323527/rename-all-the-files/… — it mentions mvv in "See also" section.
    – poige
    Oct 22, 2011 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' Nov 15, 2011 at 12:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .