Find files that do not match specific patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find files that do not match specific patterns
# 1  
Old 01-26-2011
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
# 2  
Old 01-26-2011
Not too elegant, but this perl example should print a list of files that don't match your patterns:

Code:
#!/usr/local/bin/perl

$\ = "\n";

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

# 3  
Old 01-26-2011
Thanks for that - but I don't seem to have perl on the hosted server I am using - any other suggestions?
# 4  
Old 01-26-2011
Try this

Code:
#!/bin/sh

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


Last edited by SandmanCL; 01-26-2011 at 05:10 AM.. Reason: Typo: 'fi' should be 'done' on the last line
# 5  
Old 01-26-2011
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?
# 6  
Old 01-26-2011
replace "/path/to/yourdir" with whatever it should be and copy the script to clipboard

Code:
cat > script

paste content of clipboard and hit ctrl-d

Code:
chmod 755 script

and then run the script

Code:
./script

# 7  
Old 01-26-2011
when I execute the script I get: ./script: line 7: syntax error near unexpected token 'fi'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match patterns between two files and extract certain range of strings

Hi, I need help to match patterns from between two different files and extract region of strings. inputfile1.fa >l-WR24-1:1 GCCGGCGTCGCGGTTGCTCGCGCTCTGGGCGCTGGCGGCTGTGGCTCTACCCGGCTCCGG GGCGGAGGGCGACGGCGGGTGGTGAGCGGCCCGGGAGGGGCCGGGCGGTGGGGTCACGTG... (4 Replies)
Discussion started by: bunny_merah19
4 Replies

2. Shell Programming and Scripting

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

4. Shell Programming and Scripting

Insert a newline after match in files of specific name under some subdirectories?

Hi I'd like to add the newline: \tuser: nobody", or "<TAB>user: nobody to all files named: docker-compose.ymlin subfolders of pwd with names beginning with 10-20. Within these files, I'd like to find the line (there'll only be one) containing: command: celery workerNOTE: As far as... (2 Replies)
Discussion started by: duncanbetts
2 Replies

5. Shell Programming and Scripting

Find matched patterns in multiple files

Hi, I need help to find matched patterns in 30 files residing in a folder simultaneously. All these files only contain 1 column. For example, File1 Gr_1 st-e34ss-11dd bt-wwd-fewq pt-wq02-ddpk pw-xsw17-aqpp Gr_2 srq-wy09-yyd9 sqq-fdfs-ffs9 Gr_3 etas-qqa-dfw ddw-ppls-qqw... (10 Replies)
Discussion started by: redse171
10 Replies

6. UNIX Desktop Questions & Answers

Combining files with specific patterns of naming in a directory

Greetings Unix exports, I am facing some problems in combining files with different name patterns with a directory and I would appreciate if you can help me I have more than 1000 files but they follow a specific pattern of naming. e.g. 64Xtest01.txt They are divided into two sets of test and... (9 Replies)
Discussion started by: A-V
9 Replies

7. UNIX for Dummies Questions & Answers

Find diff between two patterns in two files and append

Hi, I'm a newbie at programming in Unix, and I seem to have a task that is greater than I can handle. Trying to learn awk by the way (but in the end, i just need something that works). My goal is to compare two files and output the difference between the two. I've been reading, and I think I... (5 Replies)
Discussion started by: legato22
5 Replies

8. UNIX for Dummies Questions & Answers

move files that match specific conditions

Hi all, I'm new to this forum and bash scripting. I have the following problem, I need to copy some files (from one dir. to another) whose first 5 numbers (subjects' ID) match the directory names. Here a shortened version of what I'm trying to do: names=(32983_f 35416_f 43579_f) # these are... (6 Replies)
Discussion started by: ada1983
6 Replies

9. Shell Programming and Scripting

Using AWK to match CSV files with duplicate patterns

Dear awk users, I am trying to use awk to match records across two moderately large CSV files. File1 is a pattern file with 173,200 lines, many of which are repeated. The order in which these lines are displayed is important, and I would like to preserve it. File2 is a data file with 456,000... (3 Replies)
Discussion started by: isuewing
3 Replies

10. Shell Programming and Scripting

script to match patterns in 2 different files.

I am new to shell scripting and need some help. I googled, but couldn't find a similar scenario. Basically, I need to rename a datafile. This is the scenario - I have a file, readonly.txt that has 2 columns - file# and name. I have another file,missing_files.txt that has id and name. Both the... (3 Replies)
Discussion started by: mathews
3 Replies
Login or Register to Ask a Question