Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple files to rename...

Status
Not open for further replies.

kumariaru

IS-IT--Management
Mar 9, 2006
61
AU
Hi All,
I need to rename multiple files after copying them to a folder from other folder.

I am getting files in this format..

test_20070513.txt, test1_20070513.txt,...

I want to strip the _ & date and I want file names to be as

test.txt, test1.txt,.....

I am trying to do something like this

cp /tmp/target/* /tmp/source/
echo 'files are moved to source folder'
ls -l |tr 's' '_*.txt' '.txt'
echo 'status=0'
exit 0


I tried to use 'sed' command too. I am not able to get what I need.Please help me in building this script...


Thanks in Advance...

 
One way:
Code:
cd /tmp/target
ls *_*.txt | awk '
{x=$0;sub(/_.*\.txt$/,".txt",x);printf "mv %s /tmp/source/%s\n",$0,x}
' | sh -v


Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OOps, sorry for the typo:
Code:
cd /tmp/target
ls *_*.txt | awk '
{x=$0;sub(/_.*\.txt$/,".txt",x);printf "[!]cp[/!] %s /tmp/source/%s\n",$0,x}
' | sh -v
 
Hi PHV..
Thanks a lot. I am getting below errors when i am executing the below script..


awk: syntax error near line 2
awk: illegal statement near line 2




cd /tmp/target
ls *_*.txt | awk '
{x=$0;sub(/_.*\.txt$/,".txt",x); printf "cp % s /tmp/source
/%s\n",$0,x}
' |sh -v

 
Replace this:
{x=$0;sub(/_.*\.txt$/,".txt",x); printf "cp % s /tmp/source
/%s\n",$0,x}
with this:
{x=$0;sub(/_.*\.txt$/,".txt",x); printf "cp % s /tmp/source/%s\n",$0,x}

i.e. remove the EndOfLine after source.
 
Hi PHV & Annihilannic,
Thanks a lot. The script is working. I changed awk to nawk.

Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top