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 |
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 Cheers, Tink P.S.: I've removed the original thread-jacking post of yours and left this one open. |
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 |
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 |
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 |
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 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 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 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. |