From f505b012c45bfff5c0034f7ba6f856545299805a Mon Sep 17 00:00:00 2001 From: Jean Froment Date: Sun, 19 Apr 2020 16:43:44 +0200 Subject: [PATCH] Add a maintenance script --- clean-fs.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 clean-fs.sh diff --git a/clean-fs.sh b/clean-fs.sh new file mode 100755 index 0000000..2bcf587 --- /dev/null +++ b/clean-fs.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# Usage: ./clean-fs.sh DIRECTORY +# Where DIRECTORY is the path of the directory to clean + +if [ -z "${1}" ] ;then + echo "[$0] Missing DIRECTORY parameter. Exiting..." + exit 1 +fi + +f="${1}" +result="$(file $f)" +if [[ $result == *"cannot open"* ]] || [[ $result != *"directory"* ]];then + echo "[$0] Directory not found (non-existing or is a file): ($result) "; + exit 1; +fi + +echo "[$0] Directory found: $1" + +echo "[$0] Listing files which ARE NOT video files..." +find $1 -maxdepth 10 -type f | grep -v -E "\.webm$|\.flv$|\.vob$|\.ogg$|\.ogv$|\.drc$|\.gifv$|\.mng$|\.avi$|\.mov$|\.qt$|\.wmv$|\.yuv$|\.rm$|\.rmvb$|/.asf$|\.amv$|\.mp4$|\.m4v$|\.mp*$|\.m?v$|\.svi$|\.3gp$|\.flv$|\.f4v$" + +while true; do + read -p "[$0] Do you want to REMOVE these files?" yn + case $yn in + [Yy]* ) find $1 -maxdepth 10 -type f | grep -v -E "\.webm$|\.flv$|\.vob$|\.ogg$|\.ogv$|\.drc$|\.gifv$|\.mng$|\.avi$|\.mov$|\.qt$|\.wmv$|\.yuv$|\.rm$|\.rmvb$|/.asf$|\.amv$|\.mp4$|\.m4v$|\.mp*$|\.m?v$|\.svi$|\.3gp$|\.flv$|\.f4v$" | xargs rm -f; break;; + [Nn]* ) echo "[$0] Skipping this part. Continuing...";; + * ) echo "[$0] Please answer yes or no.";; + esac +done + +echo "[$0] Listing subdirectories of $1 which have a size under 5 MB..." +find $1 -mindepth 1 -maxdepth 1 -type d -exec du -ks {} + | awk '$1 <= 5000' | cut -f 2- + +while true; do + read -p "[$0] Do you want to REMOVE these folders?" yn + case $yn in + [Yy]* ) find $1 -mindepth 1 -maxdepth 1 -type d -exec du -ks {} + | awk '$1 <= 5000' | cut -f 2- | xargs -d \\n rm -rf; break;; + [Nn]* ) echo "[$0] Skipping this part. Continuing...";; + * ) echo "[$0] Please answer yes or no.";; + esac +done + +echo "[$0] End of script." + +exit 0 \ No newline at end of file