In this article, we will solve VNSGU TYBCA Semester 5 Linux (UNIX) Practical – Set F using shell scripting.
This practical focuses on:
- File and directory identification
- Current working directory
- File test operators
- Shell loops
findwc
📌 Practical Question
Write a shell script to find total number of files and total number of directories in current working directory.
The script should count the files and directories present in the current working directory.
Important
For this question, we are counting the direct contents of the current directory only.
For example:
project/
├── file1.txt
├── file2.txt
├── script.sh
├── notes/
└── programs/
The result is:
Files : 3
Directories : 2
Files inside notes/ or programs/ are not counted.
🧠 Concepts Used
| Concept | Purpose |
|---|---|
pwd |
Display current directory |
find |
Search files/directories |
-type f |
Identify regular files |
-type d |
Identify directories |
wc -l |
Count results |
for loop |
Process directory entries |
-f |
File test |
-d |
Directory test |
✅ Solution 1: Using find
This is one of the cleanest solutions for the practical.
Shell Script
#!/bin/bash
files=$(find . -maxdepth 1 -type f | wc -l)
directories=$(find . -mindepth 1 -maxdepth 1 -type d | wc -l)
echo "Current Working Directory: $(pwd)"
echo "Total Files : $files"
echo "Total Directories : $directories"
🔍 Step-by-Step Explanation
Step 1: Find Files
find . -maxdepth 1 -type f
Here:
-
.means the current directory. -
-maxdepth 1means don't go inside subdirectories. -
-type fmeans search for regular files.
For example:
./file1.txt
./file2.txt
./script.sh
Step 2: Count the Files
find . -maxdepth 1 -type f | wc -l
The | is called a pipe.
The output of find becomes the input of wc.
find
↓
pipe
↓
wc -l
wc -l counts the number of lines returned by find.
Step 3: Find Directories
find . -mindepth 1 -maxdepth 1 -type d
This finds directories in the current directory.
The important part is:
-mindepth 1
It prevents find from counting the current directory . itself.
🖥️ Example
Suppose the current directory contains:
file1.txt
file2.txt
program.sh
notes/
backup/
Run:
./script.sh
Output:
Current Working Directory: /home/student/linux
Total Files : 3
Total Directories : 2
✅ Solution 2: Using Shell Loop and File Tests
This approach is useful for understanding shell scripting logic instead of depending mainly on find.
Shell Script
#!/bin/bash
files=0
directories=0
for item in *
do
if [ -f "$item" ]
then
files=$((files + 1))
elif [ -d "$item" ]
then
directories=$((directories + 1))
fi
done
echo "Current Working Directory: $(pwd)"
echo "Total Files : $files"
echo "Total Directories : $directories"
🔍 How This Solution Works
Step 1: Initialize Counters
files=0
directories=0
Both counters start from zero.
Step 2: Loop Through Items
for item in *
The * represents entries in the current directory.
The loop processes each item one by one.
Step 3: Check for File
if [ -f "$item" ]
The -f test returns true if the item is a regular file.
Then:
files=$((files + 1))
increases the file counter.
Step 4: Check for Directory
elif [ -d "$item" ]
The -d test returns true if the item is a directory.
Then:
directories=$((directories + 1))
increases the directory counter.
🖥️ Example
Suppose:
project/
├── a.txt
├── b.txt
├── c.sh
├── notes/
└── backup/
The loop processes:
a.txt → File
b.txt → File
c.sh → File
notes → Directory
backup → Directory
Output:
Current Working Directory: /home/student/project
Total Files : 3
Total Directories : 2
✅ Solution 3: Using find with -mindepth
We can also count both types directly without first storing the results in variables.
Shell Script
#!/bin/bash
echo "Current Working Directory: $(pwd)"
echo "Total Files : $(find . -mindepth 1 -maxdepth 1 -type f | wc -l)"
echo "Total Directories : $(find . -mindepth 1 -maxdepth 1 -type d | wc -l)"
The -mindepth 1 option ensures that the current directory itself is not counted.
🔍 Understanding -mindepth
Consider:
find . -maxdepth 1 -type d
It can return:
.
./notes
./backup
The . represents the current directory.
Now:
find . -mindepth 1 -maxdepth 1 -type d
returns:
./notes
./backup
So there is no need to subtract 1 from the result.
📊 Comparing the Solutions
| Method | Main Concept | Difficulty | Recommended |
|---|---|---|---|
find + wc
|
Linux commands | ⭐ Easy | ✅ Excellent |
for + -f/-d
|
Shell scripting | ⭐⭐ | ✅ Excellent |
find + -mindepth
|
find options |
⭐⭐ | ✅ Excellent |
Which Solution Should You Use?
For the practical exam, Solution 1 or Solution 3 is very convenient.
If the examiner wants to see your shell scripting logic, Solution 2 is a better choice because it uses:
for
if
-f
-d
🧠 Important Difference: -maxdepth 1
This is very important for this question.
Consider:
project/
├── a.txt
├── notes/
│ ├── b.txt
│ └── c.txt
└── script.sh
If we use:
find . -type f
the result includes:
./a.txt
./notes/b.txt
./notes/c.txt
./script.sh
So it searches recursively.
But:
find . -maxdepth 1 -type f
only finds:
./a.txt
./script.sh
Therefore, -maxdepth 1 is important when the question specifically asks for files in the current working directory.
⚠️ Common Mistakes
Mistake 1: Counting Recursively
Don't use:
find . -type f | wc -l
if the question means only the current directory.
This will also count files inside subdirectories.
Use:
find . -maxdepth 1 -type f | wc -l
Mistake 2: Counting . as a Directory
This:
find . -maxdepth 1 -type d
includes:
.
which represents the current directory.
Use:
-mindepth 1
to exclude it.
Mistake 3: Forgetting Quotes
Prefer:
[ -f "$item" ]
instead of:
[ -f $item ]
Quotes make the script safer when file or directory names contain spaces.
🎯 Practical Exam Tips
Remember these important commands.
Find Files
find . -maxdepth 1 -type f
Find Directories
find . -mindepth 1 -maxdepth 1 -type d
Count Results
wc -l
Check File
[ -f "$item" ]
Check Directory
[ -d "$item" ]
Current Directory
pwd
📝 Quick Revision
The simplest command-based solution is:
files=$(find . -maxdepth 1 -type f | wc -l)
directories=$(find . -mindepth 1 -maxdepth 1 -type d | wc -l)
echo "Files : $files"
echo "Directories : $directories"
The shell-loop approach is:
for item in *
do
if [ -f "$item" ]
then
files=$((files + 1))
elif [ -d "$item" ]
then
directories=$((directories + 1))
fi
done
📚 Related Linux Practical Sets
This solution is part of the VNSGU TYBCA Sem 5 Linux (UNIX) Practical – OCT/Nov 2025 solution series.
- Set A – Simple Interest & Compound Interest
- Set B – Vowels Count & Case Conversion
- Set C – Palindrome String
- Set D – File Operations & Text Processing
- Set E – Display Lines with Validation
- Set F – Count Files & Directories
- Set G – Recently Modified Files
- Set H – Employee Gross Salary
🐧 Conclusion
The Set F practical is a good exercise for understanding how Linux identifies files and directories.
The most important concepts are:
-type f → File
-type d → Directory
-maxdepth 1 → Do not search inside subdirectories
-mindepth 1 → Exclude the starting directory itself
If you understand these options, you can easily solve variations of this practical question.
💬 What Do You Prefer?
Which approach do you prefer for counting files and directories?
find command or shell for loop with -f and -d?
Share your approach in the comments! 🐧💻
📌 Tags
linux shell unix beginners
Top comments (0)