Skip to content

Instantly share code, notes, and snippets.

@spudtrooper
Created April 8, 2011 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spudtrooper/910119 to your computer and use it in GitHub Desktop.
Save spudtrooper/910119 to your computer and use it in GitHub Desktop.
If you ever untar a tarball that doesn't contain a directory of files, instead a bunch of files, and you want them all in one directory, this will remove the files extracted from that tarball that were extracted.
#!/bin/sh
#
# Removes the files that were extracted locally from a tar file. This
# can be used when a tarball doesn't contain a directory of files and
# extracts a bunch of files into the pwd.
#
# Example:
#
# % undotar file.tar
# % undotar file.tar.gz
# % undotar file.jar
# % undotar file.zip
# % undotar file.bzip2
#
function remove_files_from_tar {
file=$1
shift
case $file in
*.tar.gz)
cmd="tar tvfz"
;;
*.tar)
cmd="tar tvf"
;;
*.jar)
cmd="jar tvf"
;;
*.zip)
cmd="unzip -t"
;;
*.bzip2)
cmd="bunzip2 -t"
;;
*)
echo "Don't know how to handle $f"
return
;;
esac
all=`$cmd $f | awk '{print $NF}' | xargs`
for f in $all; do
rm -rf $f
done
}
for f in "$@"; do
remove_files_from_tar $f
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment