Find files that do not match specific patterns

Hi all,

I have been searching online to find the answer for getting a list of files that do not match certain criteria but have been unsuccessful.

I have a directory that has many jpg files. What I need to do is get a list of the files that do not match both of the following patterns (I have used the ? to act as any single character-in my case these are stricly numbers):

???-??-????.jpp AND ???-??-????-?mi.jpg

I would like to output the result to a text file.

Once I have this list it would be very helpful if anyone could propose an easy way to rename the files that to not match this pattern.

many thanks

Not too elegant, but this perl example should print a list of files that don't match your patterns:

#!/usr/local/bin/perl

$\ = "\n";

@files = split/\n/,`ls /path/to/your/dir`;
foreach (@files) {
  if (!/^...-..-....\.jpp|^...-..-....-.mi\.jpg/) {
    print;
  }
}

Thanks for that - but I don't seem to have perl on the hosted server I am using - any other suggestions?

Try this

#!/bin/sh

for file in `ls /path/to/yourdir`; do
  if [ -z `"echo $file | egrep '^...-..-....\.jpp|^...-..-....-.mi\.jpg'"` ]; then
    echo $file
  fi
done

excuse my ignorance - but I presume I would have to pass your code into a file and then execute it - how would I go about doing this?

replace "/path/to/yourdir" with whatever it should be and copy the script to clipboard

cat > script

paste content of clipboard and hit ctrl-d

chmod 755 script

and then run the script

./script

when I execute the script I get: ./script: line 7: syntax error near unexpected token 'fi'

find . -type f  -a \! -name "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9].jpp" -a \! -name "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9]mi.jpg" -print > outfile

What would you like to rename them from/to ?

Cheers...

Sorry, there was a typo in the script. The last line should be "done" instead of "fi". I've updated the code in post #4

[LEFT]@Sandman

When I run the script it returns
./script: line 4: echo 025-09-0004.jpg | egrep '^...-..-....\.jpp|^...-..-....-.mi\.jpg': command not found

for multiple files - I am presuming this is not working correctly
[/LEFT]

Another option:
man ls

ni2@hse:~$ ls
cookie.txt  testing.tar  login.php

ni2@hse:~$ ls --hide="*.tar"
cookie.txt  login.php

HTH

@citaylor - thanks for your replay however from what I see it created a file with all the files in the directory that match that partern. I see that is also continued into the child direcories - I am only interested in finding the files in the current directory.

how can we stop the find from executing recursively into the child directories?

find . -maxdepth 1 -type f  -a \! -name "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9].jpp" -a \! -name "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9]mi.jpg" -print > outfile

@citaylor - thanks for the tip on the non-recursive however the output file contains all matching filesname - I have attached it for your reference

Im afraid, that output has no files that match your criteria - maybe the typo of ".jpp" in your original request should have been ".jpg" ?

@citaylor - thanks very much for your help - that worked like a charm! :slight_smile:

Now that I have a list of these files is there someway I could create a pairing file which I could execute and have all the files renamed as would specified by the pairing file?

certainly...could you describe what you want renamed from what to what ?

this is bad. do not shell out unless you absolutely have to. In Perl , you can use

while (<*>){
   ....
}

to display files

---------- Post updated at 08:38 AM ---------- Previous update was at 08:35 AM ----------

another bad example. Don't use ls to parse files. use the shell

for file in *
do
  ....
done

@citaylor - well i would ideally like to be able to create a flat file in the form of:

[name of file to rename] | [new file name]

and then execute some sort of script that will apply the renaming to the files that exist in a specific folder.

again many thanks for your help

Is there a predefined list of these renames, or is there an algorithm we could use to know what to rename the files to ? The latter is easier, but the former isnt difficult either...