In this article, we will solve VNSGU TYBCA Semester 5 Linux (UNIX) Practical – Set G using shell scripting.
This practical focuses on:
- Finding recently modified files
- File modification time
findsortawklsdustat- File size
- Current working directory
📌 Practical Question
Write a shell script to find all files in the current directory that were modified in the last 7 days and display their names and sizes.
The script should:
- Search for files in the current directory.
- Find files modified within the last 7 days.
- Display the file names.
- Display the file sizes.
🧠 Concepts Used
| Command / Option | Purpose |
|---|---|
find |
Search for files |
-type f |
Select regular files |
-mtime |
Check modification time |
-maxdepth |
Limit search depth |
sort |
Sort command output |
awk |
Process and format output |
ls -lh |
Display file information |
du -h |
Display file size |
stat |
Display file information |
⏱️ Understanding -mtime
The -mtime option is used with find to search files according to their modification time.
For example:
find . -mtime -7
means:
Find files modified less than 7 complete 24-hour periods ago.
The - before 7 means less than 7.
Common Examples
-mtime -1
→ Modified within the last 1 day.
-mtime -7
→ Modified within the last 7 complete 24-hour periods.
-mtime +7
→ Modified more than 7 complete 24-hour periods ago.
For this practical, we use:
-mtime -7
⚠️ Important: -mtime Uses 24-Hour Periods
find -mtime works with complete 24-hour periods, not simply calendar dates.
Therefore:
-mtime -7
means files whose age is less than 7 complete 24-hour periods.
If an exact time-based 7-day window is required, -mmin can be used.
✅ Solution 1: Using find and ls
This is one of the simplest solutions for the practical examination.
Shell Script
#!/bin/bash
echo "Files modified in the last 7 days:"
echo "-----------------------------------"
find . -maxdepth 1 -type f -mtime -7 -exec ls -lh {} \;
How It Works
find .
Searches from the current directory.
-maxdepth 1
prevents the search from going inside subdirectories.
-type f
selects regular files only.
-mtime -7
selects files modified within the last 7 complete 24-hour periods.
Finally:
-exec ls -lh {} \;
runs ls -lh for every matching file.
🖥️ Example Output
Suppose the current directory contains:
notes.txt
program.sh
old.txt
data.csv
and three files were modified within the last 7 days.
The output may look like:
Files modified in the last 7 days:
-----------------------------------
-rw-r--r-- 1 user user 2.1K Aug 8 09:30 ./notes.txt
-rwxr-xr-x 1 user user 1.5K Aug 7 16:20 ./program.sh
-rw-r--r-- 1 user user 8.4K Aug 6 11:45 ./data.csv
The exact permissions, dates, owner, and sizes depend on the system.
✅ Solution 2: Display Only File Name and Size
The previous solution displays complete ls -lh information.
If the question specifically asks for file name and size, we can make the output cleaner.
Shell Script
#!/bin/bash
echo "Files modified in the last 7 days:"
echo "-----------------------------------"
find . -maxdepth 1 -type f -mtime -7 -print0 |
while IFS= read -r -d '' file
do
size=$(du -h "$file" | cut -f1)
echo "File: $file"
echo "Size: $size"
echo
done
How It Works
-print0
separates file names using a null character.
This is useful for file names containing spaces.
For example:
my notes.txt
can be processed safely.
Then:
du -h "$file"
gets the file size in human-readable format.
For example:
4.0K ./notes.txt
The cut -f1 extracts the size.
🖥️ Example Output
Files modified in the last 7 days:
-----------------------------------
File: ./notes.txt
Size: 4.0K
File: ./program.sh
Size: 8.0K
File: ./data.csv
Size: 12K
✅ Solution 3: Using find and stat
stat can be used to get the exact file size in bytes.
Shell Script
#!/bin/bash
echo "Files modified in the last 7 days:"
echo "-----------------------------------"
find . -maxdepth 1 -type f -mtime -7 -print0 |
while IFS= read -r -d '' file
do
size=$(stat -c "%s" "$file")
echo "File: $file"
echo "Size: $size bytes"
echo
done
Understanding stat
The command:
stat -c "%s" "$file"
returns the file size in bytes.
For example:
4096
means the file size is 4096 bytes.
Here:
%s → File size in bytes
🖥️ Example Output
Files modified in the last 7 days:
-----------------------------------
File: ./notes.txt
Size: 4096 bytes
File: ./program.sh
Size: 1536 bytes
File: ./data.csv
Size: 8192 bytes
✅ Solution 4: Using find, sort and awk
This is an interesting approach when we want to sort the files according to their modification time and then display their names and sizes.
Here, find finds the files, sort sorts the results, and awk formats the output.
Shell Script
#!/bin/bash
echo "Files modified in the last 7 days:"
echo "-----------------------------------"
find . -maxdepth 1 -type f -mtime -7 \
-printf '%T@ %s %p\n' |
sort -n |
awk '{
printf "File: %-25s Size: %s bytes\n", $3, $2
}'
Understanding the find Output
The important part is:
-printf '%T@ %s %p\n'
These format specifiers mean:
%T@ → Modification timestamp
%s → File size in bytes
%p → File path/name
For example, the generated output may look like:
1754650200.123 2048 ./notes.txt
1754700300.456 4096 ./program.sh
1754750100.789 1024 ./data.txt
Sorting the Files
The output is passed to:
sort -n
The -n option performs numeric sorting.
Since the modification timestamp is the first value, the files are sorted according to their modification time.
The oldest matching file appears first.
Displaying the Result with awk
Finally:
awk '{
printf "File: %-25s Size: %s bytes\n", $3, $2
}'
formats the output.
Here:
$2 → File size
$3 → File name/path
🔄 Display Latest Modified File First
If we want the most recently modified file first, use:
find . -maxdepth 1 -type f -mtime -7 \
-printf '%T@ %s %p\n' |
sort -nr |
awk '{
printf "File: %-25s Size: %s bytes\n", $3, $2
}'
Here:
-n → Numeric sorting
-r → Reverse order
Therefore:
sort -nr
sorts the modification timestamps from newest to oldest.
🖥️ Example Output
Files modified in the last 7 days:
-----------------------------------
File: ./data.txt Size: 1024 bytes
File: ./notes.txt Size: 2048 bytes
File: ./program.sh Size: 4096 bytes
The exact order depends on the modification timestamps.
⚠️ Note About File Names with Spaces
The simple awk example above assumes that file names do not contain spaces.
For example:
my notes.txt
can cause fields to be split by awk.
For robust scripts, the -print0 + read -d '' approach from Solution 2 is safer.
For a basic university practical, however, the find + sort + awk approach is useful for demonstrating how Linux commands can be combined.
🔍 Understanding the Command Flow
The sort solution follows this pipeline:
find
↓
Find files modified in last 7 days
↓
-printf
↓
Generate timestamp + size + filename
↓
sort
↓
Sort by modification timestamp
↓
awk
↓
Format the output
This is a good example of combining multiple Linux commands using a pipeline.
📊 Comparing the Solutions
| Method | Main Commands | Output | Difficulty | Recommended |
|---|---|---|---|---|
| Solution 1 | find + ls |
Full file information | ⭐ | ✅ Excellent |
| Solution 2 | find + du |
Name + readable size | ⭐⭐ | ✅ Excellent |
| Solution 3 | find + stat |
Name + size in bytes | ⭐⭐ | ✅ Good |
| Solution 4 | find + sort + awk |
Sorted name + size | ⭐⭐⭐ | 🔥 Advanced |
Which Solution Should You Use?
For a practical examination:
Beginner-friendly: Solution 1
Only name and readable size: Solution 2
Size in bytes: Solution 3
Want to demonstrate sort + awk: Solution 4
🧠 Important Difference: -mtime vs -mmin
find provides both -mtime and -mmin.
Using -mtime
find . -maxdepth 1 -type f -mtime -7
This works with complete 24-hour periods.
Using -mmin
find . -maxdepth 1 -type f -mmin -10080
Because:
7 days × 24 hours × 60 minutes = 10080 minutes
-mmin can be useful when a minute-based time range is required.
For this practical, -mtime -7 is the straightforward solution.
🧠 Current Directory vs Recursive Search
This distinction is important.
Current Directory Only
find . -maxdepth 1 -type f -mtime -7
This searches only the current directory.
Current Directory and Subdirectories
find . -type f -mtime -7
This searches recursively.
Since the question specifically says:
in the current directory
we use:
-maxdepth 1
⚠️ Common Mistakes
Mistake 1: Forgetting -type f
Don't use:
find . -mtime -7
because the result can include directories.
Use:
find . -type f -mtime -7
Mistake 2: Searching Inside Subdirectories
Don't use:
find . -type f -mtime -7
if only the current directory should be searched.
Use:
find . -maxdepth 1 -type f -mtime -7
Mistake 3: Using -mtime 7
These are different:
-mtime 7
and:
-mtime -7
-mtime 7 refers to a particular 24-hour age bucket.
-mtime -7 means less than 7 complete 24-hour periods old.
For "modified in the last 7 days", -mtime -7 is generally the intended approach.
Mistake 4: Confusing sort -n and sort -nr
sort -n
sorts numeric values in ascending order.
sort -nr
sorts numeric values in descending order.
Therefore:
sort -n → Older → Newer
sort -nr → Newer → Older
Mistake 5: Parsing ls Carelessly
Commands such as:
ls -lh | awk ...
can become unreliable when file names contain spaces or special characters.
For safer file-name handling, use:
find ... -print0
with:
read -d ''
🎯 Practical Exam Tips
Remember this basic command:
find . -maxdepth 1 -type f -mtime -7
It means:
. → Current directory
-maxdepth 1 → Do not enter subdirectories
-type f → Files only
-mtime -7 → Modified within the last 7 complete days
For human-readable file size:
ls -lh filename
or:
du -h filename
For file size in bytes:
stat -c "%s" filename
For sorting numeric values:
sort -n
For reverse numeric sorting:
sort -nr
📝 Quick Revision
Find recently modified files
find . -maxdepth 1 -type f -mtime -7
Find files and display information
find . -maxdepth 1 -type f -mtime -7 -exec ls -lh {} \;
Get human-readable size
du -h "$file"
Get size in bytes
stat -c "%s" "$file"
Sort by modification timestamp
sort -n
Sort newest first
sort -nr
📚 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 G practical is a useful exercise for learning how Linux can search files based on their modification time and display their sizes.
The most important command is:
find . -maxdepth 1 -type f -mtime -7
You can then combine it with other Linux commands depending on the required output:
find → Find files
ls → Display file information
du → Display readable size
stat → Display exact size
sort → Sort results
awk → Format results
The find + sort + awk solution is especially useful for understanding how multiple Linux commands can work together through a pipeline.
💬 What Do You Prefer?
For finding recently modified files, which approach do you prefer?
find + ls, find + du, find + stat, or the find + sort + awk approach?
Share your approach in the comments! 🐧💻
📌 Tags
linux shell unix beginners
Top comments (0)