-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolder-stats-1.sh
68 lines (58 loc) · 2.04 KB
/
folder-stats-1.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
#!/bin/bash
# set some global variables initially to 0
filesCount=0
hiddenfilesCount=0
dirsCount=0
hiddendirsCount=0
# a function that will traverse a directory
function check_dir {
# for each regular file/directory in this directory (argument passed)
for i in "$1"/*
do
# if this particular file/directory is a file
if [[ -f "$i" ]]
then
# keep track of the regular file count, adding 1
filesCount=$(expr $filesCount + 1)
fi
if [[ -d "$i" && "$i" != "$1/." && "$i" != "$1/.." ]]
then
# keep track of the regular directory count, adding 1
dirsCount=$(expr $dirsCount + 1)
# now traverse this directory (this is a recursive function)
check_dir $i
fi
done
# if this particular file/directory is a directory
for j in "$1"/.*
do
# if this particular file/directory is a file
if [[ -f "$j" ]]
then
# keep track of the hidden file count, adding 1
hiddenfilesCount=$(expr $hiddenfilesCount + 1)
fi
# if this particular file/directory is a directory
if [[ -d "$j" && "$j" != "$1/." && "$j" != "$1/.." ]]
then
# keep track of the hidden directory count, adding 1
hiddendirsCount=$(expr $hiddendirsCount + 1)
# now traverse this directory (this is a recursive function)
check_dir $j
fi
done
}
# the main function to be run
function main {
# run the check_dir function
check_dir $1
# display each of the counts after they have been added to (global variables)
echo "Files found: $filesCount (plus $hiddenfilesCount hidden)"
echo "Directories found: $dirsCount (plus $hiddendirsCount hidden)"
# add the counts together using the expr command
echo "Total files and directories: $(expr $filesCount + $hiddenfilesCount + $dirsCount + $hiddendirsCount)"
}
# run the main function, passing the global parameter
main $1
# finish and return with a successful exit code
exit 0