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:

#!/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 files 

8   rename 's/ /_/g' ~/test/ripmime/*

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

How can I do this?

link|improve this question
maybe mmv can do this. ss64.com/bash/mmv.html – stijn Nov 29 '11 at 8:19
feedback

2 Answers

I use https://gist.github.com/995151

rename 's/\.bak$//' *.pdf

link|improve this answer
I just don't want to rename *.pdf files as I don't know what formats they could be in. Therefore just want to rename all the files as input file that ARE NOT xx_textfilex. I used rip mime to extract mails and attachments from MIME formatted mails. Do you have some thing in mind for that? – Atul Kakrana Nov 29 '11 at 16:15
feedback

You can combine ls and grep -v:

ls | grep -v ".*textfile.*" | while read filename; do
  # rename $filename to something
done
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.