Skip to content

Instantly share code, notes, and snippets.

@vvondra
Created January 17, 2013 14:33

Revisions

  1. vvondra created this gist Jan 17, 2013.
    136 changes: 136 additions & 0 deletions find.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,136 @@
    adresare=""
    cesty=true
    while [ $# -gt 0 ]
    do
    # nacteni adresaru, ktere se budou prohledavat
    if [ ${1:0:1} != "-" ] ; then
    if $cesty ; then
    adresare="$adresare $1"
    shift
    continue
    else
    echo "Cesty musi predchazet filtrum"
    exit 1
    fi
    fi

    # nacteni filtru
    cesty=false
    case $1 in
    -name)
    shift
    if [ -n "$1" ]; then
    name=$1
    else
    echo "Chybi parametr pro \"-name\""
    exit 1
    fi

    ;;
    -iname)
    shift
    if [ -n "$1" ]; then
    iname=$1
    else
    echo "Chybi parametr pro \"-iname\""
    exit 1
    fi

    ;;
    -type)
    shift

    # detekce typu, v pripade ze neznam typ souboru, konec
    case $1 in
    f|d|l)
    type=$1
    ;;
    *)
    echo "Neznamy nebo chybejici typ souboru \"$1\", povolene je f, d, l."
    exit 1
    ;;
    esac

    ;;
    *)
    echo "Neznamy filtr \"$1\", povolene jsou -name, -iname a -type"
    exit 1
    ;;
    esac

    shift
    done

    iname=`echo "$iname" | tr [:upper:] [:lower:]`

    # pokud nebyl v parametrech adresar, vychozi je soucasny
    if [ ! -n "$adresare" ] ; then
    adresare="."
    fi

    # kontrola existence adresaru v parametrech
    for file in $adresare
    do
    if [ ! -d "$file" ] ; then
    echo "Neexistujici adresar $file"
    exit 1
    fi
    done

    ls -R1 $adresare | while read line
    do
    if [[ "$line" == total* ]] ; then
    continue
    fi

    if [ ! -n "$line" ] ; then
    readpath=true
    continue;
    fi

    if $readpath ; then
    currentpath=${line/:/}
    readpath=false
    continue
    fi

    match=true

    # nyni uz mame jednotlive soubory a muzeme je otestovat, jestli vyhovuji nasemu findu
    if [ -n "$name" ] ; then
    if [[ $line != $name ]] ; then
    match=false
    fi
    fi

    if [ -n "$iname" ] ; then
    if [[ `echo "$line" | tr [:upper:] [:lower:]` != $iname ]]; then
    match=false
    fi
    fi

    if [ -n "$type" ] ; then
    case $type in
    f)
    if [ ! -f "$currentpath/$line" ] ; then
    match=false
    fi
    ;;
    d)
    if [ ! -d "$currentpath/$line" ] ; then
    match=false
    fi
    ;;
    l)
    if [ ! -h "$currentpath/$line" ] ; then
    match=false
    fi
    ;;
    esac
    fi

    if $match ; then
    echo "$currentpath/$line"
    fi

    done