How to Find Changed Files in Linux

3 min read .

Tracking changes to files is crucial for system administration, security auditing, and data management in Linux. Whether you need to monitor modifications for backup purposes, compliance, or troubleshooting, identifying recently changed files can streamline your tasks. In this guide, we’ll explore various methods to find files that have been changed in Linux, helping you stay on top of file modifications efficiently.

Why Track Changed Files?

Monitoring file changes can be important for several reasons:

  • Security: Detect unauthorized modifications or potential breaches.
  • Backup: Identify files that need to be backed up since the last backup.
  • System Maintenance: Track changes during updates or software installations.
  • Troubleshooting: Diagnose issues related to file modifications.

Using find Command to Identify Changed Files

The find command is a versatile tool for searching files based on various criteria, including modification times.

1. Find Files Changed in the Last Hour

To locate files modified in the last hour, use:

find /path/to/directory -type f -mmin -60
  • -type f: Search for files (not directories).
  • -mmin -60: Files modified in the last 60 minutes.

2. Find Files Changed in the Last 24 Hours

To find files modified in the last day:

find /path/to/directory -type f -mtime -1
  • -mtime -1: Files modified in the last 24 hours.

3. Find Files Changed in the Last 7 Days

To search for files modified in the last week:

find /path/to/directory -type f -mtime -7
  • -mtime -7: Files modified in the last 7 days.

Using ls Command to List Recently Modified Files

The ls command with specific options can help you list files by modification time.

1. List Files in Order of Modification

To list files in the current directory, sorted by modification time (newest first):

ls -lt
  • -l: Long listing format.
  • -t: Sort by modification time.

2. List Files Modified Today

To list files modified today:

ls -lt --time-style=long-iso | grep "$(date +%Y-%m-%d)"
  • --time-style=long-iso: Display modification time in a long ISO format.
  • grep "$(date +%Y-%m-%d)": Filter results for today’s date.

Using find with ls for Detailed Information

To get detailed information about recently modified files using find and ls together:

find /path/to/directory -type f -mtime -1 -exec ls -l {} +
  • -exec ls -l {} +: Execute ls -l on each file found by find.

Monitoring File Changes with inotifywait

For real-time monitoring of file changes, use inotifywait, which is part of the inotify-tools package.

1. Install inotify-tools

To install inotify-tools:

  • On Debian-based systems (Ubuntu, Debian):

    sudo apt-get install inotify-tools
  • On Red Hat-based systems (CentOS, Fedora):

    sudo yum install inotify-tools

2. Monitor a Directory for Changes

To monitor a directory for changes:

inotifywait -m /path/to/directory
  • -m: Monitor the directory continuously.

3. Monitor Specific Events

To monitor specific events such as modifications:

inotifywait -m -e modify /path/to/directory
  • -e modify: Monitor only modification events.

Using auditd for Comprehensive Monitoring

For more extensive file change monitoring, consider using auditd, the Linux Audit Daemon, which provides detailed logs of file access and changes.

1. Install auditd

To install auditd:

  • On Debian-based systems (Ubuntu, Debian):

    sudo apt-get install auditd
  • On Red Hat-based systems (CentOS, Fedora):

    sudo yum install auditd

2. Configure auditd to Monitor Files

To add a rule to monitor changes to a specific file or directory:

sudo auditctl -w /path/to/file -p wa
  • -w: Watch the file or directory.
  • -p wa: Monitor write and attribute changes.

3. View Audit Logs

To view audit logs:

sudo ausearch -m FILE
  • -m FILE: Search for file-related audit events.

Conclusion

Finding and tracking changed files is essential for managing Linux systems effectively. By using commands like find, ls, and tools such as inotifywait and auditd, you can efficiently monitor file modifications, ensuring you stay informed about changes and can take appropriate actions as needed. Whether you’re performing routine checks, security audits, or system maintenance, mastering these techniques will enhance your Linux file management skills.

Tags:
Linux

See Also

chevron-up