This was one of my assignments in which I had to create a backup script that will individually compress all files (of an arbitrary number) of file-types (indicated by their .extension).
Example: backup a b c will take all files ending in .a, .b and .c and compress them using tar, keeping the original file, and naming the new compressed file the same as before except with .tar.gz at the end.
The -t flag can be used to specify the target directory.
The -d flag can be used to specify the destination directory.
Example: backup -t ~/Desktop/stuff -d goesHere gif where goesHere is a directory in your current path and all files ending with .gif come from ~/Desktop/stuff.
$ ls . .. backup cat.gif dog.gif funny_video.mov media $ ./backup -d media gif mov backup: compressed .gif files backup: compressed .mov files backup: files saved to "media" $ cd media $ ls . .. cat.gif.tar.gz dog.gif.tar.gz funny_video.mov.tar.gz $
Both flags are optional, allowing you to specify either one or both. If you specify both flags you must use the order -t <directory> -d <directory>.
Granted I really don't like the specifications of the assignment, but that is what they are. The fact we're asked to compress all files by their .extension is kind of anti-*nix anyways (everyone knows cat.png could be a plain text file).
#!/bin/bash
list_begin=1 # where the filetype arguments begins
list_end=$ # where the filetype arguments end
target_directory=$(pwd)
destination_directory=$(pwd)
if [[ $# < 1 ]]; then
echo "backup: not enough arguments"
exit
fi
# determine if any flags are used and if so update directories and $list_begin
if [[ $1 == "-t" ]]; then # -t
target_directory=$2
list_begin=3
if [[ $3 == "-d" ]]; then # -t, -td
destination_directory=$4
list_begin=5
fi
elif [[ $1 == "-d" ]]; then # -d
destination_directory=$2
list_begin=3
fi
# confirm directories are real
if [[ ! -d "${target_directory}" ]]; then # invalid -t
echo "backup: \"${target_directory}\" is not valid"
exit
elif [[ ! -d "${destination_directory}" ]]; then # invalid -d
echo "backup: \"${destination_directory}\" is not valid"
exit
fi
declare -a filetypes # array to store filetypes
for ((i=${list_begin}, j=0; i<=${list_end}; i++, j++)); do # fetch all filetype arguments and add to array
eval dir=\${$i}
filetypes[${j}]=${dir}
done
for i in "${filetypes[@]}"; do # for each filetype
f_list="${target_directory}/*.${i}" # directory to iterate through
for file in ${f_list}; do # for each file of that filetype in the target directory
if [[ -f "${file}" ]]; then
tar -czf ${destination_directory}/$(basename "$file").tar.gz -P ${file} # compress the file in the destination directory
fi
done
echo "backup: finished .${i} files"
done
echo "backup: files saved to \"${destination_directory}\""
tar. \$\endgroup\$tarisn't a compression tool. Yes, it has options to pass the result throughgziporbzip2orxzetc., but that's just a bonus of GNU tar, so if compression's all you want, it's simpler to stick to the tool-for-the-job, and you'll get simpler code that way. I'll look at adapting my answer in the knowledge thattarisn't part of the requirement. \$\endgroup\$