forked from vab/sysadmin-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress.sh
More file actions
executable file
·63 lines (52 loc) · 1.32 KB
/
compress.sh
File metadata and controls
executable file
·63 lines (52 loc) · 1.32 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
#!/bin/bash
# Recursive File Compression Script
# Author: V. Alex Brennen <vab@mit.edu>
# License: This script is public domain
# Date: 2014-03-17
# Description: This script will take a directory as an argument.
# It will then transverse the directory, and all sub directories,
# and compress the regular files in those directories with bzip2.
# The script just ignores any files that are not either a regular
# file or a directory (symbolic links, devices, etc).
BZIP2=/bin/bzip2
NICE=/bin/nice
# Compression level
CLVL="-9"
# Use nice
BE_NICE=1
# Nice level
NLVL=19
# Test for a server list file argument
if [ -z "$1" ]; then
echo "Usage: $0 <directory>";
exit
fi
# Make sure the directory is valid and readable
if [ ! -d "$1" ]; then
echo "Error: Argument does not appear to be a directory: $1."
exit
fi
# This is the function that does the actual compression and
# directory transversal.
function compress_files
{
for i in $( ls );
do
if [ -f $i ]; then
if [ $BE_NICE -eq 1 ]; then
$NICE -$NLVL $BZIP2 $CLVL $i
else
$BZIP2 $CLVL $i
fi
elif [ -d $i ]; then
cd $i
compress_files
cd ..
fi
done
}
# To start the script, we change the current working directory
# to the directory to compress then kick off the initial call
# to the recursive compress_files function.
cd $1
compress_files