Operating System - HP-UX
1819804 Members
3042 Online
109607 Solutions
New Discussion юеВ

how to automatically rename files

 
SOLVED
Go to solution
Charles Holland
Trusted Contributor

how to automatically rename files

I have a series of files in a directory some with the same prefix and some without. Example

/tmp/a.out
/tmp/b_001
/tmp/b_001.cfg
/tmp/b_001.txt
/tmp/b_001.ust
/tmp/b_001.xyz
/tmp/c_001
/tmp/e_567.abc

Now what I have been trying to do is get something that I can feed two environment variables to, lets say src=b_0 and TRG=c_0, and the script will copy each *file* and rename it.
I do not want b_001 to overwrite c_001, but all the other b_001* to be renamed to c_001*.

I have been looking at sed, but that appears that it is primarily for doing substituions on character strings *within* a file. I'm looking to do something like

files=`ls /tmp/|grep -v -e a.out -e 'b_001 ' -e c_001 -e e_567.xyz`
;export files
# I think I now have a list of files I want to change
for i in $files
do
....I have to do some type of name maniplation and execute a mv command
of moving the SRC to the TRG here... but how?
done

I've searched ITEC and can't find it. Burned to many brain cells today..... need adult beverage.

Points will be awarded as usual.

thanks in advance
"Not everything that can be counted counts, and not everything that counts can be counted" A. Einstein
4 REPLIES 4
Steven E. Protter
Exalted Contributor

Re: how to automatically rename files

General suggestion:

cd /target_directory
ls -1 > filelist
# ls dash one


while read -r filename

do
if [ -f "something${filename}"
then
mv $filename "somenew${filename}"
else
mv $filename "something${filename}"
fi

done < filelist


its an outline. You should be able to build into a working script, based on your requirements.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Biswajit Tripathy
Honored Contributor
Solution

Re: how to automatically rename files

Put the following script in a file named "mvfile" and
run (TEST BEFORE USE)

$ mvfile SRC TRG

#--------- mvfile Start ---------
src=$1
trg=$2
for file in `ls -1 /tmp | grep "^$src"`
do
newfile=`echo $file | sed "s/^$src/$trg/"`
if [ -f /tmp/$newfile ]
then
echo " File /tmp/$newfile exists; can not mv"
else
mv /tmp/$file /tmp/$newfile"
fi
done
--------- End -------------
:-)
Robert Salter
Respected Contributor

Re: how to automatically rename files

Try this;

src=b_0
TGT=c_0

cd /tmp

for i in `ls $SRC* | awk '{print substr($1, 4, 8)}'`
do
if [ -f $TGT$i ]
then
mv $SRC$i $TGT$i.a
else
mv $SRC$i $TGT$i
fi
done


Depending on the length of your filename you can change the number of chars in the awk statement, I used 8 on 5 character filenames and it worked ok.
Time to smoke and joke
Charles Holland
Trusted Contributor

Re: how to automatically rename files

Thanks to all and points have been assigned.

SEP - this was probably a good "General suggestion:". Just couldn't follow it.

Biswajit - Exactly what I was looking for. Had to tweek it a bit as the actual line that did the mv command had an extra " at the end.

Robert - Feel this would work equally well.

Thanks for all answers
"Not everything that can be counted counts, and not everything that counts can be counted" A. Einstein