Skip to content

Instantly share code, notes, and snippets.

@ronin1
Created October 7, 2023 00:32
Show Gist options
  • Save ronin1/ccf75dcad1ee7473730d5fd08acb477c to your computer and use it in GitHub Desktop.
Save ronin1/ccf75dcad1ee7473730d5fd08acb477c to your computer and use it in GitHub Desktop.
Bash Script to Remove Day old Files in /tmp directory that has no open file handles
#!/bin/bash
# Meant to be run nightly by a cron job on this box. This script will:
# - find a list of files that are 1 day old and has no current file handles attached
# - log these files into a: deleted.<day_of_the_month>.log file so we have a max log count (no need to clean up)
# - delete the files from disk
DRY_RUN=${DRY_RUN:-'0'}
CLR_TMP_DIR=${CLR_TMP_DIR:-'/tmp'}
CLR_TMP_LOG=${CLR_TMP_LOG:-"${HOME}/clr_tmp"}
CLR_ACTIVE_LOG=${CLR_ACTIVE:-"${CLR_TMP_LOG}/active_handles.log"}
rem_dt=$(date +'%d')
CLR_REM_LOG=${CLR_REM_LOG:-"${CLR_TMP_LOG}/remove_files_${rem_dt}.target"}
echo '===================================================='
echo "Starting at $(date)"
if [ "${DRY_RUN}" != '0' ]; then
echo 'DRY RUN!'
fi
echo '----before----'
du -sh "${CLR_TMP_DIR}"
echo '--------------'
echo "Cleaning orphan files in: '${CLR_TMP_DIR}' using log path -> '${CLR_TMP_LOG}'"
mkdir -p $CLR_TMP_LOG
# build a list of all files with active handles
lsof +D $CLR_TMP_DIR | awk '{ if(NR>1)print $9 }' | sort | uniq > "${CLR_ACTIVE_LOG}"
# remove the files from the above list from this list of all files that re created -1 days ago
find $CLR_TMP_DIR -mtime +1 -type f | grep -v -f "${CLR_ACTIVE_LOG}" > "${CLR_REM_LOG}"
# remove all entries in file with this pattern matched
grep -v "snap" "${CLR_REM_LOG}" > "${CLR_REM_LOG}.tmp" && mv "${CLR_REM_LOG}.tmp" "${CLR_REM_LOG}"
rem_count=$(wc -l "${CLR_REM_LOG}" | tr ' ' '\n' | head -1)
echo "List of files to be removed: '${CLR_REM_LOG}' @ ${rem_count} files"
if [ "${rem_count}" != "0" ]; then
echo "Removing fies from list now"
if [ "${DRY_RUN}" != '0' ]; then
echo "Dry Run Only!"
fi
rem_count=0
rem_done="${CLR_REM_LOG}.log"
echo $(date) > "${rem_done}"
echo '=================================' >> "${rem_done}"
# load the target list file and loop through each line (should be a full file path)
while IFS= read -r line; do
if [ "${DRY_RUN}" = '0' ]; then
# if not dry run, actually delete the file!
rm -f "${line}"
fi
rem_count=$(expr $rem_count + 1)
# log each deleted file into a log
echo "${line}" >> "${rem_done}"
done < "${CLR_REM_LOG}"
echo "DONE: See all ${rem_count} removed files here: '${rem_done}'"
echo '----after----'
du -sh "${CLR_TMP_DIR}"
echo '--------------'
else
echo "SKIP: Nothing to remove!"
fi
echo "Ended at $(date)"
echo ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment