WannaBeProg
WannaBeProg

Reputation: 13

Create a parameter to find files starting with a specific letter in linux

I'm currently studying for my Linux finals and I'm currently struggling with an exercise:

So my script looks like this:

    #!/bin/bash

if [ "$1" ==  "-e" ]
  then
fullfilename=$(basename $2)
extension=${fullfilename##*.}
filename=${fullfilename%.*}

path="$3"
#string=$( find . -type f -name "*.$extension" )
string=$( find $path -type f -name "*.$extension" ) 

for FILE in $string
do
nam=$( basename $FILE )
DIR=$( dirname $FILE )
#DIR=$( ls -d -1 $PWD/*.$extension )
echo -e "$nam \t $DIR "

done 



 elif [ x$1 = "x-h" ] || [ x$1 = "x--help" ] 
then
echo -e "\nusage: bash filename.sh [parameter] [extension] [directory] \n
     example : bash filename.sh -e txt /home/user/ \n
         Parameters :\n
     \t -e\t \t Default parameter \n
         \t -h or --help\t \t show help \n
     \t -nr \t\t find with no recursion "


elif [ $1 == "-nr" ]
 then
fullfilename=$(basename $2)
extension=${fullfilename##*.}
filename=${fullfilename%.*}

path="$3"
#string=$( find . -type f -name "*.$extension" )
string=$( find $path -maxdepth 1 -type f -name "*.$extension" )

for FILE in $string
do
nam=$( basename $FILE )
DIR=$( dirname $FILE )
#DIR=$( ls -d -1 $PWD/*.$extension )
echo -e "$nam \t $DIR "

done

else
echo "use -h or --help for help"
fi

The goal of this script is to find files with a certain extension and show the directory they're in.

What I have to do now is to add a parameter "-fl" that will only search for files starting with the letter you put behind the parameter (Example ./filename.sh -fl m txt /home/user would only search for txt files starting with the letter "m")

Any idea how to implement this into my script?

I think it would have something to do with this

#!/bin/bash

if [ $1 = "-fl" ] 
then
echo [$(echo "$2")]*
fi

But don't know how to add it without messing up.

Upvotes: 0

Views: 401

Answers (1)

konsolebox
konsolebox

Reputation: 75548

It's hard actually to try fixing your code so I made a version of it just for fun. You may refer to this. For anything you wouldn't understand on it, reading the bash manual would give much explanation. With it you wouldn't even need wikis or tutorials if you're diligent enough trust me.

#!/bin/bash

CONFIG_EXT=''
CONFIG_FIRST_LETTER=''
CONFIG_NO_RECURSION=false
CONFIG_PATH=''

function error {
    echo "$1" >&2
    exit 1
}

function show_help_info {
    echo "Usage: $0 -e ext [-fl letter] [-nr] [--] directory
Example: $0 -e txt -fl c -- /home/user/
Options:
  -e          Specifies extension.
  -fl         Specifies first letter.
  -nr         Find with no recursion.
  -h, --help  Show help."
}

while [[ $# -gt 0 ]]; do
    case "$1" in
    -e)
        [[ -n $2 ]] || error "Option '-e' doesn't have an argument."
        CONFIG_EXT=$2
        shift
        ;;
    -nr)
        CONFIG_NO_RECURSION=true
        ;;
    -fl)
        [[ -n $2 ]] || error "Option '-fl' doesn't have an argument."
        [[ $2 == [[:alpha:]] ]] || error "Argument to option '-fl' must be a single letter."
        CONFIG_FIRST_LETTER=$2
        shift
        ;;
    -h|--help)
        show_help_info
        exit 1
        ;;
    -*)
        error "Invalid option: $1"
        ;;
    --)
        CONFIG_PATH=$2
        ;;
    *)
        CONFIG_PATH=$1
        ;;
    esac
    shift
done

if [[ -z $CONFIG_EXT ]]; then
    error "Extension was not specified. Please use -h or --help for usage info."
elif [[ -z $CONFIG_PATH ]]; then
    error "Target path was not specified. Please use -h or --help for usage info."
fi

FL_ARGS=()
[[ -n $CONFIG_FIRST_LETTER ]] && FL_ARGS=('-name' "${CONFIG_FIRST_LETTER}*")

MAXDEPTH_ARGS=()
[[ $CONFIG_NO_RECURSION == true ]] && MAXDEPTH_ARGS=('-maxdepth' 1)

while IFS= read -r FILE; do
    printf "%s\t%s\n" "${FILE##*/}" "${FILE%/*}"
done < <(exec find "$CONFIG_PATH" "${MAXDEPTH_ARGS[@]}" -type f "${FL_ARGS[@]}" -name "*.${CONFIG_EXT}")

# Similar:
#
# find "$CONFIG_PATH" "${MAXDEPTH_ARGS[@]}" -type f "${FL_ARGS[@]}" -name "*.${CONFIG_EXT}" -printf '%f\t%H\n'

Example:

bash temp.sh /var/tmp/tar-1.27.1/ -e m4 -fl c

Output:

configmake.m4   /var/tmp/tar-1.27.1/m4
codeset.m4      /var/tmp/tar-1.27.1/m4
closeout.m4     /var/tmp/tar-1.27.1/m4
closedir.m4     /var/tmp/tar-1.27.1/m4
close.m4        /var/tmp/tar-1.27.1/m4
close-stream.m4 /var/tmp/tar-1.27.1/m4
clock_time.m4   /var/tmp/tar-1.27.1/m4
chown.m4        /var/tmp/tar-1.27.1/m4
chdir-long.m4   /var/tmp/tar-1.27.1/m4
canonicalize.m4 /var/tmp/tar-1.27.1/m4

Upvotes: 2

Related Questions