Svuotare Automaticamente I Cestini Di Tutti Gli Utenti
Autotrash
Autotrash e' uno script che vi permette di tenere sotto controllo i cestini di tutti gli utenti o solo il vostro. Autotrash รจ su github qui: https://github.com/bneijt/autotrash
Per installarlo in tutto il sistema vi basta mandare in esecuzione i seguenti comandi:
$ sudo apt-get install pipx $ pipx install autotrash $ sudo ln -s ~/.local/pipx/venvs/autotrash/bin/autotrash /bin/autotrash
Ci sono tante altre opzioni disponibili che sono spiegate nel manuale qui: https://github.com/bneijt/autotrash/blob/main/doc/autotrash.md
Per controllare solo il vostro cestino vi basta il comando
$ autotrash -d 30 --trash_limit 1000
Per fare in modo che il controllo avvenga in automatico per tutti gli utenti, bisogna mettere il comando nella cartella /etc/profile.d/, questo file deve essere un normale file di testo, ad esempio autotrashpertutti.sh e deve avere solo il comando. Ad esempio, in modo che il cestino di ogni utente non sia maggiore di 1Gb:
/usr/bin/autotrash -td 30 --trash_limit 10000
Vecchia soluzione
Prima di autotrash avevo scritto il seguente script che permette di impostare quanto spazio concedere al cestino di ogni utente, e ripulirlo quando supera una certa dimensione. Magari vi puo' essere utile per altre necessita'.
Prima di tutto bisogna mettere questo script e chiamarlo delete_spaces.pl, metterlo in /usr/bin/:
#! /usr/bin/perl
use File::Find;
finddepth sub { # finddepth so directories are done AFTER their contents
my $old = $_;
tr/ /_/ or return; # don't do anything if you don't have spaces
-e and return; # don't rename over an existing file!
rename $old, $_ or warn "cannot rename $old to $_: $!";
}, "."; # starting directory
Spero possa essere utile a qualcuno, consiglio di metterlo in "/etc/cron.daily/" e renderlo eseguibile:
#! /bin/bash
# By in orginal idea of Frederic Connes
#This is a cript to mantain all Trash directories below the maximum granted by System Administrator
#released on GPL v.2 by Massimiliano Vessi (maxint@tiscali.it)
######################################################################
# Configuration #
# #
#Answer following question: #
######################################################################
#Where is local Trash in user home directory? #
trash=".local/share/Trash/files"
##########################################################################
#How much space you permit on Trash? (uncomment only the correct one) #
# 1 byte
#limit=1
#1kB
#limit=1024
#1MB
#limit=1048576
# 1GB
# limit=1073741824
# 10GB
# limit=10737418240
# 100GB
limit=107374182400
#######################################################################
#Where store temporary files? #
tmp_file=/tmp/svuota
tmp_file2=/tmp/svuota2
tmp_file3=/tmp/svuota3
users_file=/tmp/users_list
########################End Configuration##############################
#echo "Cleaning all user trashcans" > /dev/pts/0
#Create a file with all users
cut -f 1 -d : /etc/passwd > $users_file
for user in `cat $users_file`
do
#Do user's trash exist?
if [ -d "/home/$user/$trash" ] ; then
#here start the hard work
cd /home/$user/$trash
#Remove spaces in name files
/usr/bin/delete_spaces.pl
#Evaluate total space used by trash files
totalspace=`du -b -s | cut -f 1 `
if [ $totalspace -gt $limit ] ; then
#Let's create files list, ordering for time in reverse order (younger on top, older on bottom)
find -type f -printf "%T@ p\n" > $tmp_file
sort -g -r $tmp_file > $tmp_file2
#first a backup, something could go wrong
cp $tmp_file2 $tmp_file
#Initializing some variables
dim_total=0
lines_count=0
#Now count lines in our files list
lines_total=`wc -l $tmp_file |cut -f 1 -d \ `
#It isn't difficult: a new files list is created with all oldest files wich total exceed our limit (my english is awful... I know...)
while [ $dim_total -lt $limit ]
do
#read first line of new list
first_line=`head -n 1 $tmp_file2`
#read file dimension, remeber: "cut" field separator is space "\ "
dim_file=` echo $first_line | cut -f 2 -d \ `
#How much space, all file read untill now, use?
dim_total=`expr $dim_total + $dim_file`
#new list becomes shorter of 1 line (his first line)
lines_total=`expr $lines_total - 1`
tail -n $lines_total $tmp_file > $tmp_file2
done
#Now we have a new list with all file in exceed
cut -f 3 -d \ $tmp_file2 > $tmp_file3
#So, let's eliminate forever them
for file in `cat $tmp_file3`
do
rm -f $file
done
for file in `find -type d`
do
rmdir $file 2> /dev/null
done
fi
fi
done
