summaryrefslogtreecommitdiffstats
path: root/compress_large_files
blob: 06faf11f981d5c25e1cd008abbe76c53918dd8f9 (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
#!/bin/bash
#
# Recursively compress any files greater than a supplied MINSIZE (-s), starting
# from the supplied DIRECTORY (-d).
#

error() {
    echo "Error: $1"
    exit 1
}

usage() {
    echo "Usage: $0 <options>

Where <options> include:
   -d DIRECTORY - Specify directory to recurse
   -s MINSIZE   - Only compress files larger than MINSIZE
   -a AGE       - Only compress files modified more than AGE minutes ago (default 60)
   -n           - Don't make changes, just display
"
    exit 1
}

# Initialize variables
COMPRESS_SUFFIX=".gz"
COMPRESS_CMD="gzip -v --suffix ${COMPRESS_SUFFIX}"

# Process cmdline arguments
DRYRUN=0
MINSIZE=""
MINAGE=60
COMPRESS_DIR=""
while getopts "ns:a:d:h" options; do
  case $options in
    n ) DRYRUN=1 ;;
    s ) MINSIZE=$OPTARG ;;
    a ) MINAGE=$OPTARG ;;
    d ) COMPRESS_DIR=$OPTARG ;;
    \?|h ) usage ;;
    * ) usage ;;
  esac
done

# Validate input
test -z "${COMPRESS_DIR}" && error "Missing -d argument"
test -z "${MINSIZE}" && error "Missing -s argument"
test -z "${MINAGE}" && error "Missing -a argument"

# Find files to compress using the supplied arguments
#  - larger than MINSIZE Mib
#  - older than MINAGE minutes
#  - not already compressed
find "${COMPRESS_DIR}" -type f -not -name "*${COMPRESS_SUFFIX}" -mmin +"${MINAGE}" -size +"${MINSIZE}"M |
while read line
do
    if [ ${DRYRUN} -eq 1 ]; then
        echo "\$ ${COMPRESS_CMD} ${line}"
        echo "\$ ln -s $(basename $line).gz ${line}"
    else
        ${COMPRESS_CMD} ${line}
        # Create symlink to original file
        ln -s $(basename $line).gz ${line}
    fi
done