The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
renaming file anushree.a Shell Programming and Scripting 3 09-30-2008 04:30 AM
renaming xls file systemsb UNIX for Dummies Questions & Answers 2 10-19-2007 07:50 AM
Help in renaming file !!! kumarsaravana_s UNIX for Dummies Questions & Answers 5 05-02-2007 12:45 PM
Renaming a file name dbrundrett Shell Programming and Scripting 2 01-06-2004 10:36 AM
Renaming a file to the same name lachino8 UNIX for Dummies Questions & Answers 3 07-31-2002 08:52 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-13-2008
larne larne is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 11
file renaming

How can I rename files named like these
Code:
iq - 000001 - 2008.07.31 - 14.49.47 - location1.bin
iq - 000001 - 2008.07.31 - 14.49.47 - location12.bin
iq - 000008 - 2008.07.31 - 14.52.01 - location500.bin
to
Code:
iq_2008.07.31_14.49.47_location1.bin
iq_2008.07.31_14.49.47_location12.bin
iq_2008.07.31_14.52.01_location500.bin
I know how to loose the spaces, but I cant figure out how to skip the 00000X part of the file names. I cant substitute it since I don't know what number will be there? And I cant really cut it since I don't know how long the filename is since location may vary, or can I? Any ideas?
Best regards
  #2 (permalink)  
Old 10-13-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,302
Try this:

Code:
ls -1 iq*.bin | awk '{print "mv "$0" "$1"_"$5"_"$7"_"$9}'
If the command is correct, pipe the result to sh:

Code:
ls -1 iq*.bin | awk '{print "mv "$0" "$1"_"$5"_"$7"_"$9}' | sh
Regards
  #3 (permalink)  
Old 10-13-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by Franklin52 View Post
Code:
ls -1 iq*.bin | awk '{print "mv "$0" "$1"_"$5"_"$7"_"$9}'

There's no need for -1 when the output of ls is not going to a terminal. In fact, there's no need for ls at all:

Code:
printf "%s\n" iq*.bin | awk '{printf  "mv \"$0\" \"%s_%s_%s_%s\"\n", $1, $5, $7, $9}'
(printf is a shell builtin in all the major shells.)
  #4 (permalink)  
Old 10-13-2008
treesloth treesloth is offline
Registered User
  
 

Join Date: Oct 2008
Location: Orem, Utah
Posts: 73
Quote:
Originally Posted by cfajohnson View Post
There's no need for -1 when the output of ls is not going to a terminal. In fact, there's no need for ls at all:
Is there a reason why

Code:
printf "%s\n" iq*.bin
is preferable to
Code:
ls iq*.bin
?
  #5 (permalink)  
Old 10-14-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

printf is built into all the major shells; ls is always an external command and therefore slower.

It is also overkill, since the shell expands the wildcard and sorts the file list.
  #6 (permalink)  
Old 10-14-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,086
Code:
echo "iq - 000001 - 2008.07.31 - 14.49.47 - location1.bin" |
nawk -F" - " '{
print $1"_"$3"_"$4"_"$5
}'
Code:
iq_2008.07.31_14.49.47_location1.bin
  #7 (permalink)  
Old 10-13-2008
larne larne is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 11
thank you very much for the clean solution
I just had to had add \" around "$0" since the file name contains spaces
Closed Thread

Bookmarks

Tags
rename files

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0