ibuclaw
May 9th, 2008, 05:30 AM
Hi all,
To admins, move this thread elsewhere if you feel that there is a better forum to discuss it.
To everyone else.
Basically, I've been sitting on a home-made utility for the past month without much thought about what to do with it.
So I've decided that it's probably best to just put it out in the open free for everyone to play with and improve.
I don't really know much of a place where, but here seemed like a good place to me.
As the title says, it's a command line trash app, simply made to replace the "unforgivable" rm app for Linux (Or, at least to give yourself a second chance to restore the file).
It is fully written in BASH (with a small sprinkle of Perl now), so it is perfectly integrate-able, just copy the scripts into the /usr/local/bin directory and the names of the commands are in your reach of power.
It's integrated with the Gnome folders, so trash a file/folder in nautilus, and you can restore that file to it's original location with this app in the commandline.
It also comes with other slick little devices to that have helped, at least me, to gain some form of productivity when clearing out unwanted sections of my home folder in the command line.
For those who can read source:
Here is the main jelly, completely usable as is:
Although it's not the complete package, it is what every other script branches into.
The source here is the testing version (v0.2-4f) with various fixes that both I have made and people have sent me.
It also is the starting of implementing the FreeDesktop.org specifications for how trash should be handled.
So if anyone has any ideas on what I could add. Please send them.
It is currently in the middle of being commented on (pure laziness is all thats stopping me :)). So for the moment, you still have to guess what everything does ;)
#!/usr/bin/env bash
# License: GPLv3 #
# Author: Iain Bucław #
#######################
### Main Variables ###
export uid=$UID
export uname=$USER
export excludefs="-x tmpfs -x fuse.gvfs-fuse-daemon"
export XDG_DATA_HOME=".local/share"
[ ! "$cwd" ] && export cwd=$PWD
###
### Other Variables ###
export Interact="0"
export OutDir=
###
### Functions Section ###
setdir()
{
for topdir in $(df $excludefs 2>/dev/null | awk 'FS="% "{print $2}' | tac)
do export where="${1#$topdir}"
if [ "$where" != "$1" ]
then break
fi
done
case $topdir in
"/home")
if [ $uname == root ]
then export Main="/home/.Trash-$uid"
else export Main="/home/$uname/$XDG_DATA_HOME/Trash"
fi
;;
"/")
if [ $uname == root ]
then export Main="/root/$XDG_DATA_HOME/Trash"
else export Main="/tmp/.Trash-$uid"
fi
;;
*)
export Main="$topdir/.Trash-$uid"
;;
esac
export Files="$Main/files"
export Info="$Main/info"
if [ ! -e "$Main" ]
then mkdir -p "$Main/files/../info"
fi
}
trash() # Main trash function.
{
mvtrash() # Sub-function moves file to trash and writes data to info file.
{
while [ ! -z "$1" ]
do if [ "${1: -1}" == "/" ]
then export n1="${1%/}"
else export n1="$1"
fi; if [ -e "$Files/${n1##*/}" ]
then export n=2
while [ -e "$Files/${n1##*/}.$n" ]
do let "n++"
done
mv "$Path" "$Files/${n1##*/}.$n" 2>/dev/null
export exitnum="$?"
export n="."$n
else mv "$Path" "$Files"
export exitnum="$?"
export n=""
fi;
printf "%s\n" "[Trash Info]" > "$Info/${n1##*/}$n.trashinfo"
printf "%s\n" "Path=$Path" >> "$Info/${n1##*/}$n.trashinfo"
printf "%s\n" "DeletionDate=$DelDate" >> "$Info/${n1##*/}$n.trashinfo"
if [ "$exitnum" -eq 0 ]
then printf "%s\n" " Moved $n1 to Trash..."
else printf "%s\n" " Cannot Move file. $n1 Copied to Trash..."
fi
shift
done
}
while [ "$#" -gt "0" ]
do if [ -e "$1" ]
then export DelDate=`date +%FT%T`
if [ ! "${1%%/*}" ] || [ "${1:0:3}" == "../" ]
then cd "${1%/*}/"
export Path="$PWD"/"${1/*\/}"
else export Path="$cwd"/"$1"
fi
setdir "$Path"
if [ "$Interact" = "0" ]
then mvtrash "$1"
else printf "%s" " Trash Item '$1'? [Y/N]: "
read -n 1 input
echo
case "$input" in
y|Y)
mvtrash "$1"
;;
*)
printf "%s\n" " Skipping $1..."
;;
esac
fi
else printf "%s\n" " Cannot Move $1. File(s) Don't Exist..."
fi
shift
done
}
restore() # Main restore section.
{
rmain()
{
checkpath()
{
mvorigin() # Retrieves the origin of the file and restores it.
{
export Origin=`grep Path "$Info/$1.trashinfo"` && export Origin="${Origin/Path=/}"
mkdir -p ${Origin%/*}
mv "$Files/$1" "$Origin"
rm "$Info/$1.trashinfo"
}
while [ "$#" -gt "0" ]
do if [ ! $OutDir ]
then if [ -f "$Info/$1.trashinfo" ]
then mvorigin "$1"
printf "%s\n" " Restoring $1..."
else printf "%s\n" " No Information file for $1. Cannot Restore..."
fi
else mv "$Files/$1" "$OutDir"
printf "%s\n" " Restoring $1 to current path..."
fi
shift
done
}
while [ "$#" -gt "0" ]
do for i in `find "$Files" -maxdepth 1 -iname "$1" -exec echo {} \; | sed 's/ /?/g'`
do if [ "$i" != "$Files" ]
then export files=1
checkpath "$(basename "$i")"
fi
done
[ "$files" ] || printf "%s\n" " $1 is not in Trash..."
export files=""
shift
done
}
if [ "$1" == "-o" ]
then export OutDir="$cwd"
shift
fi
cd "$Files"
rmain "$@"
}
list()
{
lmain()
{
export count=0
for h in .*
do if [ "$h" != "." ] && [ "$h" != ".." ] && [ "$h" != "*" ]
then printf "\033[37m %-20s\n" "$h"
printf "\033[0m"
let "count++"
fi
done
for i in *
do if [ "$i" != "*" ]
then if [ -h "$i" ]
then printf "\033[1;36m %-20s\n" "$i"
elif [ -x "$i" ]
then if [ ! -d "$i" ]
then printf "\033[1;32m %-20s\n" "$i"
else printf "\033[1;34m %-20s\n" "$i"
fi
else colour "$i" "$i"
printf "\n"
fi
printf "\033[0m"
let "count++"
fi
done
if [ $count -eq "0" ]
then printf "%s\n" " *"
fi
}
lvmain()
{
getinfo()
{
split()
{
export input="$@"
export Origin=${input#[*=} && export Origin=${Origin% *}
export DelDate=${input##[*=} && export DelDate="${DelDate:0:10}"
export DelTime=${input: -8}
}
split $(cat "$Info/$1.trashinfo")
if [ "${Origin: -1}" == "/" ]
then export Origin="${Origin%/}"
fi
export Origin=${Origin/$1/} && export Origin=${Origin%${1/%.*/}}
export Origin="${Origin##${1##${1/.*/}}}"
if [ "${#Origin}" -gt "35" ]
then export Origin="${Origin:0:33}*"
fi
printf " %-35s %s\n" "$Origin" "$DelDate | $DelTime"
}
for i in `find -maxdepth 1 -exec echo {} \; | sed 's/ /?/g;s/.\///g' | sort`
do if [ "$i" != "." ]
then if [ "${#i}" -gt "20" ]
then export n=17
export j="${i:0:$n}*${i##*.}"
while [ $n -gt "0" ]
do let "n--"
if [ "${#j}" -gt "20" ]
then export j="${i:0:$n}*${i##*.}"
fi
done
if [ "${#j}" -gt "20" ]
then export j="${i:0:19}*"
fi
else export j="$i"
fi
if [ -h "$i" ]
then printf "\033[1;36m %-20s" "$j"
elif [ -x "$i" ]
then if [ ! -d "$i" ]
then printf "\033[1;32m %-20s" "$j"
else printf "\033[1;34m %-20s" "$j"
fi
else colour "$i" "$j"
fi
printf "\033[0m"
if [ -f "$Info/$i.trashinfo" ]
then getinfo "$i"
else printf "%-43s %s\n" " No Information Availiable" "N/A | N/A"
fi
let "count++"
fi
done
}
colour()
{
case "${1##*.}" in
7z|ace|arj|bz|bz2|cpio|deb|dz)
printf "\033[1;31m %-20s" "$2"
;;
gz|jar|lzh|lzma|rar|rpm|rz|svgz)
printf "\033[1;31m %-20s" "$2"
;;
tar|taz|tbz2|tgz|tz|z|Z|zip|zoo)
printf "\033[1;31m %-20s" "$2"
;;
bmp|gif|jpeg|jpg|mng|pbm|pcx|pdf)
printf "\033[1;35m %-20s" "$2"
;;
pgm|ps|png|ppm|svg|tga|tif|tiff|xbm)
printf "\033[1;35m %-20s" "$2"
;;
desktop|directory)
printf "\033[1;36m %-20s" "$2"
;;
aac|au|flac|mid|midi|mka|mp3|mpc|ogg|ra|wav)
printf "\033[1;33m %-20s" "$2"
;;
asf|avi|dl|flc|fli|gl|m2v|m4v|mkv)
printf "\033[1;33m %-20s" "$2"
;;
mov|mp4|mp4v|mpeg|mpg|nuv|ogm|qt)
printf "\033[1;33m %-20s" "$2"
;;
rm|rmvb|vob|wmv|xcf|xwd|yuv)
printf "\033[1;33m %-20s" "$2"
;;
*)
printf "\033[37m %-20s" "$2"
;;
esac
}
export title=0
export count=0
for topdir in $(df $excludefs 2>/dev/null | awk 'FS="% "{print $2}')
do if [ -e "$topdir" ]
then case "$topdir" in
"/home")
if [ $uname == root ]
then export Main="/home/.Trash-$uid"
else export Main="/home/$uname/$XDG_DATA_HOME/Trash"
fi
;;
"/")
if [ $uname == root ]
then export Main="/root/$XDG_DATA_HOME/Trash"
else export Main="/tmp/.Trash-$uid"
fi
;;
*)
export Main=$(echo "$topdir/.Trash-$uid")
;;
esac
if [ -e "$Main" ]
then export Files="$Main/files"
export Info="$Main/info"
if [ "$1" == "-v" ]
then if [ $title -eq 0 ]
then printf " %-20s %-35s %s\n" "Name" "Original-Path" "Deletion-Date & Time"
export title=1
fi
cd "$Files"
lvmain
else
cd "$Files"
printf "\033[1;37m%s" "$Main"
printf "\033[0m\n"
lmain
fi
fi
fi
done
if [ "$1" == "-v" ]
then if [ $count -eq 0 ]
then printf "%-20s %-43s %s\n" " *" " Trash is Empty" "N/A | N/A"
fi
fi
}
emptytrash()
{
printf "%s\n" " Removing Files..."
rm -rf "$Files"
if [ "$?" -eq "0" ]
then rm -rf "$Info"
printf "%s\n" " Removing Config..."
mkdir -p "$Files"
mkdir -p "$Info"
printf "%s\n" " Trash is Now Empty."
else printf "%s\n" "Error, You are not the owner of one or more files..."
printf "%s" "To fix this, type in: "
printf "%s\n" "cd $Files && sudo chown $user:$user * -R"
exit 1
fi
}
empty()
{
emain()
{
remove()
{
rm -rf "$Files/$1"
rm -rf "$Info/$1.trashinfo"
}
while [ "$#" -gt "0" ]
do remove "$1"
printf "%s\n" " Removing $1..."
shift
done
}
cd "$Files"
while [ "$#" -gt "0" ]
do for i in `find "$Files" -maxdepth 1 -iname "$1" -exec echo {} \; | sed 's/ /?/g'`
do export files=1
emain "$(basename "$i")"
done
[ "$files" ] || printf "%s\n" " $1 is not in Trash..."
export files=""
shift
done
}
clean() # Main clean function.
{
mvclean() # Sub-function moves file to trash and writes data to info file.
{
while [ ! -z "$1" ]
do if [ "${1: -1}" == "/" ] # Remove tailing "/" from filename (ie: "test/")
then export n1="${1%/}"
else export n1="$1"
fi; if [ -e "$Files/${n1##/*/}" ] # Checks if file already exists in trash.
then export n=2
while [ -e "$Files/${n1##/*/}.$n" ] # Renames file numerically until it is unique.
do let "n++"
done
mv "$1" "$Files/${n1##/*/}.$n"
export n="."$n
else mv "$1" "$Files"
export n=""
fi; # This section stores recovery info about the file.
printf "%s\n" "[Trash Info]" > "$Info/${n1##/*/}$n.trashinfo"
printf "%s\n" "Path=$Path" >> "$Info/${n1##/*/}$n.trashinfo"
printf "%s\n" "DeletionDate=$DelDate" >> "$Info/${n1##/*/}$n.trashinfo"
printf "%s\n" " Moved $n1 to Trash..."
shift
done
}
while [ "$#" -gt "0" ]
do if [ -e "$1" ]
then export DelDate=`date +%FT%T` # Gets the time of moving the file.
if [ ! "${1%%/*}" ] # Checks for path of file. If doesn't exist, it is created.
then export Path="$1"
else export Path=$cwd/"$1"
fi
mvclean "$1"
fi
shift
done
}
###
### Main Logic Section ###
case "$1" in
-i)
export Interact="1"
shift
setdir "$cwd"
trash "$@"
;;
-r)
shift
setdir "$cwd" # FIX ME
restore "$@"
;;
-ra)
setdir "$cwd" # FIX ME
restore "*"
;;
-roa)
setdir "$cwd" # FIX ME
restore -o "*"
;;
-ro)
shift
setdir "$cwd" # FIX ME
restore -o "$@"
;;
-l)
list
;;
-lv)
list -v
;;
-e)
shift
setdir "$cwd" # FIX ME?
if [ "$#" == "0" ]
then emptytrash
else empty "$@"
fi
;;
-c)
cd "$cwd"
setdir "$cwd"
clean "#"* *~ .*~ *.bak *.log
;;
-ct)
cd "$cwd"
setdir "$cwd"
clean "#"* *~ .*~
;;
-cb)
cd "$cwd"
setdir "cwd"
clean *.bak
;;
-cl)
cd "$cwd"
setdir "$cwd"
clean *.log
;;
*)
trash "$@"
;;
esac
###
exit 0
For everyone else, feel free to try it out in the attachment I've added. (Readme guide on usage included). And let me know what you think of it.
Regards
Iain
Download:
trashapp-0.2-4b.tar.gz (http://ubuntuforums.org/attachment.php?attachmentid=72697&stc=1&d=1212431633)
To admins, move this thread elsewhere if you feel that there is a better forum to discuss it.
To everyone else.
Basically, I've been sitting on a home-made utility for the past month without much thought about what to do with it.
So I've decided that it's probably best to just put it out in the open free for everyone to play with and improve.
I don't really know much of a place where, but here seemed like a good place to me.
As the title says, it's a command line trash app, simply made to replace the "unforgivable" rm app for Linux (Or, at least to give yourself a second chance to restore the file).
It is fully written in BASH (with a small sprinkle of Perl now), so it is perfectly integrate-able, just copy the scripts into the /usr/local/bin directory and the names of the commands are in your reach of power.
It's integrated with the Gnome folders, so trash a file/folder in nautilus, and you can restore that file to it's original location with this app in the commandline.
It also comes with other slick little devices to that have helped, at least me, to gain some form of productivity when clearing out unwanted sections of my home folder in the command line.
For those who can read source:
Here is the main jelly, completely usable as is:
Although it's not the complete package, it is what every other script branches into.
The source here is the testing version (v0.2-4f) with various fixes that both I have made and people have sent me.
It also is the starting of implementing the FreeDesktop.org specifications for how trash should be handled.
So if anyone has any ideas on what I could add. Please send them.
It is currently in the middle of being commented on (pure laziness is all thats stopping me :)). So for the moment, you still have to guess what everything does ;)
#!/usr/bin/env bash
# License: GPLv3 #
# Author: Iain Bucław #
#######################
### Main Variables ###
export uid=$UID
export uname=$USER
export excludefs="-x tmpfs -x fuse.gvfs-fuse-daemon"
export XDG_DATA_HOME=".local/share"
[ ! "$cwd" ] && export cwd=$PWD
###
### Other Variables ###
export Interact="0"
export OutDir=
###
### Functions Section ###
setdir()
{
for topdir in $(df $excludefs 2>/dev/null | awk 'FS="% "{print $2}' | tac)
do export where="${1#$topdir}"
if [ "$where" != "$1" ]
then break
fi
done
case $topdir in
"/home")
if [ $uname == root ]
then export Main="/home/.Trash-$uid"
else export Main="/home/$uname/$XDG_DATA_HOME/Trash"
fi
;;
"/")
if [ $uname == root ]
then export Main="/root/$XDG_DATA_HOME/Trash"
else export Main="/tmp/.Trash-$uid"
fi
;;
*)
export Main="$topdir/.Trash-$uid"
;;
esac
export Files="$Main/files"
export Info="$Main/info"
if [ ! -e "$Main" ]
then mkdir -p "$Main/files/../info"
fi
}
trash() # Main trash function.
{
mvtrash() # Sub-function moves file to trash and writes data to info file.
{
while [ ! -z "$1" ]
do if [ "${1: -1}" == "/" ]
then export n1="${1%/}"
else export n1="$1"
fi; if [ -e "$Files/${n1##*/}" ]
then export n=2
while [ -e "$Files/${n1##*/}.$n" ]
do let "n++"
done
mv "$Path" "$Files/${n1##*/}.$n" 2>/dev/null
export exitnum="$?"
export n="."$n
else mv "$Path" "$Files"
export exitnum="$?"
export n=""
fi;
printf "%s\n" "[Trash Info]" > "$Info/${n1##*/}$n.trashinfo"
printf "%s\n" "Path=$Path" >> "$Info/${n1##*/}$n.trashinfo"
printf "%s\n" "DeletionDate=$DelDate" >> "$Info/${n1##*/}$n.trashinfo"
if [ "$exitnum" -eq 0 ]
then printf "%s\n" " Moved $n1 to Trash..."
else printf "%s\n" " Cannot Move file. $n1 Copied to Trash..."
fi
shift
done
}
while [ "$#" -gt "0" ]
do if [ -e "$1" ]
then export DelDate=`date +%FT%T`
if [ ! "${1%%/*}" ] || [ "${1:0:3}" == "../" ]
then cd "${1%/*}/"
export Path="$PWD"/"${1/*\/}"
else export Path="$cwd"/"$1"
fi
setdir "$Path"
if [ "$Interact" = "0" ]
then mvtrash "$1"
else printf "%s" " Trash Item '$1'? [Y/N]: "
read -n 1 input
echo
case "$input" in
y|Y)
mvtrash "$1"
;;
*)
printf "%s\n" " Skipping $1..."
;;
esac
fi
else printf "%s\n" " Cannot Move $1. File(s) Don't Exist..."
fi
shift
done
}
restore() # Main restore section.
{
rmain()
{
checkpath()
{
mvorigin() # Retrieves the origin of the file and restores it.
{
export Origin=`grep Path "$Info/$1.trashinfo"` && export Origin="${Origin/Path=/}"
mkdir -p ${Origin%/*}
mv "$Files/$1" "$Origin"
rm "$Info/$1.trashinfo"
}
while [ "$#" -gt "0" ]
do if [ ! $OutDir ]
then if [ -f "$Info/$1.trashinfo" ]
then mvorigin "$1"
printf "%s\n" " Restoring $1..."
else printf "%s\n" " No Information file for $1. Cannot Restore..."
fi
else mv "$Files/$1" "$OutDir"
printf "%s\n" " Restoring $1 to current path..."
fi
shift
done
}
while [ "$#" -gt "0" ]
do for i in `find "$Files" -maxdepth 1 -iname "$1" -exec echo {} \; | sed 's/ /?/g'`
do if [ "$i" != "$Files" ]
then export files=1
checkpath "$(basename "$i")"
fi
done
[ "$files" ] || printf "%s\n" " $1 is not in Trash..."
export files=""
shift
done
}
if [ "$1" == "-o" ]
then export OutDir="$cwd"
shift
fi
cd "$Files"
rmain "$@"
}
list()
{
lmain()
{
export count=0
for h in .*
do if [ "$h" != "." ] && [ "$h" != ".." ] && [ "$h" != "*" ]
then printf "\033[37m %-20s\n" "$h"
printf "\033[0m"
let "count++"
fi
done
for i in *
do if [ "$i" != "*" ]
then if [ -h "$i" ]
then printf "\033[1;36m %-20s\n" "$i"
elif [ -x "$i" ]
then if [ ! -d "$i" ]
then printf "\033[1;32m %-20s\n" "$i"
else printf "\033[1;34m %-20s\n" "$i"
fi
else colour "$i" "$i"
printf "\n"
fi
printf "\033[0m"
let "count++"
fi
done
if [ $count -eq "0" ]
then printf "%s\n" " *"
fi
}
lvmain()
{
getinfo()
{
split()
{
export input="$@"
export Origin=${input#[*=} && export Origin=${Origin% *}
export DelDate=${input##[*=} && export DelDate="${DelDate:0:10}"
export DelTime=${input: -8}
}
split $(cat "$Info/$1.trashinfo")
if [ "${Origin: -1}" == "/" ]
then export Origin="${Origin%/}"
fi
export Origin=${Origin/$1/} && export Origin=${Origin%${1/%.*/}}
export Origin="${Origin##${1##${1/.*/}}}"
if [ "${#Origin}" -gt "35" ]
then export Origin="${Origin:0:33}*"
fi
printf " %-35s %s\n" "$Origin" "$DelDate | $DelTime"
}
for i in `find -maxdepth 1 -exec echo {} \; | sed 's/ /?/g;s/.\///g' | sort`
do if [ "$i" != "." ]
then if [ "${#i}" -gt "20" ]
then export n=17
export j="${i:0:$n}*${i##*.}"
while [ $n -gt "0" ]
do let "n--"
if [ "${#j}" -gt "20" ]
then export j="${i:0:$n}*${i##*.}"
fi
done
if [ "${#j}" -gt "20" ]
then export j="${i:0:19}*"
fi
else export j="$i"
fi
if [ -h "$i" ]
then printf "\033[1;36m %-20s" "$j"
elif [ -x "$i" ]
then if [ ! -d "$i" ]
then printf "\033[1;32m %-20s" "$j"
else printf "\033[1;34m %-20s" "$j"
fi
else colour "$i" "$j"
fi
printf "\033[0m"
if [ -f "$Info/$i.trashinfo" ]
then getinfo "$i"
else printf "%-43s %s\n" " No Information Availiable" "N/A | N/A"
fi
let "count++"
fi
done
}
colour()
{
case "${1##*.}" in
7z|ace|arj|bz|bz2|cpio|deb|dz)
printf "\033[1;31m %-20s" "$2"
;;
gz|jar|lzh|lzma|rar|rpm|rz|svgz)
printf "\033[1;31m %-20s" "$2"
;;
tar|taz|tbz2|tgz|tz|z|Z|zip|zoo)
printf "\033[1;31m %-20s" "$2"
;;
bmp|gif|jpeg|jpg|mng|pbm|pcx|pdf)
printf "\033[1;35m %-20s" "$2"
;;
pgm|ps|png|ppm|svg|tga|tif|tiff|xbm)
printf "\033[1;35m %-20s" "$2"
;;
desktop|directory)
printf "\033[1;36m %-20s" "$2"
;;
aac|au|flac|mid|midi|mka|mp3|mpc|ogg|ra|wav)
printf "\033[1;33m %-20s" "$2"
;;
asf|avi|dl|flc|fli|gl|m2v|m4v|mkv)
printf "\033[1;33m %-20s" "$2"
;;
mov|mp4|mp4v|mpeg|mpg|nuv|ogm|qt)
printf "\033[1;33m %-20s" "$2"
;;
rm|rmvb|vob|wmv|xcf|xwd|yuv)
printf "\033[1;33m %-20s" "$2"
;;
*)
printf "\033[37m %-20s" "$2"
;;
esac
}
export title=0
export count=0
for topdir in $(df $excludefs 2>/dev/null | awk 'FS="% "{print $2}')
do if [ -e "$topdir" ]
then case "$topdir" in
"/home")
if [ $uname == root ]
then export Main="/home/.Trash-$uid"
else export Main="/home/$uname/$XDG_DATA_HOME/Trash"
fi
;;
"/")
if [ $uname == root ]
then export Main="/root/$XDG_DATA_HOME/Trash"
else export Main="/tmp/.Trash-$uid"
fi
;;
*)
export Main=$(echo "$topdir/.Trash-$uid")
;;
esac
if [ -e "$Main" ]
then export Files="$Main/files"
export Info="$Main/info"
if [ "$1" == "-v" ]
then if [ $title -eq 0 ]
then printf " %-20s %-35s %s\n" "Name" "Original-Path" "Deletion-Date & Time"
export title=1
fi
cd "$Files"
lvmain
else
cd "$Files"
printf "\033[1;37m%s" "$Main"
printf "\033[0m\n"
lmain
fi
fi
fi
done
if [ "$1" == "-v" ]
then if [ $count -eq 0 ]
then printf "%-20s %-43s %s\n" " *" " Trash is Empty" "N/A | N/A"
fi
fi
}
emptytrash()
{
printf "%s\n" " Removing Files..."
rm -rf "$Files"
if [ "$?" -eq "0" ]
then rm -rf "$Info"
printf "%s\n" " Removing Config..."
mkdir -p "$Files"
mkdir -p "$Info"
printf "%s\n" " Trash is Now Empty."
else printf "%s\n" "Error, You are not the owner of one or more files..."
printf "%s" "To fix this, type in: "
printf "%s\n" "cd $Files && sudo chown $user:$user * -R"
exit 1
fi
}
empty()
{
emain()
{
remove()
{
rm -rf "$Files/$1"
rm -rf "$Info/$1.trashinfo"
}
while [ "$#" -gt "0" ]
do remove "$1"
printf "%s\n" " Removing $1..."
shift
done
}
cd "$Files"
while [ "$#" -gt "0" ]
do for i in `find "$Files" -maxdepth 1 -iname "$1" -exec echo {} \; | sed 's/ /?/g'`
do export files=1
emain "$(basename "$i")"
done
[ "$files" ] || printf "%s\n" " $1 is not in Trash..."
export files=""
shift
done
}
clean() # Main clean function.
{
mvclean() # Sub-function moves file to trash and writes data to info file.
{
while [ ! -z "$1" ]
do if [ "${1: -1}" == "/" ] # Remove tailing "/" from filename (ie: "test/")
then export n1="${1%/}"
else export n1="$1"
fi; if [ -e "$Files/${n1##/*/}" ] # Checks if file already exists in trash.
then export n=2
while [ -e "$Files/${n1##/*/}.$n" ] # Renames file numerically until it is unique.
do let "n++"
done
mv "$1" "$Files/${n1##/*/}.$n"
export n="."$n
else mv "$1" "$Files"
export n=""
fi; # This section stores recovery info about the file.
printf "%s\n" "[Trash Info]" > "$Info/${n1##/*/}$n.trashinfo"
printf "%s\n" "Path=$Path" >> "$Info/${n1##/*/}$n.trashinfo"
printf "%s\n" "DeletionDate=$DelDate" >> "$Info/${n1##/*/}$n.trashinfo"
printf "%s\n" " Moved $n1 to Trash..."
shift
done
}
while [ "$#" -gt "0" ]
do if [ -e "$1" ]
then export DelDate=`date +%FT%T` # Gets the time of moving the file.
if [ ! "${1%%/*}" ] # Checks for path of file. If doesn't exist, it is created.
then export Path="$1"
else export Path=$cwd/"$1"
fi
mvclean "$1"
fi
shift
done
}
###
### Main Logic Section ###
case "$1" in
-i)
export Interact="1"
shift
setdir "$cwd"
trash "$@"
;;
-r)
shift
setdir "$cwd" # FIX ME
restore "$@"
;;
-ra)
setdir "$cwd" # FIX ME
restore "*"
;;
-roa)
setdir "$cwd" # FIX ME
restore -o "*"
;;
-ro)
shift
setdir "$cwd" # FIX ME
restore -o "$@"
;;
-l)
list
;;
-lv)
list -v
;;
-e)
shift
setdir "$cwd" # FIX ME?
if [ "$#" == "0" ]
then emptytrash
else empty "$@"
fi
;;
-c)
cd "$cwd"
setdir "$cwd"
clean "#"* *~ .*~ *.bak *.log
;;
-ct)
cd "$cwd"
setdir "$cwd"
clean "#"* *~ .*~
;;
-cb)
cd "$cwd"
setdir "cwd"
clean *.bak
;;
-cl)
cd "$cwd"
setdir "$cwd"
clean *.log
;;
*)
trash "$@"
;;
esac
###
exit 0
For everyone else, feel free to try it out in the attachment I've added. (Readme guide on usage included). And let me know what you think of it.
Regards
Iain
Download:
trashapp-0.2-4b.tar.gz (http://ubuntuforums.org/attachment.php?attachmentid=72697&stc=1&d=1212431633)