Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 12-14-2007, 12:04 PM   #1
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.
Old 12-14-2007, 12:57 PM   #2
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927

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.

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

for i in `ls $*`
  out=`echo $i|sed 's/_m0*/_m/'`
  mv "$i" "$out"


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
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!

Old 12-15-2007, 11:43 AM   #4
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:
for i in `ls $*`
  out=`echo $i|sed 's/_m0*/_m/'`
  mv "$i" "$out"
  cp "$out" backup/
unless I am misinterpreting you.
Old 12-21-2007, 07:04 AM   #5
LQ Newbie
Registered: Dec 2007
Posts: 13

Original Poster
Rep: Reputation: 0

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!

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

Rep: Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681
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.
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}
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:
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.
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:34 PM.

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