Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # A bash scripts that processes a directory in three deferent ways
- # Author : Obada Alabbadi
- # displays Information about using this script
- usage()
- {
- cat <<EOF
- usage: dirproc DIRECTORY ana REGEX
- or: dirproc DIRECTORY del MAXSIZE
- or: dirproc DIRECTORY arr FILETYPE
- This script processes a directory in three deferent ways, DIRECTORY can be a relative or absolute path
- dirproc DIRECTORY ana REGEX
- recursivly searches for and displays all files in DIRECTORY that match the regular expresion REGEX, also prints the number of matches
- dirproc DIRECTORY del MAXSIZE
- recursivly searches for all files in DIRECTORY that are larger than MAXSIZE bytes, where MAXSIZE is an unsigned integer, the user must confirm before deleting by typing
- y : delete this file
- yall : delete this file and all next matches
- n : do not delete this file
- nall : do not delete this file and all next matches
- dirproc DIRECTORY arr FILETYPE
- creates a directory in the pwd named "all FILETYPE files" (if it doesn't already exist), then recursivly searches for all files in DIRECTORY that have an extension of FILETYPE and copies them to the created directory
- EOF
- exit 1
- }
- # process a specific file depending on the arguments, the first argument is the file path
- process()
- {
- # file info
- filename=$(basename -- "$1")
- extension="${filename##*.}"
- filename="${filename%.*}"
- filesize=$(stat -c%s "$1")
- if [ -n "$ana" ] && [[ "$filename" =~ $regex ]] # found a matching regex
- then
- echo "Found match : $filename"
- count=`expr $count + 1`
- elif [ -n "$del" ] && [[ $filesize -gt $mxsize ]] && ! [ -n "$nall" ] # found a file with big size
- then
- #if the user already choose "yall" remove the file immediately
- if [ -n "$yall" ]
- then
- rm "$1"
- return
- fi
- echo "file : $item"
- echo "size : $filesize"
- #prompt the user for confirmation
- echo "are you sure you want to delete this file? [y/n/yall/nall]"
- read reply
- if [ "$reply" = "yall" ]
- then
- yall=1
- rm "$1"
- elif [ "$reply" = "nall" ]
- then
- nall=1
- elif [ "$reply" = "y" ]
- then
- rm "$1"
- echo "succesfully removed"
- else
- echo "file not removed"
- fi
- echo
- elif [ -n "$arr" ] && [[ "$extension" = "$type" ]] # found a file with the extension
- then
- cp "$1" "$PWD/all $type files"
- fi
- }
- # recursivly process a directory (the first parameter)
- search()
- {
- # iterate over all directories and files
- for item in "$1"/*
- do
- if [ -d "$item" ] # another directory, search inside it
- then
- search "$item"
- elif [ -f "$item" ]
- then
- process "$item" # a file, process it
- fi
- done
- }
- # needed variables
- ana=
- count=0
- regex=
- del=
- mxsize=
- numregex='^[0-9]+$'
- yall=
- nall=
- arr=
- type=
- # get the abolute path
- absolute=
- case $1 in
- /*) absolute=$1;;
- *) absolute=$PWD/$1;;
- esac
- if ! [ -d $absolute ] # invalid directory, display usage and exit
- then
- usage
- elif [ "$2" = "ana" ] && [ -n "$3" ] # first option
- then
- ana=1
- regex=$3
- elif [ "$2" = "del" ] && [[ "$3" =~ $numregex ]] # second option
- then
- del=1
- mxsize=$3
- elif [ "$2" = "arr" ] && [ -n "$3" ] # third option
- then
- arr=1
- type=$3
- if ! [ -d "$PWD/all $type files" ]
- then
- mkdir "all $type files"
- fi
- else # invalid arguments, display usage
- usage
- fi
- # search recursivly
- search "$absolute"
- # display number of matches for REGEX
- if [ -n "$ana" ]
- then
- echo "$count matching files"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement