Published on

How to Display the 20 Last Modified Files and Folders in Linux

Authors
  • Mehdi Akiki avatar
    Name
    Mehdi Akiki
    Twitter

When working in Linux, knowing how to quickly locate recently modified files can be crucial for troubleshooting, monitoring changes, or simply managing your system effectively. This guide will cover how to display the 20 most recently modified files and folders using common Linux commands.

Display the 20 Last Modified Files and Folders

The ls command is one of the most commonly used tools for listing files and directories. To display the 20 most recently modified files and folders, you can use the following command:

ls -lt | head -n 20

Let's break down what this command does:

  • ls -l: Lists files and folders in a long format, showing additional details like permissions, owner, size, and modification date.
  • -t: Sorts the output by modification time, with the newest files and folders displayed first.
  • head -n 20: Shows the first 20 lines of the output, effectively giving you the 20 most recently modified files or directories.

This combination is very helpful for quickly identifying which files were modified recently, especially when trying to understand changes on a system or debug an issue.

Alternative Commands to List Modified Files

Here are a few other useful commands that can help you find recently modified files and folders, each with slightly different functionality.

Using find Command

The find command provides a more powerful way to search for files based on their modification times. If you want to find files modified within the last day, you could use:

find . -type f -mtime -1

Explanation:

  • .: Specifies the current directory as the starting point.
  • -type f: Restricts the search to files only (not directories).
  • -mtime -1: Finds files modified within the last 24 hours.

You can also use the -printf option to format the output and include the modification time.

Using stat Command

The stat command provides detailed information about files, including the last modification time:

stat filename

This command will display various metadata, including access, modification, and change times. It's useful for a more in-depth view of a single file's history.

watch with ls

If you want to monitor file changes in real time, you can use the watch command along with ls to update the listing every few seconds:

watch -n 5 'ls -lt | head -n 20'

Explanation:

  • watch -n 5: Runs the command every 5 seconds.
  • 'ls -lt | head -n 20': Executes the command to display the last 20 modified files or folders.

This is particularly useful for seeing files being updated live, such as log files or temporary files created by running processes.

Summary

Finding recently modified files and directories is a fundamental skill for Linux users, particularly system administrators and developers. By mastering commands like ls -lt, find, and stat, you can efficiently track changes on your system, troubleshoot issues, or ensure that files are up to date. Whether you are checking logs, monitoring system activity, or just curious about recent changes, these tools will help you manage your files like a pro.

FAQ

1. Can I display more than 20 recently modified files?

Yes, simply change the number in head -n 20 to whatever value you need, for example head -n 50 to display the 50 most recently modified files.

2. How can I list only files and exclude directories?

You can use the find command with -type f to list only files. For example: find . -type f -mtime -1 will find only files modified in the last 24 hours.

3. How can I sort by modification time in reverse order?

Add the -r flag to the ls command to reverse the order. For example: ls -ltr | head -n 20 will display the oldest modified files first.

4. Can I find modified files across multiple directories?

Yes, using find /path/to/directories -type f -mtime -1 allows you to search for modified files across multiple directories.

5. How can I automate the process of finding recently modified files?

You can create a simple shell script that runs the ls or find commands and schedules it using cron to automate the process at specific intervals.

6. How can I find the last modified files larger than a specific size?

You can use the find command with the -size option. For example: find . -type f -mtime -1 -size +100M will find files modified in the last 24 hours that are larger than 100 MB.

7. Can I list the last modified files by a specific user?

Yes, you can use find with the -user option. For example: find . -type f -mtime -1 -user username will list files modified in the last 24 hours by a specific user.

8. How can I exclude certain directories from the search?

You can use the find command with the -prune option. For example: find . -path ./excluded_directory -prune -o -type f -mtime -1 will exclude excluded_directory from the search.

9. How can I view the modification time in a human-readable format?

You can use the --time-style flag with ls. For example: ls -lt --time-style=long-iso will display modification times in a more readable format.

10. Can I combine multiple conditions in the find command?

Yes, you can combine multiple conditions using options like -and or -or. For example: find . -type f -mtime -1 -and -size +50M will find files modified in the last 24 hours and larger than 50 MB.

I build and scale reliable production systems. Open to full-time and freelance work with U.S.-based teams that value ownership and execution.

Got something in mind?

Book a Discovery Call