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.
for i in *.tgz; do mv "$i" "${i/.tgz}".tar.gz; done
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.
Oct 21, 2011 at 7:00
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
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.
I recommend 'mmv'. Pretty simple and less error-prone than 'for' expressions.
mmv
and perhaps a link to this utility? I think many of us are unfamiliar with this utility.
Oct 21, 2011 at 22:49