Skip to content

Commit 69f6d6b

Browse files
committed
Adds a new script to simplify deleting old files from a directory.
Created in response to a tmp/sessions/ directory filling up with tens of thousands of session files on a staging server.
1 parent f73ca97 commit 69f6d6b

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"env-vars-echo",
3939
"env-vars-load",
4040
"find-symlinks-to-in",
41+
"folder-delete-items-older-than-days",
4142
"git-currentbranch",
4243
"git-localchanges",
4344
"git-remotechanges",
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
#---------------------------------------------------------------------
4+
usage () {
5+
cat <<EOT
6+
7+
${0##*/}
8+
Deletes files (only) within the given folder that have a
9+
modified time older than the optional second argument.
10+
11+
Usage:
12+
bin/${0##*/} <path> [days]
13+
14+
\$1 = The root filesystem path to search. Will be
15+
provided to \`find\`.
16+
\$2 = Optional last modified age in days for files that should
17+
be kept. Files older than (today - \$2 eu days) will be
18+
deleted. Default = 30 days.
19+
20+
Environment Variables:
21+
(none)
22+
23+
EOT
24+
25+
exit ${1:-0} # Exit with code 0 unless an arg is passed to the method.
26+
}
27+
if [ "$1" = '-h' ]; then
28+
usage
29+
fi
30+
31+
# Process command line args.
32+
if [ -n "$1" ]; then
33+
SEARCH_PATH=$1
34+
else
35+
echo "!! No path provided. Aborting."
36+
usage 1
37+
fi
38+
if [ -n "$2" ]; then
39+
AGE_DAYS=$2
40+
else
41+
AGE_DAYS=30
42+
fi
43+
44+
45+
find "${SEARCH_PATH}" -type f -mtime +${AGE_DAYS} -print0 | xargs -0 rm -f

0 commit comments

Comments
 (0)