linux:bash_clean_up_system
#!/bin/bash logdir="/var/log/" stdlogs=$(find . -name "*.log" -type f) logfile="/var/log/cleanup.log" loguse=$(du -ksh /var/log/) line () { for i in {1..100}; do echo -n "$1"; done && printf "\n"; } datenow=$(date +"%d-%m-%Y %H:%M") # cd $logdir # # Write current usage to log file /var/log/cleanup.log line '-' > $logfile printf "$datenow /var/log before cleanup:\t $loguse\n" > $logfile # # Remove unused depencies # apt autoremove # # Empty all normal logfiles i.e those not sqeuenced # for i in $stdlogs ; do > $i ; done # # WARNING USE WITH EXTREME CAUTION # || || # \/ \/ # Remove -delete before going live to see whats getting deleted # find . -name "*.gz" -type f find . -name "*.gz" -type f -delete find . -name '*.[0-99]' -type f -delete find . -name '*.log*[0-99]' -type f -delete find . -name '*root.[0-99]*' -type f -delete # /\ /\ # || || # WARNING USE WITH EXTREME CAUTION # # Alternative way is to use one of the following # find . -name "*.ext" -exec /bin/rm {} \; # find . -name "*.ext" -type f -print0 | xargs -0 /bin/rm -f # # Clear journal log # journaluse=$(journalctl --disk-usage) printf "$datenow journal before cleanup:\t$journaluse\n" >> $logfile journalctl --rotate >/dev/null 2>&1 journalctl --vacuum-time=1d >/dev/null 2>&1 journaluse=$(journalctl --disk-usage) loguse=$(du -ksh /var/log/) printf "$datenow journal after cleanup:\t$journaluse\n" >> $logfile printf "$datenow /var/log after cleanup:\t $loguse\n" >> $logfile line '-' >> $logfile # # Prune logfile # sed -i 's/\/var\/log\///g' $logfile sed -i 's/Archived and active journals take up//g' $logfile sed -i 's/in the file system.//g' $logfile
linux/bash_clean_up_system.txt · Last modified: 12/01/2024 15:42 by Allan