Join Scott Simpson for an in-depth discussion in this video Working with tar archives, part of Learning Linux Command Line.
- [Voiceover] In the early days of computer systems and still to a very large extent today, data that needed to be archived for safe keeping was recorded to back-up tapes and stored in a closet or basement, or hopefully in a more secure and suitable location. You can't just copy a bunch of folders on to a tape system as you can with disks. It needs to be somewhat more organised and sequential. The way to do that is to put all of the files into a single file that can be laid down onto the tape. These single files that contain a whole bunch of files turned out to be very handy for distributing software over the internet, where you want your downloader to only have one thing to go get, rather than a whole handful of individual files.
Tar files, short for tape archive files, are still incredibly common for software distribution across Linux and Mac OS 10. Tar files, unlike zip files, don't offer compression themselves, but there are ways to incorporate compression into a tar file, which you may see as you explore different software distribution styles. There's tar dot g z, or dot t g z which is a tar file with g zip compression. Tar dot b z 2, which is a tar file with b zip compression and others. First, let's take a look at how to create a regular tar archive to combine a few files into one.
Let's say I wanted to make an archive of my exercise files. To do that, I go up a level in my file structure with c d dot dot and then I'll write tar dash c v f myfiles dot tar exercise files. The dash c option says create an archive. We'll use another option later to extract from an archive. The v tells tar to be verbose, that is to list out each file that gets added to the archive. This can be handy to create an index of the contents of the file.
The dash f option tells the tar command you want to output the archive to a file rather than tape. That's a bit archaic of a thing to assume I think but that's how it is. After the f option comes the file name of the archive and, after that, any files or folder you want included in the archive. In this case it's just the one folder. But, you could put more than one there if you wanted to. I'll press enter to create the file. And now I'll list the folder l s dash l and we can see the file there. Let's create a compressed version too.
I'll clear the screen and I'll add just one option. An a and I'll drop the v for verbose, since we don't need to see the files go by again. I'll write tar dash c a f and my files dot tar dot g z. The a option tells tar to figure out what kind of compression to use based on the file extension. In this case dot g z. And then I'll specify the folder that I want to compress. I can do the same thing with a b zip archive. I'll write tar dash c a f my files dot tar dot b z 2.
And again in the folder I want to compress. B zip can offer a little bit more compression for some files. But it takes quite a bit longer to run. Let's take a look at the files. I'll write l s dash l h. Here at the bottom, I can see my file dot tar the original uncompressed one at 160 kilobytes. Above that I can see the g zipped version at 107 kilobytes. And above that is the b zip one at 111 kilobytes. So, we've got some archives. Now let's unpack them. I'll create a folder, and move one of the archives into it.
I'll write m k dir unpack 1. And then I'll move one of the files into that folder. I'll write m v my files dot tar dot b z 2. And, I'll move inside that folder with c d space unpack 1. Now, to extract, I'll write tar dash x f and the file name. And taking a look at the folder with l s dash l h I can see that it's extracted my exercise files folder here.
I'll go back up a level. If you don't know exactly what's in an archive, it can be safer to create a new folder and extract the archive there. Just in case there are a lot of folders and files and you don't want them to all show up in your home folder or downloads folder or wherever. We've seen one way to do that but there's another. I'll create another folder with the command m k dir unpack 2. And then I can tell tar to unarchive into that folder. I'll write tar dash x f myfiles dot tar dot g z this time dash capital c and the destination folder unpack 2.
The capital c option specifies a directory to change into for unarchiving. As you explore working at the command line, be it as a software developer, a system administrator, or a hobbyist, you're bound to come across archive files as you explore new software. Tar has a huge number of options. Most of which you won't need at a beginning or even an intermediate stage. But, if you want to learn more about them, be sure to check out the man page for the tar command.
This course will establish the foundation for more advanced Linux topics. Find other Linux training courses here.
- What is the Linux command line?
- Writing Linux commands at the prompt
- Finding help for Linux commands
- Editing files and folders
- Configuring user roles and file permissions
- Using pipes to connect commands
- Peeking at files
- Searching and editing text
- Finding disk and system information
- Installing and updating software