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.
Edit the /etc/fstab file to ensure full access time logging. Open the file with a text editor:
sudo nano /etc/fstabLook 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
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).
After editing /etc/fstab, remount the filesystem to apply the changes:
sudo mount -o remount /Check the mount options again:
mount | grep ' on / 'You should see the root filesystem mounted without noatime or relatime.
List all files in the filesystem:
find / -type f > all_files.txtList files accessed in the last 180 days:
find / -type f -atime -180 > accessed_files.txtCompare the two lists to find unaccessed files:
grep -Fxv -f accessed_files.txt all_files.txt > unaccessed_files.txtHere’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"- Save the script to a file, for example,
find_unaccessed_files.sh. - Make the script executable:
chmod +x find_unaccessed_files.sh
- 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.