Duy Dinh - 2012-02-29
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
# Shell script to find and remove files with a particular extension under a directory and its subdirectories (recursively). 
# Directory names are preceeded by a hash sign, files names are displayed in an absolute format.
# Duy Dinh, IRIT - University of Toulouse, Febr. 2012

args=$* # program arguments except $0

inputDir="." # current directory

extensions="" # list of file extensions
fileFilterSpec="" # file filter specification (e.g., txt|html|php)
useFilter=0 # use file filtering (1) or not (0)

recursive=0 # recursive file listing

usage(){
    echo "Usage:"
    echo "sh removeFiles.sh [-r] [-d inputDir] -e filtersension"
    echo "Example:"
    echo "sh removeFiles.sh -r -d ./ -e \"htm|html|txt|php\""
}

# check whether file extension is valid or not
checkFileExtension(){
    local extension=$1

    matchExtension=0 # returned value of this function
    local ext="";
    #echo "extensions: $extensions"
    for ext in $extensions
    do
        #echo $ext
        if [ $ext = $extension ] || [ $ext = ".${extension}" ] 
        then
            #echo "Matched"
            matchExtension=1
            break
        fi
    done    
}

# function browse_directory
browse_directory()
{
    if !(test -d "$1")  then 
        echo "File or directory not found: $1";
        return;
    fi

    cd "$1"
    path=`pwd` # directory name 
    #echo "#$path"

    # get file entries from current path
    for e in *
    do
        if test -d "$e"  # entry is a directory
        then
            if [ $recursive = 1 ]
            then
                browse_directory "$e" #list files recursively
                cd ..           
            fi      
        else # file entry
            # get file extension            
            extension=${e##*.}
            checkFileExtension $extension
            #echo $matchExtension
            # if not use file filtering, test file extension with or without the period before

            if [ $useFilter = 0 ] || [ $matchExtension = 1 ]
            then

                # make file name absolute
                path=`pwd`; # directory name 
                path="$path/$e"; # file name
                echo "Removing file $path"

                rm "$path"
            fi

        fi
    done
}

# main procedure
main()
{

    if (test $# -eq 0)
    then    
        usage
        exit
    fi

    inputDir=$1
    # parse options
    while getopts ":d:e:o:rh" args; do

    case $args in
        d)
            inputDir=$OPTARG;;
        e)
            fileFilterSpec=$OPTARG          
            # echo $fileFilterSpec
            # transform filters into list of extensions 
            extensions=`echo $fileFilterSpec | tr ";,:|" " "`
            ;;
        r)
            recursive=1;;
        h)
            usage
            exit;;

        ?) # unknown option
            echo "Invalid option: -$OPTARG"
            exit;;
    esac
    done

    # file filter or not
    useFilter=`expr length "$fileFilterSpec"`

    echo "Input directory: $inputDir"
    if test $useFilter -gt 0
    then
        echo "File filter(s) $fileFilterSpec"       
    else
        echo "Please specify file extension and try again!"
    exit
    fi

    # browse input directory
    browse_directory $inputDir  
}

# call main procedure
main $args
 

Last edit: Duy Dinh 2012-03-07