DevOps Zone is brought to you in partnership with:

Mark is a graph advocate and field engineer for Neo Technology, the company behind the Neo4j graph database. As a field engineer, Mark helps customers embrace graph data and Neo4j building sophisticated solutions to challenging data problems. When he's not with customers Mark is a developer on Neo4j and writes his experiences of being a graphista on a popular blog at http://markhneedham.com/blog. He tweets at @markhneedham. Mark is a DZone MVB and is not an employee of DZone and has posted 524 posts at DZone. You can read more from them at their website. View Full User Profile

Unix: tar - Extracting, Creating and Viewing Archives

08.26.2013
| 3845 views |
  • submit to reddit

I’ve been playing around with the Unix tar command a bit this week and realized that I’d memorized some of the flag combinations but didn’t actually know what each of them meant.

For example, one of the most common things that I want to do is extract a gripped Neo4j archive:

$ wget http://dist.neo4j.org/neo4j-community-1.9.2-unix.tar.gz
$ tar -xvf neo4j-community-1.9.2-unix.tar.gz

where:

  • -x means extract
  • -v means produce verbose output, i.e. print out the names of all the files as you unpack it
  • -f means unpack the file which follows this flag, i.e. neo4j-community-1.9.2-unix.tar.gz in this example

I didn’t realize that, by default, tar runs against standard input so we could actually achieve the above in one go with the following combination:

$ wget http://dist.neo4j.org/neo4j-community-1.9.2-unix.tar.gz -o - | tar -xv

The other thing I wanted to do was create a gripped archive from the contents of a folder, something which I do much less frequently and am therefore much more rusty at! The following does the trick:

$ tar -cvzpf neo4j-football.tar.gz neo4j-football/
$ ls -alh neo4j-football.tar.gz 
-rw-r--r--  1 markhneedham  staff   526M 22 Aug 23:38 neo4j-football.tar.gz

where:

  • -c means create a new archive
  • -z means gzip that archive
  • -p means preserve file permissions

Sometimes we’ll want to exclude some things from our archive which is where the –exclude flag comes in handy.

For example, I want to exclude the data, Git and Neo4j folders which sit inside neo4j-football which I can do with the following:

$ tar --exclude "data*" --exclude "neo4j-community*" --exclude ".git" -cvzpf neo4j-football.tar.gz neo4j-football/
$ ls -alh neo4j-football.tar.gz 
-rw-r--r--  1 markhneedham  staff   138M 22 Aug 23:36 neo4j-football.tar.gz

If we want to quickly check that our file has been created correctly we can run the following:

$ tar -tvf neo4j-football.tar.gz

where:

  • -t means list the contents of the archive to standard out

And that pretty much covers my main use cases for the moment!



Published at DZone with permission of Mark Needham, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)