-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathremove_old
More file actions
executable file
·243 lines (213 loc) · 6.22 KB
/
remove_old
File metadata and controls
executable file
·243 lines (213 loc) · 6.22 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash
# removes all but the latest N files
shopt -s extglob
# default variable values
to_keep=10
to_prompt=0
verbose=0
dotfiles=0
user_max=0
has_max=1
max=1
follow=0
force=0
confirm=1
usage() {
cat <<'EOF'
remove_old [OPTION]... [DIR]...
Removes all but the latest N files in DIR(s).
If no DIR is supplied, use current working directory.
If DIR is '-', read list of dirs from stdin, one per line.
Options:
-h, --help Display this prompt and exit.
-n, --num NUM Keep NUM files. default is 10.
-r, --recurse Remove files recursively.
-L, --follow Follow symlinks when recursing. By default, they will not
be followed and instead will be ignored
-m, --max NUM Only recurse NUM levels (implies -r)
-f, --force Force removal of files (rm -f)
-d, --dotfiles Remove dotfiles as well.
-i, --interactive Prompt before removal of every file.
-a, --no-confirm Do not display a confirmation prompt before removing.
By default, a prompt is printed before processing each
DIR
-v, --verbose Display more info.
Requires GNU find and sort.
EOF
}
# usage: confirm PROMPT
# yes/no prompt, returns according to the user's response
confirm() {
local -l reply
read -p "$1 [y/N] " reply
[[ $reply = y?(es) ]]
}
# usage: get_files DIR [FIND_OPTIONS]
# run the find command, using the arguments beyond "$1" as options to find
# determines whether or not to follow symlinks based on -f, --follow
get_files() {
if ((follow)); then
find -L "$@" -type f -printf '%T@ %p\0' | sort -znr
else
find "$@" -type f -printf '%T@ %p\0' | sort -znr
fi
}
# display error and exit with failure
die() {
printf '%s\n' "$@" >&2
exit 1
}
# option string, for short options.
# very much like getopts, any option followed by a ':' takes a required arg
optstring=hn:rLm:fdiav
# iterate over options, breaking -ab into -a -b and --foo=bar into --foo bar
# also turns -- into --endopts to avoid issues with things like -o-, the '-'
# should not be an end of options, but an invalid option
unset options
while (($#)); do
case $1 in
# if option is of type -ab
-[!-]?*)
# loop over each character starting with the second
for ((i=1; i<${#1}; i++)); do
c=${1:i:1}
# add current char to options
options+=("-$c")
# if option takes a required argument, and it's not the last char
# make the rest of the string its argument
if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then
options+=("${1:i+1}")
break
fi
done
;;
# if option is of type --foo=bar, split on first '='
--?*=*) options+=("${1%%=*}" "${1#*=}");;
# end of options, stop breaking them up
--)
options+=(--endopts)
shift
options+=("$@")
break
;;
# otherwise, nothing special
*) options+=("$1");;
esac
shift
done
# set new positional parameters to altered options
set -- "${options[@]}"
unset options
# parse options
while [[ $1 = -?* ]]; do
case $1 in
-h|--help) usage >&2; exit 0;;
-n|--num)
# make sure NUM is a valid int
[[ $2 && $2 != *[!0-9]* ]] || die "invalid number: $2"
to_keep=$2
shift
;;
-r|--recurse)
if ((!user_max)); then
has_max=0
fi
;;
-L|--follow) follow=1;;
-m|--max)
# make sure NUM is a valid int
[[ $2 && $2 != *[!0-9]* ]] || die "invalid number: $2"
user_max=1
has_max=1
max=$2
shift
;;
-f|--force) force=1;;
-d|--dotfiles) dotfiles=1;;
-i|--interactive) to_prompt=1;;
-a|--no-confirm) confirm=0;;
-v|--verbose) verbose=1;;
--endopts) shift; break;;
*) die "invalid option: $1"
esac
shift
done
# populate array of dirs to use
if (($#)); then
dirs=()
for dir; do
# if target is '-', read list from stdin
if [[ $dir = - ]]; then
while read -r line; do
dirs+=("$line")
done
# make stdin the terminal again for rm prompting
exec </dev/tty
# if dir exists, add it to array
elif [[ -d $dir ]]; then
dirs+=("$dir")
# otherwise err
else
printf '%s does not exist or is not a directory, skipping\n' "$dir" >&2
fi
done
# if no dirs specified, use '.'
else
dirs=(.)
fi
# set rm command
if ((to_prompt)); then
rm=(rm -i)
else
rm=(rm)
fi
if ((force)); then
rm+=(-f)
fi
# set recursion, whether or not to ignore dotfiles/dirs
findopts=()
if ((has_max)); then
findopts+=(-maxdepth "$max")
fi
if ((!dotfiles)); then
findopts+=(\( -name '.*' ! -name '.' \) -prune -o)
fi
# loop over each dir provided, remove old files
for dir in "${dirs[@]}"; do
if ((confirm)); then
confirm "Remove all files except the $to_keep latest from $dir?" ||
continue
fi
if ((verbose)); then
printf 'Processing dir: %s\n' "$dir"
fi
i=0
while { read -rd ' ' t; IFS= read -rd '' f; } <&3; do
if ((++i > to_keep)); then
if ((verbose)); then
printf 'Removing file: "%s" with mtime: "%s"\n' \
"$f" "$(date -d "@${t%.*}" +'%F %H:%M:%S')"
fi
"${rm[@]}" "$f"
fi
done 3< <(get_files "$dir" "${findopts[@]}")
done
# Copyright Daniel Mills <dm@e36freak.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.