Skip to content

Instantly share code, notes, and snippets.

@mikemyl
Last active May 19, 2016 08:04
Show Gist options
  • Save mikemyl/882b5d32ca9a8d9068e4617b25210bc4 to your computer and use it in GitHub Desktop.
Save mikemyl/882b5d32ca9a8d9068e4617b25210bc4 to your computer and use it in GitHub Desktop.
Bash script to uncompress any (compressed) file under a directory tree
#!/bin/bash
PROG_NAME=$(basename $0)
# Bash script developed to uncompress student exercises, regardless
# of the compressed file type. The bash script takes an argument, that
# corresponds to the directory that includes all student-subdirectories
# that contain the compressed exercises.
# E.g
# students/
# sdi000001/ sdi000002/ sdi00003/ .....
if [ "$#" -eq 0 ]
then
echo "usage: ${PROG_NAME} [students_dir]"
echo "example: ${PROG_NAME} students"
exit
fi
cd $1
currentdir=`pwd`
student_dirs=`ls`
for student in $student_dirs; do
cd $currentdir/$student
compressed_files=`ls`
for compressed_file in $compressed_files; do
if file $compressed_file | grep 'gzip';
then
tar -xzvf $compressed_file
elif file $compressed_file | grep 'tar';
then
tar -xvf $compressed_file
elif file $compressed_file | grep 'bzip2';
then
tar -xvjf $compressed_file
elif file $compressed_file | grep '7-zip';
then
7z x $compressed_file
elif file $compressed_file | grep 'zip';
then
unzip $compressed_file
else
case $compressed_file in
*.tar.bz2) tar -xvjf $compressed_file ;;
*.bz2) tar -xvjf $compressed_file ;;
*.tar.gz) tar -xvzf $compressed_file ;;
*.tgz) tar -xvzf $compressed_file ;;
*.gz) gunzip $compressed_file ;;
*.zip) unzip $compressed_file ;;
*.7z) 7z x $compressed_file ;;
*) echo "I cannot unpack it"dd
esac;
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment