Linuxpedia

Svuotare I Cestini Di Tutti Gli Utenti

Ho scritto il seguente script che permette di impostare qunato spazio concedere al cestino di ogni utente, e ripulirlo quando supera una certa dimensione.

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 correct one)    #
 # 1 byte
 #limit=1
 #1kB
 #limit=1024
 #1MB
 #limit=1048576
 # 1GB
 limit=1073741824
 #######################################################################
 #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

HomePage

Guide

Altro

Translate:

PubblicitĂ :

Licenza Creative Commons
DuckDuckGo

edit SideBar

edit TopNav

Blix theme adapted by David Gilbert, powered by PmWiki