Script to list non matching files using pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script to list non matching files using pattern
# 1  
Old 11-16-2011
Script to list non matching files using pattern

Hi,

I am trying to write a script that list down all the files that do not match the pattern

My pattern will be like this "*.jpg|*.xml|*.sql". This pattern will be stored in a file. The script need to read this pattern from the file and delete the files that does not match this pattern. It has to scan all the files under the current directory

I am quiet new to unix scripting and I need help for this.
# 2  
Old 11-16-2011
egrep supports extended regular expressions, which lets you do things like this:
Code:
ls | egrep -v "\.(jpg|xml|sql)$"

The \. matches one literal "." character, the (jpg|xml|sql) matches jpg, xml, or sql, and the $ tells grep the expression has to be at the end of the line.

-v inverts the results, so that you get all the results which don't match the pattern.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-16-2011
Quote:
Originally Posted by Corona688
egrep supports extended regular expressions, which lets you do things like this:
Code:
ls | egrep -v "\.(jpg|xml|sql)$"

The \. matches one literal "." character, the (jpg|xml|sql) matches jpg, xml, or sql, and the $ tells grep the expression has to be at the end of the line.

-v inverts the results, so that you get all the results which don't match the pattern.
Thanks for the command and it works fine. I made small change so that I can get the full file path

find .| egrep -v "\.(jpg|xml|sql|txt)$"

Is there any way to suppress the display of directory names? Even though there are no files displays the command simply display all the directory names.
# 4  
Old 11-16-2011
how about:
Code:
find . -type f | egrep -v "\.(jpg|xml|sql|txt)$"

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 11-17-2011
Quote:
Originally Posted by vgersh99
how about:
Code:
find . -type f | egrep -v "\.(jpg|xml|sql|txt)$"


Hey this thread rocks!!! Getting response so fast. Thanks a lot

One final help. I need to move the files to a destination directory.

eg move this ./.libreoffice/3/user/gallery/sg30.sdv /tmp/backup

But i need to create the same directory structure under /tmp as /tmp/.libreoffice/3/user/gallery and move the file under this folder.Is there any option in mv command for this? I searched but I couldnt find one

---------- Post updated 11-17-11 at 03:38 AM ---------- Previous update was 11-16-11 at 08:58 PM ----------

I managed to build the code to get the parameters from the config file

The content of the config file is this

Code:
BACKUP_FOLDER=/root/test/tmp
EXT_PATTERN=jpg|xml|sql|log
VERIFICATION_DIR=/root/test

Code:
for i in `cat test.conf`
do
key=`echo $i | cut -f1 -d'='`
if [ "$key" == "EXT_PATTERN" ]; then
        pattern=`echo $i | cut -f2 -d'='`
fi;
if [ "$key" == "BACKUP_FOLDER" ]; then
        backupfolder=`echo $i | cut -f2 -d'='`
fi;
if [ "$key" == "VERIFICATION_DIR" ]; then
        vdir=`echo $i | cut -f2 -d'='`
        echo $vdir
        echo $pattern
        echo "There are the unwanted files present under " $vdir
        echo ""
        find $vdir -type f | egrep -v '\.($pattern)$'
        echo ""
        echo "Do you wish to move these files into backup folder?:"
        read resp
        if [ "$resp" == "y" ]; then
                find  $vdir -type f | egrep -v '\.($pattern)$' |
                while read each; do
                        cp $each $backupfolder
               done
        fi
fi
done

I've provided the pattern in the file as xml|jpg|log and when I execute this script the parameter is not getting replaced with the value I fetched from the config file.

Any idea how this can be done?


Moderator's Comments:
Mod Comment How to use code tags

Last edited by meenavin; 11-17-2011 at 05:55 AM.. Reason: Code tags
# 6  
Old 11-17-2011
try mv -R
# 7  
Old 11-17-2011
It is not that complex. The folowing works perfect on bash.

Code:
ls !(*.jpg|*.xml|*.sql)

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help with pattern matching to list missing packages

I have wrote this small program to check for installed and missing packages in LINUX (RHEL x86_64) For now it perfectly prints all the installed and missing packages however I want that it loops through to the end (in either case) and list all packages however if one or more packages are... (5 Replies)
Discussion started by: Dazzler74
5 Replies

2. Shell Programming and Scripting

awk script issue redirecting to multiple files after matching pattern

Hi All I am having one awk and sed requirement for the below problem. I tried multiple options in my sed or awk and right output is not coming out. Problem Description ############################################################### I am having a big file say file having repeated... (4 Replies)
Discussion started by: kshitij
4 Replies

3. Shell Programming and Scripting

Help with Pattern Matching and replacement in Gz files

Hi Techies, I need a help in finding junk characters and remove them from a Datafile. we have a file and it had crores of records like below SGSN_MCC_MNC=01150 but sometime due to the issue with sending server we are getting some junk characters in the middle of data like below ... (6 Replies)
Discussion started by: mahi_mayu069
6 Replies

4. Shell Programming and Scripting

Removing files matching a pattern

I am on ubuntu 11.10 using bash scripts I want to remove all files matching a string pattern and I am using the following code find . -name "*$pattern*" -exec rm -f {} \;I have encountered a problem when $pattern is empty. In this case all my files in my current directory were deleted. This... (3 Replies)
Discussion started by: kristinu
3 Replies

5. Shell Programming and Scripting

Help me to find files in a shell script with any matching pattern

Hi friends.. I have many dirs in my working directory. Every dir have thousands of files (.jsp, .java, .xml..., etc). So I am working with an script to find every file recursively within those directories and subdirectories ending with .jsp or .java which contains inside of it, the the pattern... (3 Replies)
Discussion started by: hnux
3 Replies

6. Homework & Coursework Questions

shell script that can create, monitor the log files and report the issues for matching pattern

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write an automated shell program(s) that can create, monitor the log files and report the issues for matching... (0 Replies)
Discussion started by: itian2010
0 Replies

7. UNIX for Dummies Questions & Answers

find files NOT matching name pattern

Hi, I have following files in my directory: /TESTDONTDEL> ls -alt total 14 drwxr-xr-x 2 oracle dba 1024 May 15 06:30 . -rw-r--r-- 1 oracle dba 40 May 15 06:30 exception.txt -rw-r--r-- 1 oracle dba 19 May 15 06:22 ful_1234_test1.txt -rw-r--r-- 1... (2 Replies)
Discussion started by: sagarparadkar
2 Replies

8. UNIX for Dummies Questions & Answers

to break a file into 2 files after matching a pattern.

Hi, i need to break a file into 2 files afetr matching a pattern for ex. there is a fil, file .txt which contains here i need to look for mat $ demon if it matches then i need to transfer the data into another file till the line in which a "d6s" comes,and i have to delete tat line... (3 Replies)
Discussion started by: manit
3 Replies

9. UNIX for Dummies Questions & Answers

Find files matching a pattern

Hi, I am writing a BASH shell script. I would like to count all the files in the CURRENT directory matching a specific pattern. Could someone suggest the best/simplest way to do this. I have thought of these solutions (for simplicity the pattern is all files starting with A): ls -1 *A | wc -l... (5 Replies)
Discussion started by: msb65
5 Replies

10. UNIX for Dummies Questions & Answers

rm core files and pattern matching

Hi, I am trying to delete a load of core files, but make sure I only delete core files. The system I am using has many files with core in the name, so I obviously can not simply search for "core". I have tried using the 'find' command with pattern matching via , and know that his is the way... (3 Replies)
Discussion started by: littleIdiot
3 Replies
Login or Register to Ask a Question