-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclean backup files (*~)
executable file
·109 lines (99 loc) · 4.08 KB
/
clean backup files (*~)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# https://github.com/yeKcim/my_nautilus_scripts
# License: GNU General Public License V3, 29 June 2007
# Installation:
# Nautilus: copy this file in ~/.local/share/nautilus/scripts/ and chmod +x it
# Nemo: copy this file in ~/.local/share/nemo/scripts/ and chmod +x it
# Caja: copy this file in ~/.config/caja/scripts/ and chmod +x it
# Dependency: trash-cli
# clean files~
IFS="
"
################################################
# notification depends of system #
################################################
function notif {
# the script is running in a term
if [ $(env | grep '^TERM') ]; then printf "\n#### $(basename -- "$0") notification ####\n ⇒ $1\n\n"
else # in x, notifications
if hash notify-send 2>/dev/null; then notify-send "$1"
elif hash zenity 2>/dev/null; then { echo "message:$1" | zenity --notification --listen & }
elif hash kdialog 2>/dev/null; then { kdialog --title "$1" --passivepopup "This popup will disappear in 5 seconds" 5 & }
elif hash xmessage 2>/dev/null; then xmessage "$1" -timeout 5
else echo "$1" > "$(basename -- $0)_notif.txt"
fi
fi
}
################################################
# dependency check #
################################################
function depend_check {
for arg; do
hash "$arg" 2>/dev/null || { notif >&2 "Error: Could not find \"$arg\" application."; exit 1; }
done
}
################################################
# do not overwrite with output #
################################################
function do_not_overwrite {
out="$1"
while [[ -a "$out" ]]; do
when=$(date +%Y%m%d-%H:%M:%S)
[[ -f "$out" ]] && out="${out%.*}#$when.${out##*.}" || out="$out#$when"
done
echo "$out"
}
################################################
# check if input files > min #
################################################
function nb_files_check {
nb_files="$1"
min_nb_files="$2"
if (( $1 < $2 )); then
[[ $2 == 1 ]] && notif "$1 file selected, \"$(basename -- $0)\" needs at least one input file" || notif "$1 file selected, \"$(basename -- $0)\" needs at least $2 input files"
exit 1
fi
}
################################################
# error notifications #
################################################
function error_check {
nb_files="$1"
error_message="Error: $2"
nb_error="$3"
name_error_files="$4"
if [[ $nb_error != 0 ]]; then
[[ $nb_error == 1 ]] && error_message="$error_message:$name_error_files"
[[ $nb_error > 1 ]] && [[ $nb_error < $nb_files ]] && error_message="$error_message ($nb_error/$nb_files files: $name_error_files)"
[[ $nb_error > 1 ]] && [[ $nb_error = $nb_files ]] && error_message="$error_message (All selected files)"
notif "$error_message"
fi
}
################################################
# error write rights notifications #
################################################
function writeout_right_check {
out=$(readlink -f -- "$1")
outdir="${out%/*}"
[[ ! -w "$outdir" ]] && echo "1" || echo "0"
}
################################################
# script #
################################################
nb_files_check $# 1
hash "trash" 2>/dev/null && commandrm="trash" || { notif >&2 "If you don't like rm command in script, install trash (trash-cli package)."; commandrm="rm"; }
writeout_error=0; writeout_error_file=""
for arg
do
input=$(readlink -f -- "$arg")
if [[ -d "$input" ]]; then dir_input="$input"
elif [[ -f "$input" ]]; then dir_input=$(readlink -f $(dirname $input))
fi
if [[ $(writeout_right_check "$dir_input/my_function_is_bad") == "1" ]]; then
((writeout_error++))
writeout_error_file="$writeout_error_file \"$dir_input\""
continue
fi
find "$dir_input" -name "*~" -type f -exec $commandrm -f -- {} \;
done
error_check "$#" "Can't write in output directory" "$writeout_error" "$writeout_error_file"