Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Linux commands to find all files modified older than a certain date or time, remove, find largest files or folder in a human readable way.
## NOTE: when finding file by time, use one of the following flags:
# ctime : the time a file was changed, in hours. On most systems this timestamp cannot be altered by any user, so it is fairly reliable
# atime : the time a file was last accessed, in hours. This is also set by the system.
# mtime : the modified time of a file, in hours. This can be set by any user. So if you are looking for mailcious files, never use this flag.
# cmin : same as mtime, but in minutes.
# amin : same as atime, but in minutes.
# mmin : same as mtime, but in minutes.
# Find all files in current directory changed more than 8 hours ago (480 minutes)
find $PWD -mindepth 1 -type f -cmin +480
# Find all files in current directory modified more than 31 days ago
find $PWD -mindepth 1 -type f -ctime +31
# Find all files in current directory modified in the last 2 days, exclude any cache, log and media folders
find data/web/ -type f -ctime -2 -not -path "*/cache/*" -not -path "*/log/*" -not -path "*/media/*" -not -path "*/session/*"
# LIST filename and modified date of all files in current directory modified more than 8 hours ago (480 minutes)
find $PWD -mindepth 1 -type f -cmin +480 -exec stat -c "%n %y" {} \;
# LIST filename and modified date and highlight modified date of all files in current directory modified more than 8 hours ago (480 minutes)
find $PWD -mindepth 1 -type f -cmin +480 -exec stat -c "%n %y" {} \; | grep '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}'
# Find 10 largest folders
du -hsx * | sort -rh | head -10
# Find 10 largest folders, but do not list their subfolders
du -hx --max-depth=0 * | sort -rh | head -10
# Find 10 largest folders, and also list their subfolders 1 level deep
du -hx --max-depth=1 * | sort -rh | head -10
# Find 10 largest files and display size in a human readable way
find . -type f -exec du --human {} + | sort --human --reverse | head -10
# Find largest log files in current directory
find $PWD -name '*.log' -type f -exec ls -lh {} + | sort -nr | head -10
# --- DELETE !!!! ----- all files modified more than 8 hours ago (480 minutes), recursively from the current directory
find $PWD -mindepth 1 -type f -cmin +480 -exec rm {} \;
# Find used disk space
df -h
# Find any line in a file that does NOT contain a certain string, for instance 'Magento'
cat somefile.log | grep -v 'Magento'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment