DEV Community

Fahad Vahora
Fahad Vahora

Posted on

Linux Practical Set F Solution – Count Files and Directories

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
  • find
  • wc

📌 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/
Enter fullscreen mode Exit fullscreen mode

The result is:

Files       : 3
Directories : 2
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

🔍 Step-by-Step Explanation

Step 1: Find Files

find . -maxdepth 1 -type f
Enter fullscreen mode Exit fullscreen mode

Here:

  • . means the current directory.
  • -maxdepth 1 means don't go inside subdirectories.
  • -type f means search for regular files.

For example:

./file1.txt
./file2.txt
./script.sh
Enter fullscreen mode Exit fullscreen mode

Step 2: Count the Files

find . -maxdepth 1 -type f | wc -l
Enter fullscreen mode Exit fullscreen mode

The | is called a pipe.

The output of find becomes the input of wc.

find
  ↓
pipe
  ↓
wc -l
Enter fullscreen mode Exit fullscreen mode

wc -l counts the number of lines returned by find.


Step 3: Find Directories

find . -mindepth 1 -maxdepth 1 -type d
Enter fullscreen mode Exit fullscreen mode

This finds directories in the current directory.

The important part is:

-mindepth 1
Enter fullscreen mode Exit fullscreen mode

It prevents find from counting the current directory . itself.


🖥️ Example

Suppose the current directory contains:

file1.txt
file2.txt
program.sh
notes/
backup/
Enter fullscreen mode Exit fullscreen mode

Run:

./script.sh
Enter fullscreen mode Exit fullscreen mode

Output:

Current Working Directory: /home/student/linux
Total Files       : 3
Total Directories : 2
Enter fullscreen mode Exit fullscreen mode

✅ 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"
Enter fullscreen mode Exit fullscreen mode

🔍 How This Solution Works

Step 1: Initialize Counters

files=0
directories=0
Enter fullscreen mode Exit fullscreen mode

Both counters start from zero.


Step 2: Loop Through Items

for item in *
Enter fullscreen mode Exit fullscreen mode

The * represents entries in the current directory.

The loop processes each item one by one.


Step 3: Check for File

if [ -f "$item" ]
Enter fullscreen mode Exit fullscreen mode

The -f test returns true if the item is a regular file.

Then:

files=$((files + 1))
Enter fullscreen mode Exit fullscreen mode

increases the file counter.


Step 4: Check for Directory

elif [ -d "$item" ]
Enter fullscreen mode Exit fullscreen mode

The -d test returns true if the item is a directory.

Then:

directories=$((directories + 1))
Enter fullscreen mode Exit fullscreen mode

increases the directory counter.


🖥️ Example

Suppose:

project/
├── a.txt
├── b.txt
├── c.sh
├── notes/
└── backup/
Enter fullscreen mode Exit fullscreen mode

The loop processes:

a.txt     → File
b.txt     → File
c.sh      → File
notes     → Directory
backup    → Directory
Enter fullscreen mode Exit fullscreen mode

Output:

Current Working Directory: /home/student/project
Total Files       : 3
Total Directories : 2
Enter fullscreen mode Exit fullscreen mode

✅ 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)"
Enter fullscreen mode Exit fullscreen mode

The -mindepth 1 option ensures that the current directory itself is not counted.


🔍 Understanding -mindepth

Consider:

find . -maxdepth 1 -type d
Enter fullscreen mode Exit fullscreen mode

It can return:

.
./notes
./backup
Enter fullscreen mode Exit fullscreen mode

The . represents the current directory.

Now:

find . -mindepth 1 -maxdepth 1 -type d
Enter fullscreen mode Exit fullscreen mode

returns:

./notes
./backup
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

🧠 Important Difference: -maxdepth 1

This is very important for this question.

Consider:

project/
├── a.txt
├── notes/
│   ├── b.txt
│   └── c.txt
└── script.sh
Enter fullscreen mode Exit fullscreen mode

If we use:

find . -type f
Enter fullscreen mode Exit fullscreen mode

the result includes:

./a.txt
./notes/b.txt
./notes/c.txt
./script.sh
Enter fullscreen mode Exit fullscreen mode

So it searches recursively.

But:

find . -maxdepth 1 -type f
Enter fullscreen mode Exit fullscreen mode

only finds:

./a.txt
./script.sh
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

if the question means only the current directory.

This will also count files inside subdirectories.

Use:

find . -maxdepth 1 -type f | wc -l
Enter fullscreen mode Exit fullscreen mode

Mistake 2: Counting . as a Directory

This:

find . -maxdepth 1 -type d
Enter fullscreen mode Exit fullscreen mode

includes:

.
Enter fullscreen mode Exit fullscreen mode

which represents the current directory.

Use:

-mindepth 1
Enter fullscreen mode Exit fullscreen mode

to exclude it.


Mistake 3: Forgetting Quotes

Prefer:

[ -f "$item" ]
Enter fullscreen mode Exit fullscreen mode

instead of:

[ -f $item ]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Find Directories

find . -mindepth 1 -maxdepth 1 -type d
Enter fullscreen mode Exit fullscreen mode

Count Results

wc -l
Enter fullscreen mode Exit fullscreen mode

Check File

[ -f "$item" ]
Enter fullscreen mode Exit fullscreen mode

Check Directory

[ -d "$item" ]
Enter fullscreen mode Exit fullscreen mode

Current Directory

pwd
Enter fullscreen mode Exit fullscreen mode

📝 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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

📚 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
Enter fullscreen mode Exit fullscreen mode

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)