Wednesday, August 21, 2013

How to rename or unzip or untar and vice-versa all files in a directory with same file extension - UNIX

To rename or unzip or untar and vice-versa all files in a directory use below commands

Command:
$ for i in `ls -latr *.txt.2013 | awk '{print $9}'`
do
mv $i `basename $i .2013`
done

The above command will rename all files in a directory for example test01.txt.2013, test02.txt.2013, test03.txt.2013 to test01.txt, test02.txt, test03.txt

Note: All lines after $ symbol line together form the command.

A similar command can be used to unzip or untar all files in a directory

Command:

$ for i in `ls -latr *.txt.gz | awk '{print $9}'`
do
gunzip $i
done

The above command will unizp all files in a directory for example test01.txt.gz, test02.txt.gz, test03.txt.gz to test01.txt, test02.txt, test03.txt

Note: All lines after $ symbol line together form the command.


No comments: