summaryrefslogtreecommitdiff
path: root/70_Trash.zsh
blob: 0dbe042ecaf96e3a6b23a40aa0ee03ee7f5b7c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
##
## Part of configuration files for Zsh4
## AUTHOR: Hugues Hiegel <hugues@hiegel.fr>
## 
## You are encouraged to use, modify, and redistribute
## these files with or without this notice.
## 
## NO WARRANTY PROVIDED, USE AT YOUR OWN RISKS
##

TRASH=$ZDOTDIR/.trash

move_to_trash ()
{
	FOLDER=$TRASH/$PWD

	for element in $@
	do
		if [ -e $element ]
		then
			echo "Deleting $element..."
			mkdir -p $FOLDER/${element:h}
			mv -f $element $FOLDER/${element:h}/.
		fi
	done
}

list_deleted_elements ()
{
	FOLDER=$TRASH/$PWD

	if [ -d $FOLDER ]
	then
			ls -lad $(find $FOLDER -maxdepth 1 ! -wholename $FOLDER) | sed "s:$FOLDER/::"
	else
		echo "Nothing found in trash."
	fi
}

undelete_from_trash ()
{
	FOLDER=$TRASH/$PWD

	for element in $@
	do
		if [ -e $FOLDER/$element ]
		then
			echo "Getting back $element..."
			mkdir -p ${element:h}
			mv $FOLDER/$element .
			rmdir --ignore-fail-on-non-empty -p $FOLDER 
		else
			echo "Not found in trash: $element"
		fi
	done
}

alias delete='move_to_trash'
alias undelete='undelete_from_trash'
alias lsdeleted='list_deleted_elements'

alias cdtrash='cd $TRASH/$PWD'
alias sotrash='cd ${PWD/$TRASH/}'

hash -d trash=$TRASH