Operating System - HP-UX
1832082 Members
3021 Online
110037 Solutions
New Discussion

Re: How to rename all files in bulk.

 
Gulam Mohiuddin
Regular Advisor

How to rename all files in bulk.

I have more than 100 files in one directory with *.log extension and I want to rename all of them to *.bak.

[/logs]$>gis1.log
[/logs]$>gis2.log
[/logs]$>gis3.log
.
.
.
Now the following command does not work:
$mv *.log *.bak


Thanks,
Gulam.
Everyday Learning.
8 REPLIES 8
Curtis Larson
Trusted Contributor

Re: How to rename all files in bulk.

for i in $(ls *.log)
do
mv $i $(basname $i .log).bak
done
Alan Riggs
Honored Contributor

Re: How to rename all files in bulk.

Just for fun:

echo *.log |while read i
do
mv $i ${i%log}bak
done
Stephen Tatrai
Advisor

Re: How to rename all files in bulk.

Here is one way

ls *.log | sed "s/\(.*\)\.log $/mv '&' '\1.bak'/" | sh

remove the | sh to see what the command would do
John Waller
Esteemed Contributor

Re: How to rename all files in bulk.

Gulam,
I've been looking for a similar solution. Out of the 3 listed only Carson's appeared to work but be careful to type basename not basname as basname will delete all the *.log files
federico_3
Honored Contributor

Re: How to rename all files in bulk.


I would do like this:

cd DIR
ls -1 *.log | awk -F "." '{system("mv "$0 " "$1".bak")}'

I hope this helps
Federico
Shannon Petry
Honored Contributor

Re: How to rename all files in bulk.

This works in borne and korn shell. I have made lots of steps so you can see what is going on!

>for NAME in `*.log` ; do
>BASE=`basename ${$} .log`
>NEWNAME="${BASE}.bak"
>echo "Renaming $NAME to $NEWNAME"
>/usr/bin/mv ${NAME} ${NEWNAME}
>sleep 1
>done


If you want to see what is occurring, then use some echo statements in your loop. The sleep will give you 1 second to look at what is happening in the loop.

This will work in Cshell
>foreach NAME (`ls *.log)
>BASE=`basename ${$} .log`
>NEWNAME="${BASE}.bak"
>echo "Renaming $NAME to NEWNAME"
>/usr/bin/mv ${NAME} {NEWNAME}
>sleep 1
>end

In both cases you should first understand the loops in the shell your using. also, understand the primary command being used to derive the new name.
man ksh
man sh
man csh
man basename

It would be recommended as well to get a book or two or three on shells and shell scripting. This is pretty basic shell programing which most admins need to understand.

Regards,
Shannon
Microsoft. When do you want a virus today?
Alan Riggs
Honored Contributor

Re: How to rename all files in bulk.

I am curious as to what results you found with my solution. It should function appropriately under posix or korn shells, and has the benefit of not being subject to command buffer overruns should the number of files in teh directory grow too large.
Gregory Fruth
Esteemed Contributor

Re: How to rename all files in bulk.

In csh:

foreach f ( *.log )
mv $f $f:r.bak
end

(The :r modifier strips off the filename extension.)

If sh:

for f in *.log
do
mv $f `basename $f .log`.bak
done

This only works properly if the files are in the current directory.
If that's not the case, change the "mv" line to:

mv $f `dirname $f`/`basename $f .log`.bak