Operating System - HP-UX
1848631 Members
5024 Online
104033 Solutions
New Discussion

Re: Renaming Multiple files in one command

 
SOLVED
Go to solution
Amit Dixit_2
Regular Advisor

Renaming Multiple files in one command

Hi,
I have more that 500 file with naming convention
myfile.txt.20050117042157484.arc
and I want to rename them to
myfile.txt.20050117042157484

Want to remove ".arc" extension from all of them.

will this command do
ls -1 *.arc | awk '{print "mv "$1.arc" "$1" "}'

Thanks,
Amit.
10 REPLIES 10
Paul_481
Respected Contributor

Re: Renaming Multiple files in one command

Hi Amit,

Run this script on the directory of the file.

for i in `ll|grep txt|awk '{print $9}'`
do
nfile=${i%.arc}
mv $i $nfile
done

I checked it, it work. Modify to suit your needs.

Regards,
Paul
Hein van den Heuvel
Honored Contributor

Re: Renaming Multiple files in one command

You are not the first one to ask this question.
For example look for rename in a Unix faq:
http://www.faqs.org/faqs/unix-faq/faq/part2/index.html


The following examples, tailored to your problem work for me:

piping ls selection into perl:

$ ls my*arc | perl -ne 'chop; $old=$_; s/\.arc//; rename $old,$_'

or just perl glob function:

perl -e 'while (){$old=$_; s/\.arc//;rename $old,$_}'


btw... Paul's example can be simplified a lot replacing:
`ll|grep txt|awk '{print $9}'`
with:
`ls *txt*`


Regards,
Hein.
Venkatesan_5
Frequent Advisor

Re: Renaming Multiple files in one command

hi amit,

try this ...
goto the directory where you want to change ....execute this ....

for i in *.arc
do
nfile=${i%.arc}
mv $i $nfile
done

I have checked and it works...


regds
Venkatesan.
Muthukumar_5
Honored Contributor

Re: Renaming Multiple files in one command

You can do it as,


for file in `ls *.arc`
do

mv $file $(echo $file | cut -d"." -f1-3)

done

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Renaming Multiple files in one command

You can use your method also as,

ls -1 *.arc | awk '{ split($1,a,"."); print "mv "$1" "a[1]"."a[2]"."a[3] }' | sh

| sh is used to make action.

It will do it.
Easy to suggest when don't know about the problem!
C. Beerse_1
Regular Advisor
Solution

Re: Renaming Multiple files in one command

I already see some replies, none of which uses the `basename` command, which has an option to remove the extention.

for f in *.arc
do
mv $f `basename $f .arc`
done

Or with find, to traverse a filesystem (this is a one-liner)

find . -name '*.arc' -exec 'mv {} `dirname {}`/`basename {} .arc` ;'
make everything as simple as possible, not simpler (A.Einstein??)
Sandman!
Honored Contributor

Re: Renaming Multiple files in one command

amit,

the following command will do what you're looking for...


# ls -1 *.arc | awk -F".arc" '{system("mv "$0" "$1)}'

Re: Renaming Multiple files in one command

amit,

try this ,

ls -1 *arc |sed 's/\(.*\)\(\.arc\)/mv \1\2 \1'|sh -v

regards
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Renaming Multiple files in one command

Why to complicate sed

sed -e 's/\.arc$//g'
Vibhor Kumar Agarwal
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Renaming Multiple files in one command

Lets generalise it a bit:

The following will work for any extension

sed -e 's/\..*$//g'
Vibhor Kumar Agarwal