DEV Community

Fahad Vahora
Fahad Vahora

Posted on

Linux Practical Set G Solution – Find Files Modified in Last 7 Days

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
  • find
  • sort
  • awk
  • ls
  • du
  • stat
  • 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:

  1. Search for files in the current directory.
  2. Find files modified within the last 7 days.
  3. Display the file names.
  4. 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
Enter fullscreen mode Exit fullscreen mode

means:

Find files modified less than 7 complete 24-hour periods ago.

The - before 7 means less than 7.

Common Examples

-mtime -1
Enter fullscreen mode Exit fullscreen mode

→ Modified within the last 1 day.

-mtime -7
Enter fullscreen mode Exit fullscreen mode

→ Modified within the last 7 complete 24-hour periods.

-mtime +7
Enter fullscreen mode Exit fullscreen mode

→ Modified more than 7 complete 24-hour periods ago.

For this practical, we use:

-mtime -7
Enter fullscreen mode Exit fullscreen mode

⚠️ Important: -mtime Uses 24-Hour Periods

find -mtime works with complete 24-hour periods, not simply calendar dates.

Therefore:

-mtime -7
Enter fullscreen mode Exit fullscreen mode

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 {} \;
Enter fullscreen mode Exit fullscreen mode

How It Works

find .
Enter fullscreen mode Exit fullscreen mode

Searches from the current directory.

-maxdepth 1
Enter fullscreen mode Exit fullscreen mode

prevents the search from going inside subdirectories.

-type f
Enter fullscreen mode Exit fullscreen mode

selects regular files only.

-mtime -7
Enter fullscreen mode Exit fullscreen mode

selects files modified within the last 7 complete 24-hour periods.

Finally:

-exec ls -lh {} \;
Enter fullscreen mode Exit fullscreen mode

runs ls -lh for every matching file.


🖥️ Example Output

Suppose the current directory contains:

notes.txt
program.sh
old.txt
data.csv
Enter fullscreen mode Exit fullscreen mode

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

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

How It Works

-print0
Enter fullscreen mode Exit fullscreen mode

separates file names using a null character.

This is useful for file names containing spaces.

For example:

my notes.txt
Enter fullscreen mode Exit fullscreen mode

can be processed safely.

Then:

du -h "$file"
Enter fullscreen mode Exit fullscreen mode

gets the file size in human-readable format.

For example:

4.0K    ./notes.txt
Enter fullscreen mode Exit fullscreen mode

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

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

Understanding stat

The command:

stat -c "%s" "$file"
Enter fullscreen mode Exit fullscreen mode

returns the file size in bytes.

For example:

4096
Enter fullscreen mode Exit fullscreen mode

means the file size is 4096 bytes.

Here:

%s → File size in bytes
Enter fullscreen mode Exit fullscreen mode

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

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

Understanding the find Output

The important part is:

-printf '%T@ %s %p\n'
Enter fullscreen mode Exit fullscreen mode

These format specifiers mean:

%T@ → Modification timestamp
%s  → File size in bytes
%p  → File path/name
Enter fullscreen mode Exit fullscreen mode

For example, the generated output may look like:

1754650200.123 2048 ./notes.txt
1754700300.456 4096 ./program.sh
1754750100.789 1024 ./data.txt
Enter fullscreen mode Exit fullscreen mode

Sorting the Files

The output is passed to:

sort -n
Enter fullscreen mode Exit fullscreen mode

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

formats the output.

Here:

$2 → File size
$3 → File name/path
Enter fullscreen mode Exit fullscreen mode

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

Here:

-n → Numeric sorting
-r → Reverse order
Enter fullscreen mode Exit fullscreen mode

Therefore:

sort -nr
Enter fullscreen mode Exit fullscreen mode

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

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

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

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

This works with complete 24-hour periods.

Using -mmin

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

Because:

7 days × 24 hours × 60 minutes = 10080 minutes
Enter fullscreen mode Exit fullscreen mode

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

This searches only the current directory.

Current Directory and Subdirectories

find . -type f -mtime -7
Enter fullscreen mode Exit fullscreen mode

This searches recursively.

Since the question specifically says:

in the current directory

we use:

-maxdepth 1
Enter fullscreen mode Exit fullscreen mode

⚠️ Common Mistakes

Mistake 1: Forgetting -type f

Don't use:

find . -mtime -7
Enter fullscreen mode Exit fullscreen mode

because the result can include directories.

Use:

find . -type f -mtime -7
Enter fullscreen mode Exit fullscreen mode

Mistake 2: Searching Inside Subdirectories

Don't use:

find . -type f -mtime -7
Enter fullscreen mode Exit fullscreen mode

if only the current directory should be searched.

Use:

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

Mistake 3: Using -mtime 7

These are different:

-mtime 7
Enter fullscreen mode Exit fullscreen mode

and:

-mtime -7
Enter fullscreen mode Exit fullscreen mode

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

sorts numeric values in ascending order.

sort -nr
Enter fullscreen mode Exit fullscreen mode

sorts numeric values in descending order.

Therefore:

sort -n  → Older → Newer
sort -nr → Newer → Older
Enter fullscreen mode Exit fullscreen mode

Mistake 5: Parsing ls Carelessly

Commands such as:

ls -lh | awk ...
Enter fullscreen mode Exit fullscreen mode

can become unreliable when file names contain spaces or special characters.

For safer file-name handling, use:

find ... -print0
Enter fullscreen mode Exit fullscreen mode

with:

read -d ''
Enter fullscreen mode Exit fullscreen mode

🎯 Practical Exam Tips

Remember this basic command:

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

It means:

.             → Current directory
-maxdepth 1   → Do not enter subdirectories
-type f       → Files only
-mtime -7     → Modified within the last 7 complete days
Enter fullscreen mode Exit fullscreen mode

For human-readable file size:

ls -lh filename
Enter fullscreen mode Exit fullscreen mode

or:

du -h filename
Enter fullscreen mode Exit fullscreen mode

For file size in bytes:

stat -c "%s" filename
Enter fullscreen mode Exit fullscreen mode

For sorting numeric values:

sort -n
Enter fullscreen mode Exit fullscreen mode

For reverse numeric sorting:

sort -nr
Enter fullscreen mode Exit fullscreen mode

📝 Quick Revision

Find recently modified files

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

Find files and display information

find . -maxdepth 1 -type f -mtime -7 -exec ls -lh {} \;
Enter fullscreen mode Exit fullscreen mode

Get human-readable size

du -h "$file"
Enter fullscreen mode Exit fullscreen mode

Get size in bytes

stat -c "%s" "$file"
Enter fullscreen mode Exit fullscreen mode

Sort by modification timestamp

sort -n
Enter fullscreen mode Exit fullscreen mode

Sort newest first

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

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

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)