-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountfiles
More file actions
executable file
·92 lines (78 loc) · 2.33 KB
/
countfiles
File metadata and controls
executable file
·92 lines (78 loc) · 2.33 KB
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
#!/bin/zsh
NAME="countfiles"
VERSION="1.0.2"
VERSION_ECHO="$NAME v$VERSION"
RECURSIVE=0
COUNT_DIRS=0
COUNT_FILES=1
SPECIFY_COUNT_FILES=0
GREEN="\e[0;32m"
YELLOW="\e[0;33m"
BLUE="\e[0;34m"
RESET="\e[0m"
_Help()
{
cat <<- "EOF"
Usage: $NAME [-hvrdf] [directory]
-h, --help print help document
-v, --version print script version
-r, --recursive include all sub-directories in search
-d, --directory count directories (files will not be counted unless "-f" is explicitly specified)
-f, --files count files (default behaviour; this flag is only useful in combination with "-d")
Examples:
$NAME -r
$NAME -d ~/Documents
Notes:
If either no directory or an invalid directory are specified, the current working directory is used instead.
EOF
printf "\n$VERSION_ECHO\n"
}
OPTIONS=$(getopt -l "help,version,recursive,directories,files" -o "hvrdf" -a -- "$@")
eval set -- "$OPTIONS"
while true
do
case "$1" in
-h|--help)
_Help
exit 0
;;
-v|--version)
printf "$VERSION_ECHO\n"
exit 0
;;
-r|--recursive)
RECURSIVE=1
;;
-d|--directories)
COUNT_DIRS=1
;;
-f|--files)
SPECIFY_COUNT_FILES=1
;;
--)
shift
break
;;
esac
shift
done
if [[ -n "$1" ]] && [[ $(ls $1 &>/dev/null; echo $?) == 0 ]] ; then
DIR="$(cd $1; pwd)"
else
DIR="$(pwd)"
fi
FILE_COUNT=0
DIRECTORY_COUNT=0
[[ $COUNT_DIRS -eq 1 && $SPECIFY_COUNT_FILES -ne 1 ]] && COUNT_FILES=0
[[ $RECURSIVE -eq 1 ]] && _RECURSIVE_PRINTOUT=" $YELLOW(Recursive search active!)$RESET\n" || _RECURSIVE_PRINTOUT="\n"
if [[ $COUNT_FILES -eq 1 ]]; then
EXTRA_NEWLINE="\n"
printf "::Counting the number of files in $BLUE$DIR$RESET$_RECURSIVE_PRINTOUT"
[[ $RECURSIVE -eq 1 ]] && FILE_COUNT=$(find "$DIR" -type f | wc -l) || FILE_COUNT=$(find "$DIR" -maxdepth 1 -type f | wc -l)
printf "Files count: $GREEN$FILE_COUNT$RESET\n"
fi
if [[ $COUNT_DIRS -eq 1 ]]; then
printf "$EXTRA_NEWLINE::Counting the number of directories in $BLUE$DIR$RESET$_RECURSIVE_PRINTOUT"
[[ $RECURSIVE -eq 1 ]] && DIRECTORY_COUNT=$(find "$DIR" -type d | wc -l) || DIRECTORY_COUNT=$(find "$DIR" -maxdepth 1 -type d | wc -l)
printf "Directories count: $GREEN$DIRECTORY_COUNT$RESET\n"
fi