Batch rename files that do not match pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Batch rename files that do not match pattern
# 1  
Old 11-29-2011
Batch rename files that do not match pattern

Hi all,

I do not have programming experience and mostly use one-liner or sometimes more to get the job done. I am having problem to batch rename the files that do not match a particular pattern.

Sample file-names from directory:

Meeting_Packages.pdf 13_textfile0 19_textfile0 23_textfile0 29_textfile0 33_textfile1 45_textfile0 5_textfile3 Membership.pdf 13_textfile1 19_textfile1 23_textfile1 29_textfile1 34_textfile0 46_textfile0 6_textfile0 xyz2009.pdf 13_textfile2 19_textfile2 23_textfile2 29_textfile2 34_textfile1 47_textfile0 6_textfile1 meeting.ics

I want to rename the files i.e Meeting_Packages.pdf, Membership.pdf, meeting.ics and xyz2009.pdf to the file from where they came (input file). Actually it is output of 'ripmime' with mails as xx_textfilex and others are attachments. I want to name attachments as the original input file

My Code:

Code:
#!/bin/bash

1 FILES=*.mime
2 for f in $FILES
3 do
4	echo "Processing $f"
5      #rip mails into attachments and text files also add a prefix to text files
6      ripmime -i $f -d ~/test/ripmime --prefix 
7	#Remove white spaces from output files	
8	rename 's/ /_/g' ~/test/ripmime/*
9	#rename output attachments as original input files	
10	rename 's/\[^0-9]/'$f/ ~/test/ripmime/*     
11	
12 done

My problem is line-10 where I try to filter files other then xx_textfilex and rename. I tried different REGEX could not do that. I can select and rename the textfiles by:
rename 's/textfiles/'$f/ ~/test/ripmime/*

But I need inverse of that and rename files other then textfiles.

Need community help.

Atul

Last edited by Franklin52; 11-30-2011 at 03:16 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 11-29-2011
Welcome to the forum. Please use code tags when posting code or sample in/output.
I would make a shell (bash) loop and do a regex check on the filename for each file. Like this:
Code:
for i in * ; do 
  if [[ "$i" =~ "textfile" ]] ; then 
     echo keep $i ; 
  else 
    echo mv $i  ${i}.old 
  fi 
done

But you could also do quicker, if all your keepers start with a number:
Code:
rename 's/$/.old/' [^0-9]*  #append '.old' to all files that do not start with a number

Also, bash provides extended globbing. Check whether you have this enabled:
Code:
$ shopt  | grep extglob
extglob            on

If it's off, turn it on by
Code:
$ shopt -s extglob

Then, you can do something like:
Code:
 $ ls !(*text*)  #matches all files that do not contain the string "text".

See man bash for more info.

Last edited by mirni; 11-29-2011 at 03:44 AM..
# 3  
Old 11-29-2011
Hello Mirni,

Thanks for replying. But I am still having problems.

Code:
#!/bin/bash

FILES=*.mime
for f in $FILES
do
	echo "Processing $f"
        ripmime -i $f -d ~/test/ripmime --prefix 
	#Remove white spaces from output files	
	rename 's/ /_/g' ~/test/ripmime/*
	#rename output file if it does not start with digit	
	rename "s/$/$f/" [^0-9]* ~/test/ripmime/*
done

It renames all the files (not just files other than xx_textfilex) in directory and that too multiple times. I mean adds '$f' multiple times to a file name.

i.e Original output file Membership.pdf renamed to
Membership.pdf1296999601.M436638P3547Q16.cybercom.mime1296999622.M603070P3547Q17.cybercom.mime129699 9622.M852205P3547Q18.cybercom.mime1296999623.M313555P3547Q20.cybercom.mime1296999623.M45531P3547Q19. cybercom.mimemime.mimeripmime_batch.sh~ where as it has to be just 1296999622.M603070P3547Q17.cybercom.mime.pdf or better if 1296999622.M603070P3547Q17.pdf


I know its because of 'rename' command. But dont know how to fix it.

ATul

Last edited by atulkakrana; 11-29-2011 at 09:05 PM..
# 4  
Old 11-30-2011
Your problem is in this line:
Code:
    rename "s/$/$f/" [^0-9]* ~/test/ripmime/*

You are telling rename to append $f to all files that don't start with a digit AND all files in ~/test/ripmime directory.
Moreover, this command is run at every iteration of the for loop! That's why you end up with that mess.

Try to understand the 's///' syntax. Dollar sign in the regex pattern is an anchor and means 'the end of the string'. So you are appending, not renaming.
Also, rename has a very useful -n switch, which will just print what it would do without actually renaming (dummy run). Great for debugging.

Since you already have a for loop there, why not just use good old 'mv' to rename the oddballs? Like:
Code:
#!/bin/bash

outdir=~/test/ripmime
for f in *.mime ; do
    echo "Processing $f"
        ripmime -i $f -d $outdir --prefix 
    #rename output file if it does not start with digit
     m="" ; cnt=1   #for multiple odd files    
    for odd in ${outdir}/[^0-9]* ; do     
        newname=${f/.cybercom.mime/}.${odd##*.}$m
        mv "$odd" "$newname"
        m=.$((cnt++))
    done
done

#Remove white spaces from output files
rename 's/ /_/g' ${outdir}/*

The ${f/pattern/new} is the same as
Code:
echo $f | sed 's/pattern/new/'

which means it will replace 'pattern' with 'new', in this case it will get rid of the '.cybercom.mime' part of the $f.

The ${odd##*.} is the part of the $odd after the last dot. (the extension). See 'parameter expansion' in 'man bash' for details on these.

The 'm' and 'cnt' variables I added to differentiate the filenames, in case there are more than one files that don't start with number. If you did not have these, you would loop through those files and rename them all to the same thing, so you would lose all but the last one.

When debugging scripts, echo the output of the vars so that to make sure they are what you think they are.
This User Gave Thanks to mirni For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename files to match file list pattern

Hi All, I have 100 folders with the first delimiter has a unique name i.e (123_hello and 575_hello) and each folder have atlist 1000 plus files with naming convention i.e (575_hello_1.iso ... 575_hello_1000.iso). 575_hello/575_hello_1.iso 575_hello/575_hello_2.iso 575_hello/575_hello_3.iso... (8 Replies)
Discussion started by: lxdorney
8 Replies

2. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

3. Shell Programming and Scripting

Pattern match using grep between two files

Hello Everyone , I have two files. I want to pick line from file-1 and match with the complete data in file-2 , if there is a match print all the match lines in file 3. Below is the file cat test1.txt vikas vikasjain j ain testt douknow hello@vik@ # 33 ||@@ vcpzxcmvhvdsh... (1 Reply)
Discussion started by: mailvkjain
1 Replies

4. Shell Programming and Scripting

Executing a batch of files within a shell script with option to refire the individual files in batch

Hello everyone. I am new to shell scripting and i am required to create a shell script, the purpose of which i will explain below. I am on a solaris server btw. Before delving into the requirements, i will give youse an overview of what is currently in place and its purpose. ... (2 Replies)
Discussion started by: goddevil
2 Replies

5. Shell Programming and Scripting

Batch rename files after internal search pattern

Hello all I am trying to do a script that would allow me to mass rename files after a grep search within them. They are XML files and the pattern I would like to append to the file name is easy to find (e.g. <filename>foo</filename>), but I can't for the life of me find out how to do it and... (2 Replies)
Discussion started by: aavv
2 Replies

6. UNIX for Dummies Questions & Answers

finding all files that do not match a certain pattern

I hope I'm asking this the right way -- I've been sending out a lot of resumes and some of them I saw on Craigslist -- so I named the file as 'Craigslist -- (filename)'. Well I noticed that at least one of the files was misspelled as 'Craigslit.' I want to eventually try to write a shell... (5 Replies)
Discussion started by: Straitsfan
5 Replies

7. Shell Programming and Scripting

Remove lines from batch of files which match pattern

I need to remove all lines which does not match the pattern from a text file (batch of text files). I also need to keep the header line which is the first line of the file. Please can you provide an example for that. I used this to solve half of my work. I was unable to keep the first line of... (3 Replies)
Discussion started by: talktobog
3 Replies

8. Shell Programming and Scripting

search files which doesnot match pattern ?

Hi I need a command to search files in a directory which does not match with pattern .. Plz send me this Ex : Test is directory and has some 10 files with different name all are with *.dat extension , need to search files which doesnot contain word "Dummy file". Thanks (6 Replies)
Discussion started by: madankumar
6 Replies

9. Shell Programming and Scripting

Counting files in a directory that match a pattern

I have 20 files in a direcotry like BARE01_DLY_MKT_YYYYMMDD. The MKT differes for all these files but the remaining syntax remains the same for a particular day. If I am checking for today I need to make sure that there are 20 files that start with BARE01_DLY_MKT_20060720. How can I write a... (31 Replies)
Discussion started by: dsravan
31 Replies

10. Shell Programming and Scripting

rename in batch

example test1 will have m1234567.12a I would like to rename in batch but I don't Please help me on this. cd /a1/a2/a3 test1=$(basename /a1/a2/a3/*.*) >> /tmp/t echo $test1 echo "Extracting 8 th position" >> /tmp/t2 awk '{print substr($1,8,1); }' $test1 >> /tmp/t3 echo "extraction ... (3 Replies)
Discussion started by: kathy18
3 Replies
Login or Register to Ask a Question