LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-31-2012, 11:56 PM   #1
james jackson
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Rep: Reputation: Disabled
bash shell script filename matching patterns


i would like to know how to check a filename against patterns in a directory
for example
for entry in *;do
if [-f "entry"] then # i want to check it against the patterns
echo file $file match pattern
else
echo file did not match any of the patterns
 
Old 08-01-2012, 12:01 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,519

Rep: Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896
Look up your favourite bash resource and search for either case or =~ operator
 
Old 08-01-2012, 12:03 AM   #3
james jackson
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
can you please elaborate
 
Old 08-01-2012, 12:09 AM   #4
kauuttt
Member
 
Registered: Dec 2008
Location: Kolkata, India
Distribution: Ubuntu
Posts: 123

Rep: Reputation: 25
Do you want to check a single pattern..or you have multiple pattern to check within the directory?
It will be nice if you elaborate your question in a better way!
 
Old 08-01-2012, 12:13 AM   #5
james jackson
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
multiple pattern example when i type from the command line it would be remove /home "*.c" "*.ex" "*.fi"
 
Old 08-01-2012, 12:54 AM   #6
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 279

Rep: Reputation: Disabled
With this code in mind;
Code:
find -regex ".*\(c\|txt\|png\)$" -delete
James' posts is very vague but I think what he is trying to say is that he wants to pass args from the command line to a script. So, instead of specifying the extensions in the script and having to edit the script each time he wants to change regex args, he wants to pass them as CLI args.

I provided a solution in his previous post where he wanted to remove files with specific extensions. He had a similar question there.

Find original post here -> http://www.linuxquestions.org/questi...es-4175419261/

Last edited by amboxer21; 08-01-2012 at 03:09 AM.
 
Old 08-01-2012, 01:07 AM   #7
james jackson
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
i guess i need to do a better job explaining the problem. i going to check in a directory, if what i found is a file im going to check that file with patterns(could be 1 or multiple) if the file match with the pattern i going to print it out

for entry in *; do
if [[-f "entry]]then
check the file against the pattern
if its a match print the file
else echo file is not a match

sorry for the misunderstanding.
 
Old 08-01-2012, 03:08 AM   #8
kauuttt
Member
 
Registered: Dec 2008
Location: Kolkata, India
Distribution: Ubuntu
Posts: 123

Rep: Reputation: 25
Code:
#!/bin/sh

for filename in *;
do
    if [ -f ${filename} ]
    then
        file_ext=$(echo $filename | awk -F . '{if (NF>1) {print $NF}}')
        file_ext2=${filename##*.}
        
        echo ${file_ext}
        echo ${file_ext2}
    fi
done
This will output the extension of every file present in the cirrent directory.
Hope it helps!
 
Old 08-01-2012, 04:46 AM   #9
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 279

Rep: Reputation: Disabled
Quote:
Originally Posted by kauuttt View Post
Code:
#!/bin/sh

for filename in *;
do
    if [ -f ${filename} ]
    then
        file_ext=$(echo $filename | awk -F . '{if (NF>1) {print $NF}}')
        file_ext2=${filename##*.}
        
        echo ${file_ext}
        echo ${file_ext2}
    fi
done
This will output the extension of every file present in the cirrent directory.
Hope it helps!
No need for all of that, just use a one liner?

Code:
var=$(ls | sed '/\./d'); ls | awk '{split($0, a, "."); print a[2]}'; echo -e "$var"
Or if he wants to pass an extension as a command line arg and see if a file with that extension exists. Then list that all files with that extension if they do exist:
Code:
#!/bin/sh

find -regex ".*\($1\)$" -ls
This one line script will replicate an ls -l command on the files with the specified extension if they are found.

OP, if you want to delete files with specified extensions from the command line
Code:
#!/bin/bash


        if [ $# -lt '1' ]; then
        echo -e "\nUSAGE: $0 + extensions to files you want to remove completely.\n"
        echo -e "\n=> Listing files." && ls -a;
fi

        if [ $# -eq '1' ]; then
	echo -e "Remove files with extention $1?(Y|n) "
	read ans
	
	if [[ "ans" == 'y' || "ans" == 'Y' || "ans" == ' ' ]]; then 
        find -regex ".*\($1\)$" -delete;
        echo -e "\nRemoving files with extension $1."

		else 
		  echo "Exiting now." && 
		  exit;
fi
fi

        if [ $# -eq '2' ]; then
	echo -e "Remove files with extentions $1 and $2?(Y|n) "
	read ans
	
	if [[ "ans" == 'y' || "ans" == 'Y' || "ans" == ' ' ]]; then 
        find -regex ".*\($1\|$2\)$" -delete
        echo -e "\nRemoving files with extensions $1 and $2."

		else 
		  echo "Exiting now." | 
		  exit;
fi
fi

        if [ $# -eq '3' ]; then
	echo -e "Remove files with extentions $1, $2 and $3?(Y|n) "
	read ans
	
	if [[ "ans" == 'y' || "ans" == 'Y' || "ans" == ' ' ]]; then 
        find -regex ".*\($1\|$2\|$3\)$" -delete
        echo -e "\nRemoving files with extensions $1, $2 and $3."

		else 
		  echo "Exiting now." | 
		  exit;
fi
fi

Last edited by amboxer21; 08-01-2012 at 04:52 AM.
 
Old 08-01-2012, 06:13 AM   #10
PierreB.
Member
 
Registered: Apr 2012
Location: Grenoble
Distribution: Rhel[456],CentOS[56],fedora18
Posts: 33

Rep: Reputation: Disabled
Here is one the "innombrables" solutions, to each his own...

I would say something like :

Code:
#!/bin/bash

pattern=( lst txt log )

for entry in * ; do
  if [[ -f "${entry}" ]] ; then
    for PATTERN in ${pattern[@]} ; do
      if [[ "${entry##*.}" =~ ${PATTERN} ]] ; then
        echo "${entry} match ${PATTERN}"
      else
        echo "NO MATCH"
      fi
    done
  fi
done
This is yet simple but efficient, this is usually my guideline for scripting :
1) No external command if possible
2) Easy to read and therefore to extend and maintain
3) No pipe to pipe to pipe pipe to pipe to pipe etc ...

But it still miss a lot of what makes a script good to my understanding :
4) Good comments
5) Error management
6) Usage function
7) Help option

I am sure i miss a lot of goodies here : please let us here yours !

Last edited by PierreB.; 08-01-2012 at 06:15 AM.
 
Old 08-01-2012, 08:02 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,519

Rep: Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896
I am still not real clear on the purpose, but based on some of the replies, how about:
Code:
#!/bin/bash

IFS="|"
pattern="$*"
unset IFS

for i in *
do
    if [[ -f "$i" && $i =~ $pattern ]]
    then
	echo $i
    fi
done
Then you can execute like so:
Code:
$ ./script.sh .c .ex .fi
Of course a dot is any character so mileage may very.

As to the question asked by the OP:
Quote:
can you please elaborate
I am not sure what more you require, did you look up your resource for the items mentioned? (namely - case and =~)
If you do not have a resource, here are some suggestions:

http://tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/TitleIndex
 
  


Reply

Tags
bash scripting, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I extract the .rpm filename from long list of entries using bash shell script Glenn D. Linux - Software 2 07-14-2012 05:42 AM
[SOLVED] Search multiple patterns & print matching patterns instead of whole line Trd300 Linux - Newbie 29 03-05-2012 07:41 PM
bash script - get filename created by script using variables aolong Linux - General 8 11-19-2009 12:40 PM
bash script to use sed for filter mutiples patterns from apache access logs matyu Programming 5 02-06-2008 10:28 PM
Pattern Matching Help in Bash script cmfarley19 Programming 1 04-07-2004 09:22 AM


All times are GMT -5. The time now is 01:37 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration