-
Notifications
You must be signed in to change notification settings - Fork 0
/
files_info.sh
executable file
·71 lines (56 loc) · 1.3 KB
/
files_info.sh
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
#!/bin/bash
# ./files_info.sh [-l location] [--location location] [-e extension] [--extension extension] [-h] [--help] [-s] [--stats]
function usage() {
echo "USAGE: $0 [-l location] [--location location] [-e extension] [--extension extension] [-h] [--help] [-s] [--stats]"
echo "Examples:"
echo "$0 -l /etc/ -e txt -s"
echo "$0 -e img --stats"
echo
exit 1
}
LOC_SET=0 #0-location not set, use current location 1-location set
STATS=0 #0-do not display statistics 1-display statistics
while [ $# -gt 0 ]; do
case $1 in
-l | --location)
LOCATION="$2"
if ! [ -d "$LOCATION" ]; then
usage
fi
LOC_SET=1
shift
shift
;;
-e | --extension)
EXT="$2"
shift
shift
;;
-s | --stats)
STATS=1
shift
;;
-h | --help)
shift
usage
;;
*)
usage
;;
esac
done
if [ $LOC_SET -ne 1 ]; then
LOCATION=$(pwd)
fi
echo "Location: $LOCATION"
if [ "$EXT" != "" ]; then
ls -l $LOCATION | awk '/^-/' | grep "\.$EXT$" &>/dev/null
if [ $? -ne 0 ]; then
echo "No file with extension: $EXT found"
exit 2
fi
#count size of files with specific extension
ls -l $LOCATION | awk '/^-/' | grep "\.$EXT$" | awk -v stats=$STATS -f size.awk
else
ls -l $LOCATION | awk '/^-/' | awk -v stats=$STATS -f size.awk
fi