Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save farzadhallaji/a324d1e2ea8e60c727caf59dcb697138 to your computer and use it in GitHub Desktop.
Save farzadhallaji/a324d1e2ea8e60c727caf59dcb697138 to your computer and use it in GitHub Desktop.

Enabling Access Time Logging and Identifying Unaccessed Files on Debian

Step 1: Check Current Mount Options

To check if access time logging is enabled, use the mount command:

mount | grep ' on / '

If you see noatime or relatime, access time logging is not fully enabled.

Step 2: Edit /etc/fstab

Edit the /etc/fstab file to ensure full access time logging. Open the file with a text editor:

sudo nano /etc/fstab

Look for the line that mounts the root filesystem. It will look something like this:

UUID=xxxx-xxxx-xxxx-xxxx / ext4 defaults 0 1

Or it might include noatime or relatime:

UUID=xxxx-xxxx-xxxx-xxxx / ext4 defaults,noatime 0 1

Step 3: Modify Mount Options

Ensure the root filesystem is mounted with default options:

UUID=xxxx-xxxx-xxxx-xxxx / ext4 defaults 0 1

Save the file and exit the text editor (in nano, press Ctrl+X, then Y, then Enter).

Step 4: Remount the Filesystem

After editing /etc/fstab, remount the filesystem to apply the changes:

sudo mount -o remount /

Step 5: Verify the Changes

Check the mount options again:

mount | grep ' on / '

You should see the root filesystem mounted without noatime or relatime.

Step 6: Identify Unaccessed Files

Find All Files

List all files in the filesystem:

find / -type f > all_files.txt

Find Files Accessed in the Last Six Months

List files accessed in the last 180 days:

find / -type f -atime -180 > accessed_files.txt

Identify Files Not Accessed in the Last Six Months

Compare the two lists to find unaccessed files:

grep -Fxv -f accessed_files.txt all_files.txt > unaccessed_files.txt

Optional: Script to Automate the Process

Here’s a script to automate the process:

#!/bin/bash

# Define the output files
ALL_FILES="all_files.txt"
ACCESSED_FILES="accessed_files.txt"
UNACCESSED_FILES="unaccessed_files.txt"

# Step 1: Find all files
echo "Finding all files..."
find / -type f > "$ALL_FILES"

# Step 2: Find files accessed in the last 180 days
echo "Finding files accessed in the last 180 days..."
find / -type f -atime -180 > "$ACCESSED_FILES"

# Step 3: Identify files not accessed in the last 180 days
echo "Identifying files not accessed in the last 180 days..."
grep -Fxv -f "$ACCESSED_FILES" "$ALL_FILES" > "$UNACCESSED_FILES"

# Optional: Provide a summary
echo "Summary:"
echo "Total files found: $(wc -l < "$ALL_FILES")"
echo "Files accessed in the last 180 days: $(wc -l < "$ACCESSED_FILES")"
echo "Files not accessed in the last 180 days: $(wc -l < "$UNACCESSED_FILES")"

# Output the result
echo "Files not accessed in the last six months are listed in $UNACCESSED_FILES"

Running the Script

  1. Save the script to a file, for example, find_unaccessed_files.sh.
  2. Make the script executable:
    chmod +x find_unaccessed_files.sh
  3. Run the script:
    ./find_unaccessed_files.sh

This will generate a file named unaccessed_files.txt containing the list of files that haven't been accessed in the last six months.

Note: Review the list carefully before deleting any files to avoid removing important system or application files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment