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  
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  
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  
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  
With Perl:

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

# 5  
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  
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.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #898
Difficulty: Medium
The Toybox project was started in 2012 and became a BSD-licensed BusyBox alternative.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename multiple files in one go

OS : Oracle Linux 6.8 shell : bash As shown below, I have multiple files like below (query1-extract_aa, query1-extract_ab, query1-extract_ac, ....) $ ls -l total 235680 -rw-rw-r-- 1 reportusr reportusr 30M May 3 11:25 query1-extract_aa -rw-rw-r-- 1 reportusr reportusr 30M May 3 11:25... (5 Replies)
Discussion started by: kraljic
5 Replies

2. 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

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. UNIX for Dummies Questions & Answers

Rename Multiple Files

Hey guys, I am the definition of a newbie. I am in the process of trying to rip all my dvds onto a new HTPC I setup. While doing this, I am also trying to organize a bunch of other files I already have to proper naming conventions. So far I have just been naming each file separately (I am on a... (4 Replies)
Discussion started by: Ralze34
4 Replies

7. Shell Programming and Scripting

Rename multiple files

hello: I have multiple files with names like: somestring_y2010m01d01 somestring_y2010m01d02 .......... somestring_y2010m12d31 How... (4 Replies)
Discussion started by: sylcam
4 Replies

8. 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

9. 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

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