LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   renaming multiple files (https://www.linuxquestions.org/questions/linux-newbie-8/renaming-multiple-files-606802/)

yanchina12 12-14-2007 12:04 PM

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

Tinkster 12-14-2007 12:57 PM

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.

yanchina12 12-15-2007 05:44 AM

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

xlq 12-15-2007 11:43 AM

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.

yanchina12 12-21-2007 07:04 AM

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

jschiwal 12-21-2007 07:56 AM

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.


All times are GMT -5. The time now is 08:33 AM.