LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash shell script filename matching patterns (http://www.linuxquestions.org/questions/programming-9/bash-shell-script-filename-matching-patterns-4175419667/)

james jackson 07-31-2012 11:56 PM

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

grail 08-01-2012 12:01 AM

Look up your favourite bash resource and search for either case or =~ operator

james jackson 08-01-2012 12:03 AM

can you please elaborate

kauuttt 08-01-2012 12:09 AM

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!

james jackson 08-01-2012 12:13 AM

multiple pattern example when i type from the command line it would be remove /home "*.c" "*.ex" "*.fi"

amboxer21 08-01-2012 12:54 AM

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/

james jackson 08-01-2012 01:07 AM

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.

kauuttt 08-01-2012 03:08 AM

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!

amboxer21 08-01-2012 04:46 AM

Quote:

Originally Posted by kauuttt (Post 4742910)
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


PierreB. 08-01-2012 06:13 AM

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 !

grail 08-01-2012 08:02 AM

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


All times are GMT -5. The time now is 11:12 AM.