|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Renaming mutiple files with hyphens in name
I have searched throught a host of threads to figure out how to rename mutiple files at once using a script.
I need to convert 200+ files from: fKITLS_120605-0002-00001-000001.hdr to eStroop_001.hdr fKITLS_120605-0002-00002-000002.hdr to eStroop_002.hdr and so forth.... What is throwing me off is the hyphens (-) in the name. Any help would be greatly appreciated. Thanks |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
cd to the directory that holds the files Code:
rm mvcmds.sh
for filename in `ls -1|grep ^fK`
do
NewStr="eStroop_"
OldStr=`echo $filename|awk -F"." '{print substr($1,length($1) -2, length($1))}'`
NewName=""$NewStr""$OldStr"".hdr""
echo "mv $filename $NewName" >> mvcmds.sh
doneIf you like the mv commands that are generated in the file mvcmds.sh, run it. Hope this helps |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Assuming the dash-delimited field before the dot is always 6 characters wide: Code:
for f in *.hdr; do
mv "$f" eStroop_"${f##*-???}"
doneRegards, Alister |
|
#4
|
|||
|
|||
|
Command works lovely.
Thanks again for the help. |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
That will work with the filenames given in this thread, but in general it's a very poor practice. Any filename containing an IFS character (by default a space, tab, or newline) would be broken into pieces after the command substitution, with each piece served up individually to a different loop iteration. The -1 flag to ls is pointless when piping to another process. ls knows when it's not writing to a tty and will always output one file per line (although if a filename itself contains a newline, it's not possible to know where it ends). Since pathname expansion happens after field splitting (so that there's no need to worry about what the filename may contain) and since there's no need for multiple fork-execs to create the ls-grep pipeline, a safe and more efficient alternative to your approach would be: Code:
for filename in fK* Regards, Alister P.S. And, yes, I agree. Anyone using newlines and tabs in filenames deserves what they get.
|
| The Following User Says Thank You to alister For This Useful Post: | ||
mvijayv (03-07-2011) | ||
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Thanks Alister .. dont know what started me off with that approach a long time ago .. but never too late to get over a bad habit ... Good info, Thanks!
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| copy mutilple files to mutiple folders | M474746 | UNIX for Dummies Questions & Answers | 4 | 01-10-2011 11:20 AM |
| Mutiple For loops - moving files to another directory | tekster757 | Shell Programming and Scripting | 9 | 06-21-2007 08:42 AM |
| Sending mutiple files thru email to lotus notes | lapisguy | Shell Programming and Scripting | 4 | 04-16-2007 06:28 PM |
| removing mutiple files | run_time_error | Shell Programming and Scripting | 9 | 05-27-2005 07:40 PM |
| Report with mutiple files. | Cameron | Shell Programming and Scripting | 4 | 01-28-2002 07:44 PM |
|
|