Recently, I had to migrate several WordPress sites from one server to another, which actually might make for a good technical post. What I came upon, however, was the crucial bit of knowledge needed in zipping up directories and moving them that I had to do over and over and over again. For my own person benefit (and maybe for others), I decided to write this quick blog on how to compress files and directories using tar and how to uncompress them.
Before we begin, what the heck does tar stand for? After doing some research, because I use this command quite often now, it is a shortened form of “Tape archive” and does what those familiar win-zip does by compressing files and directories down to a manageable file object that you can quickly move around or archive.
For this tutorial, we will look at an example of the command and the flag directives used and what they mean. We will also consider not only how to “tar” something – compress a set of files in a directory but also how to uncompress a tar file. As a side note, there are a good number of derivative compression types like tar, bzip, gzip, tz, tzg and I won’t be dealing with those.
Why not just zip?
So, I when I first started down this path of “tar’ing” files, I wondered first about using what I was comfortable with: zipping. In using what I was comfortable with from my time in windows; Why not just use zip? The reason is zip does not retain all of the file permissions when you zip up a directory. If you want the user and/or group permission then you really need to use the tar command.
Tar Command Basics
# tar -cvzf wordpress_back.tgz wordpress/ wordpress/wp-admin/css/deprecated-media.min.css wordpress/wp-admin/css/customize-widgets-rtl.min.css wordpress/wp-admin/css/list-tables.css wordpress/wp-admin/css/color-picker.min.css wordpress/wp-admin/css/widgets.css wordpress/wp-admin/css/ie.min.css wordpress/wp-admin/css/customize-widgets-rtl.css wordpress/wp-admin/css/revisions-rtl.css wordpress/wp-admin/css/color-picker-rtl.css wordpress/wp-admin/css/login.css wordpress/wp-admin/css/color-picker-rtl.min.css wordpress/wp-admin/css/farbtastic.css wordpress/wp-admin/css/nav-menus.css wordpress/wp-admin/css/revisions.css ....and on and on it goes...
Let’s talk through what happened.
In the following command, what is actually happening? So let’s break it down.
#tar -cvzf wordpress_back.tgz wordpress/
The flag directives in order mean:
- c – create an archive
- v – show the progress of archive when being created
- z – filter the archive through gzip
- f – specifies the name of the archive file
Interestingly, if you get these out of order, the process will fail; however, removing the dash will still allow it pass correctly.
# tar -cfzv wordpress_back.tgz wordpress/ tar: wordpress_back.tgz: Cannot stat: No such file or directory
# tar cvzf wordpressBak.tgz wordpress/ wordpress/wp-admin/css/deprecated-media.min.css wordpress/wp-admin/css/customize-widgets-rtl.min.css wordpress/wp-admin/css/list-tables.css ....and on and on it goes...
If you need more directives, you can
man tar to see more.
.tgz or tar file is also pretty easy with the following command:
# tar -xzf wordpressBak.tgz
Ok, so what happened here; again, let’s break it down and look at the directives used.
- x – extract the archive file
- z – means pretty much as before filter the archive through gzip
- f – this also addresses the same as before in that it specifies the name of the archive file
You will also notice that if you use this command in the presence of a directory that has the same name as previously archived, you will overwrite it and all of its permissions. So, one way to deal with this is only extract the file you want:
# tar -xzf wordpressBak.tgz wordpress/wp-config.php
This give us then only the wp-config.php from the wordpress directory inside the wordpressBak.tgz. Or, you could also write it this way and get the same results:
# tar --extract --file=wordpressBak.tgz wordpress/wp-config.php
For multiple files, just encapsulate the files in quotes like so:
# tar-xzf wordpressBak.tgz "wordpress/wp-config.php" "wordpress/wp-cron.php"
You can also get at these using wildcards:
#tar-xzf wordpressBak.tgz --wildcards '*.php'
Now, I have only scratched the surface of what you can do with the
tar command. Again, if you need more information, please
man tar and it will give you plenty of details.
Alright that is it for now and don’t get stuck in the tar!