Skip to content

Commit 2a3f93a

Browse files
committedJun 28, 2022
add script that counts files and directories in a given path
1 parent 48759c3 commit 2a3f93a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
 

‎countdf.sh

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
#Task 3 (5 marks)
4+
5+
text_file_path="$1/*" #variable to hold path
6+
7+
non_empty_files=0 #variable to hold number of non-empty files
8+
empty_files=0 #variable to hold number of empty files
9+
non_empty_dir=0 #variable to hold number of non-empty directories
10+
empty_dir=0 #variable to hold number of empty directories
11+
12+
for resource in $text_file_path #loop through resources found in that path
13+
do
14+
if [ -f "$resource" ] #check if a resource is a text file
15+
then
16+
if [ -s "$resource" ]; then #check if that resource is empty
17+
non_empty_files=$((non_empty_files+1)) #increment non_empty_files if it contains data
18+
else
19+
empty_files=$((empty_files+1)) #increment empty_files if it does not contains data
20+
fi
21+
fi
22+
23+
if [ -d "$resource" ] #check if a resource is a directory
24+
then
25+
if [ "$(ls -A $resource)" ]; then #check if that directory is empty
26+
non_empty_dir=$((non_empty_dir+1)) #increment non_empty_dir if it contains data
27+
else
28+
empty_dir=$((empty_dir+1)) #increment empty_dir if it does not contains data
29+
fi
30+
fi
31+
done
32+
33+
#print results
34+
echo "The $text_file_path directory contains:"
35+
echo "$non_empty_files files that contain data"
36+
echo "$empty_files files that are empty"
37+
echo "$non_empty_dir non-empty directories"
38+
echo "$empty_dir empty directories"
39+
40+
exit 0

0 commit comments

Comments
 (0)