Skip to content


Bash one liner – how to compress, move, and extract a directory

Linux

The other day I needed to migrate a directory containing a lot of files from one location on my Linux server to another. There are a number of ways I could do this. Using a simple cp command could have done the trick. However, as the data I was copying was a database and I wanted to make sure that the copying was done perfectly, I looked on the Internet and found a brilliant one line bash solution for this.

I wanted to move the contents of the directory /var/lib/mysql to /opt/mysql. First I changed to the directory /var/lib/mysql:

# cd /var/lib/mysql

Then I ran the following one liner to do the magic for me.

# tar cf – * | ( cd /opt/mysql; tar xfp -)

I used the command shown above to copy my database files. What it does is it compresses the entire contents of the current directory, changes to the location of the destination directory (/opt/mysql in this case), and then extracts the archive.

Posted in Linux.

Get Simple Help tutorials just like this one in your email inbox every day - for free! Just enter your email address below:

 

You can always opt out of this email subscription at any time.

2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Continuing the Discussion

  1. More secure CLI file copy « Linux & Stuff linked to this post on May 28, 2009

    [...] in Uncategorized Interesting post from Simplehelp where the following command is recommended to copy a large database from one location to [...]

  2. How to setup slow query logging in MySQL | TuxWire : The Linux Blog linked to this post on September 22, 2009

    [...] Bash one liner – how to compress, move, and extract a directory [...]



Some HTML is OK

or, reply to this post via trackback.