Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.45 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # A bash scripts that processes a directory in three deferent ways
  4. # Author : Obada Alabbadi
  5.  
  6. # displays Information about using this script
  7. usage()
  8. {
  9.     cat <<EOF
  10.     usage: dirproc DIRECTORY ana REGEX
  11.     or:    dirproc DIRECTORY del MAXSIZE
  12.     or:    dirproc DIRECTORY arr FILETYPE
  13.  
  14.     This script processes a directory in three deferent ways, DIRECTORY can be a relative or absolute path
  15.  
  16.     dirproc DIRECTORY ana REGEX
  17.     recursivly searches for and displays all files in DIRECTORY that match the regular expresion REGEX, also prints the number of matches
  18.  
  19.     dirproc DIRECTORY del MAXSIZE
  20.     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
  21.     y : delete this file
  22.     yall : delete this file and all next matches
  23.         n : do not delete this file
  24.     nall : do not delete this file and all next matches
  25.  
  26.     dirproc DIRECTORY arr FILETYPE
  27.     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
  28. EOF
  29.     exit 1
  30. }
  31.  
  32. # process a specific file depending on the arguments, the first argument is the file path
  33. process()
  34. {
  35.     # file info
  36.     filename=$(basename -- "$1")
  37.     extension="${filename##*.}"
  38.     filename="${filename%.*}"
  39.  
  40.     filesize=$(stat -c%s "$1")
  41.  
  42.     if [ -n "$ana" ] && [[ "$filename" =~ $regex ]] # found a matching regex
  43.     then
  44.         echo "Found match : $filename"
  45.         count=`expr $count + 1`
  46.     elif [ -n "$del" ] && [[ $filesize -gt $mxsize ]] && ! [ -n "$nall" ] # found a file with big size
  47.     then
  48.         #if the user already choose "yall" remove the file immediately
  49.         if [ -n "$yall" ]
  50.         then
  51.             rm "$1"
  52.             return
  53.         fi
  54.  
  55.         echo "file : $item"
  56.         echo "size : $filesize"
  57.        
  58.         #prompt the user for confirmation
  59.         echo "are you sure you want to delete this file? [y/n/yall/nall]"
  60.         read reply
  61.  
  62.         if [ "$reply" = "yall" ]
  63.         then
  64.             yall=1
  65.             rm "$1"
  66.         elif [ "$reply" = "nall" ]
  67.         then
  68.             nall=1
  69.         elif [ "$reply" = "y" ]
  70.         then
  71.             rm "$1"
  72.             echo "succesfully removed"
  73.         else
  74.             echo "file not removed"
  75.         fi
  76.         echo
  77.     elif [ -n "$arr" ] && [[ "$extension" = "$type" ]] # found a file with the extension
  78.     then
  79.         cp "$1" "$PWD/all $type files"
  80.     fi
  81. }
  82.  
  83. # recursivly process a directory (the first parameter)
  84. search()
  85. {
  86.     # iterate over all directories and files
  87.     for item in "$1"/*
  88.     do
  89.                 if [ -d "$item" ] # another directory, search inside it
  90.         then
  91.             search "$item"
  92.         elif [ -f "$item" ]
  93.         then
  94.             process "$item" # a file, process it
  95.         fi
  96.         done
  97. }
  98.  
  99. # needed variables
  100.  
  101. ana=
  102. count=0
  103. regex=
  104.  
  105. del=
  106. mxsize=
  107. numregex='^[0-9]+$'
  108. yall=
  109. nall=
  110.  
  111. arr=
  112. type=
  113.  
  114. # get the abolute path
  115. absolute=
  116. case $1 in
  117.   /*) absolute=$1;;
  118.   *) absolute=$PWD/$1;;
  119. esac
  120.  
  121. if ! [ -d $absolute ] # invalid directory, display usage and exit
  122. then
  123.     usage
  124. elif [ "$2" = "ana" ] && [ -n "$3" ] # first option
  125. then
  126.     ana=1
  127.     regex=$3
  128. elif [ "$2" = "del" ] && [[ "$3" =~ $numregex ]] # second option
  129. then
  130.     del=1
  131.     mxsize=$3
  132. elif [ "$2" = "arr" ] && [ -n "$3" ] # third option
  133. then
  134.     arr=1
  135.     type=$3
  136.     if ! [ -d "$PWD/all $type files" ]
  137.     then
  138.         mkdir "all $type files"
  139.     fi
  140. else # invalid arguments, display usage
  141.     usage
  142. fi
  143.  
  144. # search recursivly
  145. search "$absolute"
  146.  
  147. # display number of matches for REGEX
  148. if [ -n "$ana" ]
  149. then
  150.     echo "$count matching files"
  151. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement