rename multiple files from search output


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers rename multiple files from search output
# 1  
Old 12-24-2008
rename multiple files from search output

Variations of multiple renames seems to come up a lot but i can't find the answer to this situation.
Tidying up a directory where people rename files to .working, .bob, .attempt1 & so on.

what i am trying to do is: list the file type, & rename from ".whatever" to .fixed.
As the ".whatever" is rarely the same, it makes this a bit more than the normal rename all ".a's" to ".b's" if you get my drift.

For this particular list of example files i have come up with:

for i in `ls WLAN_* | egrep -v 'TRF|fixed' | cut -d "." -f1`;do mv $i.* $i.fixed;done

which appears to work fine but a tad "around the houses". I was hoping someone knew a better way/shorter command string.
Example files as follows:

1.sh
bla1.txt
bla2.csv
WLAN_20081128_00051453_01.TRF
WLAN_20081128_00051468_01.TRF
WLAN_20081128_00051469_01.TRF
WLAN_20081128_00051470_01.TRF
WLAN_clo_20081202_00051984_01.sg-working2
WLAN_clo_20081129_00051540_01.sg-working
WLAN_clo_20081129_00051580_01.sg-working
WLAN_clo_20081202_00052011_01.still_failed
WLAN_clo_20080923_00043115.done
WLAN_20081128_00051467_01.TRF
WLAN_20081128_00051466_01.TRF
WLAN_20081128_00051465_01.TRF
WLAN_20081128_00051454_01.TRF
WLAN_20081128_00051455_01.TRF
WLAN_20081128_00051456_01.TRF
WLAN_20081128_00051457_01.TRF
WLAN_20081128_00051458_01.TRF
WLAN_20081128_00051459_01.TRF
WLAN_20081128_00051460_01.TRF
WLAN_20081128_00051461_01.fixed
WLAN_20081128_00051462_01.fixed
WLAN_20081128_00051463_01.TRF
WLAN_20081128_00051464_01.TRF
WLAN_clo_20081222_00054519_01.test1-sg

Desired output ( but this was using my command as above)

1.sh
bla1.txt
bla2.csv
WLAN_20081128_00051453_01.TRF
WLAN_20081128_00051468_01.TRF
WLAN_20081128_00051469_01.TRF
WLAN_20081128_00051470_01.TRF
WLAN_clo_20081202_00052011_01.fixed
WLAN_clo_20081129_00051580_01.fixed
WLAN_clo_20081202_00051984_01.fixed
WLAN_clo_20081222_00054519_01.fixed
WLAN_clo_20080923_00043115.fixed
WLAN_20081128_00051467_01.TRF
WLAN_20081128_00051466_01.TRF
WLAN_20081128_00051465_01.TRF
WLAN_20081128_00051454_01.TRF
WLAN_20081128_00051455_01.TRF
WLAN_20081128_00051456_01.TRF
WLAN_20081128_00051457_01.TRF
WLAN_20081128_00051458_01.TRF
WLAN_20081128_00051459_01.TRF
WLAN_20081128_00051460_01.TRF
WLAN_20081128_00051461_01.fixed
WLAN_20081128_00051462_01.fixed
WLAN_20081128_00051463_01.TRF
WLAN_20081128_00051464_01.TRF
WLAN_clo_20081129_00051540_01.fixed

# 2  
Old 12-24-2008
You can do something along the lines of :

Code:
ls WLAN_* | egrep -v 'TRF|fixed|.txt|.csv|.sh' | 
   while read file
      do 
         mv "$file" "${file%.*}".fixed
      done

Modify it to fit your needs.
# 3  
Old 12-27-2008
quite like that

Yep! quite like that. saves mucking around with a delimiter.
made a one liner out of it and worked fine.

ls WLAN_* | egrep -v '.fixed|.TRF'| while read file; do mv "$file" "${file%.*}".fixed; done

still quite long though. Just me being lazy i suppose. I like your use of the while loop.
# 4  
Old 12-27-2008
With Perl:

Code:
perl -e'
  map {($n = $_) =~ s/[^.]+$/fixed/; rename $_, $n}
    grep !/\.(csv|sh|txt|TRF|fixed)$/, glob "*.*"
    '

# 5  
Old 12-28-2008
Quote:
Originally Posted by rubin
You can do something along the lines of :

Code:
ls WLAN_* ...

Modify it to fit your needs.
Posting back my learnings from here Smilie

ls WLAN* is not needed - there is no need for a process to be spawned here just shell construct is sufficient here

Code:
for file_name in WLAN_*

https://www.unix.com/shell-programmin...g-loops-2.html

Thanks to cfajohnson !
# 6  
Old 01-06-2009
Thanks for your suggestions folks. Handy forum. I've not tried perl before but will have a read up. I think for the time being i will stick with the "for i" & "while" methods for this though.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

2. Shell Programming and Scripting

How to rename multiple files at one go?

Hi, I have hundreds of files with XXX in their file name and I want to rename all of them with YYY in place of XXX. for ex: $ ls -1 123XXX789 345XXX678 Output $ ls -1 123YYY789 345YYY678 I know we can loop in each file and sed to replace and rename each file but ren *XXX* *YYY*... (4 Replies)
Discussion started by: reddyr
4 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Search, Extract and Rename Multiple Files

Hi, I want perl script for the below requirement. We have lot of files like below name in the directory 750464921-RE-file2.csv 750452173-RE-file1.csv 750385426-RE-file3.csv 750373470-RE-file4.csv And also we have another file as "Group.csv" in the same directory as per the below format... (9 Replies)
Discussion started by: armsaran
9 Replies

4. Shell Programming and Scripting

Script to unzip files and Rename the Output-files

Hi all, I have a many folders with zipped files in them. The zipped files are txt files from different folders. The txt files have the same names. If i try to find . -type f -name "*.zip" -exec cp -R {} /myhome/ZIP \; it fails since the ZIP files from different folders have the same names and... (2 Replies)
Discussion started by: pmkenya
2 Replies

5. Shell Programming and Scripting

Using a single "find" cmd to search for multiple file types and output individual files

Hi All, I am new here but I have a scripting question that I can't seem to figure out with the "find" cmd. What I am trying to do is to only have to run a single find cmd parsing the directories and output the different file types to induvidual files and I have been running into problems.... (3 Replies)
Discussion started by: swaters
3 Replies

6. Shell Programming and Scripting

Rename the multiple files

Hi I need to reanme the multiple file using unix script I have multiple file like: sample_YYYYMMDD.xls test new_YYYYMMDD.xls simple_YYYYMMDD.xls I need to rename this file sample.xls testnew.xls SIMPLE.xls thanks (8 Replies)
Discussion started by: murari83.ds
8 Replies

7. UNIX for Dummies Questions & Answers

How to rename multiple files

Hi all, I have some files like: pickup.0000043200.t001.t001.data pickup.0000043200.t001.t002.data pickup.0000043200.t002.t001.data pickup.0000043200.t002.t002.data pickup.0000043200.t003.t001.data pickup.0000043200.t003.t002.data I need to rename these files to ... (4 Replies)
Discussion started by: a_dor8
4 Replies

8. Shell Programming and Scripting

now to rename multiple files

I have several hundred files in one directory which I need to move to another directory with the new extension, for example: /bb/data/rptmgr* are in the source directory need to be moved to /bb/data55/rptmgr*.new Is there an efficient way to do it? Thanks -A (4 Replies)
Discussion started by: aoussenko
4 Replies

9. Shell Programming and Scripting

rename multiple files

Hi, can anyone have a ksh script to rename multiple files (ie to remove .Z extension of the files) can someone correct this? for i in *.Z do var1 = substr($i, 1,at(".Z",$i)-1) mv $i $var1 done Thanks.. Antony (13 Replies)
Discussion started by: antointoronto
13 Replies

10. UNIX for Dummies Questions & Answers

Rename multiple files

Hello, I want to rename multiple files at a time and I don't know how to do it. I have various ".mp3" files, like "band name - music name.mp3" and I want to remove the "band name" from all files. Anybody knows how to do it using shell script or sed or even perl? Thanks (7 Replies)
Discussion started by: luiz_fer10
7 Replies
Login or Register to Ask a Question