A week of bash scripts – Crush

Header

Hello blogweb. This week I’ll be posting some of my bash scripts that I use on a regulare basis. I’m not sure I’ll get to every day but I’ll do what I’m able. With that:

Crush

I tend to do quite a bit of compressing files for uploading and I cannot always seem to remember the options and order of those options that tar needs. So I built a bash script that does it for me. I call it Crush and it’s syntax is basic:

crush <file1> <folder>... <archive-name>

If no archive name is given, crush will use the name of the last file entered.

#!/bin/bash
# crush - archive and compress file/folder(s)

# Extract last parameter (archive filename) for variable
ARCHIVENAME="${!#}"

# Remove last paramerter (archive filename) for file/folder list to compress
length=$(($#-1))
FILES=${@:1:$length}

# Usage - display usage if no parameters are given
if [[ -z $@ ]]; then
  echo "crush <file> <folder>... <compressed-name>.tar.gz"
  exit
fi

# Tar the files, name archive after last file/folder if no name given
if [[ ! -f $ARCHIVENAME ]]; then
  tar -czvpf "$ARCHIVENAME".tar.gz $FILES; else
  tar -czvpf "$ARCHIVENAME".tar.gz "$@"
fi

About Gen2ly

<3's linux

2 thoughts on “A week of bash scripts – Crush

  1. Rick Harding says:

    Awesome, I’ve been getting into more and more bash scripting so this is very cool. Keep em coming. Always looking for ideas.

  2. Gen2ly says:

    Nice to know that you could make use of it. Danke.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s