#! /bin/bash # SRC # https://help.ubuntu.com/community/Nautilus_Scripts # http://ubuntuforums.org/showthread.php?t=101859 # http://ubuntuforums.org/showthread.php?t=417978 # http://forums.archlinux.fr/topic1223.html # PRESENTATION: les + # annulation fonctionnelle (kill process fils, residu supprimé) # check de l'espace libre, des droits. # EXECUTION # à executer en mode graphique avec l'option %m dans nautilus-actions # ou lancer en CLI en se plaçant dans un repertoire où se trouve un gros fichier puis executer Copievers.sh grosFichier.avi # INITIALISATION copy="Copie en cours..." title_copy="Veuillez patienter..." title_success="Succès" title_error="Erreur" title_conflict="Conflit durant la copie" title_select="Selectionnez un répertoire" error="Une erreur est survenue lors de la copie" error_w="Vous n'avez pas les droits en écriture sur" error_s="Pas assez espace disque!" success="correctement copié(s)" progress="Copie en cours" conflict="Voulez-vous remplacer" cmd="cp -Rp" copied=0 nbFiles=0 # FONCTIONS function copie () { ORIG_SIZE=`du -k "$arg"|awk '{print $1}'` checkFreeSpace $ORIG_SIZE $cmd "$arg" "$location" & CP_SIZE=`du -k "$location/${arg//*\//}"|awk '{print $1}'` [ "$CP_SIZE" == "" ] && CP_SIZE=0 ( echo "0" sleep 0.2 while (( $CP_SIZE != $ORIG_SIZE )); do # un echo est obligatoire dans ce while echo "$(( $(( $CP_SIZE * 100 )) / $ORIG_SIZE ))"; sleep 0.2 CP_SIZE=`du -k "$location/${arg//*\//}"|awk '{print $1}'` [ "$CP_SIZE" == "" ] && CP_SIZE=0 done if [ "$CP_SIZE" == "$ORIG_SIZE" ]; then { echo "100" } fi ) | zenity --progress --auto-close --percentage=0 --text "$copy \"$arg\"..." # ^ barre de progression de zenity # v on controle si annulation ou pas. controlCp } # un paramètre: la taille du fichier function checkFreeSpace () { # location sera de la forme /, /tmp, /home, /opt... # on split le path pour trouver la partition part=`echo $location | cut -d'/' -f2` findit=`df | grep $part` # si findit est vide alors concerne la partition / if [ "$findit" == "" ]; then findit=`df / | grep /` fi tab=( $findit ) # point de montage se trouve sur 5 mountPoint=${tab[5]} # l'espace libre sur 3 freeSpace=${tab[3]} if [ $1 -gt $freeSpace ] then zenity --error --text="$error_s" --title "$title_error"; exit 1 fi } function controlCp () { # On peut utiliser $? ou ${PIPESTATUS[0]} if [ "$?" != "0" ]; then # si annulation on kill le fils et on sort du script killChildProcess # on vire le residu pathfile="$location/${arg//*\//}" # on evite les rm -R avec pour paramètre * ~ / . if [ ${#pathfile} -gt 1 ] && [ ${#location} -gt 1 ] && [ ${#arg} -gt 1 ]; then rm -R "$pathfile" fi exit 1 else copied=`expr $copied + 1` fi } function killChildProcess () { # normalement il n'y en a qu'un seul...au cas où. for child in $(ps --ppid $$ -o pid,cmd | grep -R "$cmd" | awk '{print $1}') do kill $child done } # TRAITEMENT # on renseigne le repertoire de destination while ((1)) do location=`zenity --file-selection --directory --title="$title_select"` if [ "$?" = 0 ]; then # si on a pas annuler...on check les droits en écriture if [ -w "$location" ]; then for arg do nbFiles=`expr $nbFiles + 1` # on vérifie si le fichier courant n'existe pas à la destination if [ -e "$location/${arg//*\//}" ]; then # s'il existe message -> conflit zenity --question --title="$title_conflict" --text="$conflict $location/${arg//*\//} ?" if [ "$?" = 0 ]; then # si on souhaite tout de même écraser copie fi else # s'il n'existe pas on copie copie fi done # on check copied / nbFiles if [ $copied -gt 0 ]; then zenity --info --text="$copied/$nbFiles $success" --title "$title_success"; fi break else { zenity --question --title="$title_error" --text="$error_w $location" if [ "$?" = 1 ]; then { exit 1 } fi } fi fi done