Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 e.g. 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

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

My problem is the last rename line where I try to filter files other than xx_textfilex and rename. I tried different regular expressions but could not do that. I can select and rename the textfiles by:

rename 's/textfiles/'$f/ ~/test/ripmime/*

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

How can I do this?

share|improve this question
    
maybe mmv can do this. ss64.com/bash/mmv.html –  stijn Nov 29 '11 at 8:19
    
I can see how it can be done, but you need to provide some sample resulting file names. How should the PDF files in your current example be renamed after the command has been run? I find this part to be ambiguous at the moment: if they should be renamed as the input file as you say, that would result in multiple files with the same file name, which is not possible. –  Daniel Andersson Jun 1 '12 at 7:11
    
It looks like you are working in a temporary directory anyway; have you considered moving the files you want to keep out of the way, then using something like rename to rename the remaining files, then move the first set back to where they were? –  Michael Kjörling Jun 1 '12 at 8:20

2 Answers 2

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

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

share|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

You can combine ls and grep -v:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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