- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: how to automatically rename files
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2005 09:12 AM
тАО02-01-2005 09:12 AM
/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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2005 09:20 AM
тАО02-01-2005 09:20 AM
Re: how to automatically rename files
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2005 09:47 AM
тАО02-01-2005 09:47 AM
Solutionrun (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 -------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2005 10:26 AM
тАО02-01-2005 10:26 AM
Re: how to automatically rename files
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2005 02:35 AM
тАО02-02-2005 02:35 AM
Re: how to automatically rename files
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