LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-14-2007, 12:04 PM   #1
yanchina12
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Rep: Reputation: 0
renaming multiple files


I've got several files with names like u90021_dw_m001.txt, u90021_dw_m011.hdr etc in a UNIX directory. I would like to rename them to u90021_dw_m1.txt and u90021_dw_m11.hdr respectively and copy them within a folder as a backup. So basically I would like to remove 'one/two zeros' immideately after 'v'without changing anything else and make a copy of all the renamed files with a folder. Your help will be highly appreciated! I'm a sociologist, so your help will be appreciated.
Regards,
Yan
 
Old 12-14-2007, 12:57 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Hi,

And welcome to LQ!

That's a job for a script, I guess. None of the "renaming tools" I
know will do that kind of manipulation out of the box. But I'm
curious - why would you want to do that? It will screw up the
sort order all together.

[edit]
I'll igve you a script solution anyway; I just recommend
against doing what you're up to.

Code:
#/bin/bash
IFS='
'
for i in `ls $*`
do
  out=`echo $i|sed 's/_m0*/_m/'`
  mv "$i" "$out"
done
[/edit]


Cheers,
Tink


P.S.: I've removed the original thread-jacking post of yours and
left this one open.

Last edited by Tinkster; 12-14-2007 at 02:46 PM. Reason: [edit][/edit]
 
Old 12-15-2007, 05:44 AM   #3
yanchina12
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Original Poster
Rep: Reputation: 0
renaming multiple files

Many Thanks for your help. I need to feed the files to another programe. This programme can't read if there is a '0' or '00' immidiately after m, thats why.

Just wondering your scripts will remove one or two '0' immidiately after the m not after another digit! For example it will rename the file 'u90021_dw_m010.txt' to 'u90021_dw_m10.txt'. Am I right?(If it renmaes to 'u90021_dw_m1.txt', then it won't work for my programme). If it does so, great! I also need to make a copy of all the rename files withinn folder called 'backup'.



Your help is much appreciated!



Regards,
Yan
 
Old 12-15-2007, 11:43 AM   #4
xlq
Member
 
Registered: Feb 2007
Distribution: Slackware 12.0
Posts: 58

Rep: Reputation: 15
Yes, it only matches zeroes immediately after '_m'.
I would usually cram that code onto one line in bash :P

If you want to copy all the files into the backup dir:
Code:
#/bin/bash
for i in `ls $*`
do
  out=`echo $i|sed 's/_m0*/_m/'`
  mv "$i" "$out"
  cp "$out" backup/
done
unless I am misinterpreting you.
 
Old 12-21-2007, 07:04 AM   #5
yanchina12
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Renaming

Hello ,

Thanks for your help. It works fine. Just two things, I would like to copy only the renamed files on the folder, not all the files. But currently it copies everything. I addaed one line of code as well 'mkdir backup'? So everytimes it creates backup folders automatically. Your help is much appreciated!Happy X'mas and new year!

Regards,
Yan
 
Old 12-21-2007, 07:56 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You can limit which files match
Also, if you use bash you can enable the extglob option. Then you don't need sed at all.
Code:
#!/bin/bash 
DESTDIR=thetargetdirectory
shopt -s extglob

# create the target directory if it doesn't exits
if [ ! -d "${DESTDIR}" ]; then mkdir "${DESTDIR}"; fi

# for every file with an _m0*.txt suffix, remove leading zeros.
for i in *_m+(0)*.txt ; do 
  cp $i "${DESTDIR}"/${i/_m+(0)/_m}
done
If you have tens of thousands of files matching this pattern, you will need to use the find command to produce the filelist instead of file globbing or ls.

If you have a more complicated pattern to match, you could use sed to provide the initial file list:
Code:
for i in $(ls *.txt | sed -n '/<pattern>/p'); do
...
Sometimes, having a complicated pattern to match, you want to either use echo before the "cp" command to debug your script, or instead of executing commands, use sed to construct a script.
I did something similar yesterday where I used a directory list of your MVL server, extracted spot numbers into a list (with "cut").
Using cut, i got a list of spot numbers from a range of schedules. Then I produce another list of spot numbers from the adtec units and produced two files of just spot numbers.
Code:
comm -13 <(sort -u scheduledspots) <(sort inventory) | sed 's/.*/DEL &.MPG/' >ftpscript
A oneliner produced an ftp script to delete files in the units that weren't in the schedules.
I could then look at this script and proof it to make sure I wasn't deleting scheduled files.

Last edited by jschiwal; 12-21-2007 at 08:18 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
renaming multiple files linuxhippy Slackware 10 01-21-2011 01:53 AM
Renaming multiple files ajcns Linux - Newbie 2 01-19-2007 11:04 PM
renaming multiple files in general kkatebian Linux - General 5 11-06-2005 11:05 AM
Renaming multiple files with the same format Shr00mBoXx Slackware 7 06-20-2004 07:22 PM
Solution: renaming multiple files with spaces HawkeyeCoug Linux - Newbie 0 03-26-2004 10:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration