How to backup and compress a folder in one step, using tar and gzip.

If disk space is limited on your Linux or Unix server and you wish to backup a folder or file that is larger than

the available free space. Then creating a compressed archive in one step may be the only option, not to mention

that it is also faster.

tar cf – folder_name | gzip -c > ./andrewlin/folder_name.tgz

This will backup the folder isp to standard output which is represented by the – (hyphen).
The ‘|’ (pipe) will redirect the result to the commands following it.
gzip compresses the contents in standard input, represented with – (hyphen).
The result of gzip is redirected to a file folder_name.tgz, located in the folder andrewlin.

You can also capture the contents of the standard output to your workstation across the network. Meaning that your

can ssh to the remote server, create the archive, compress it, then save the file onto the computer you ssh from.

This method helps if you want to store the backup on a different server. Below is the command to do that.

ssh user@remotemachine “cd /path;tar -cf – target | gzip -c” > /path/target.tgz

About Andrew Lin

Hi, I have always wanted to creat a blog site but never had the time. I have been working in Information Technology for over 15 years. I specialize mainly in networks and server technologies and dabble a little with the programming aspects. Andrew Lin

View all posts by Andrew Lin →

Leave a Reply

Your email address will not be published. Required fields are marked *