1. Home
  2. Training Library
  3. Cloud Computing Fundamentals
  4. Courses
  5. Shell Scripting: Parsing Command Line Options

Archives with Tar

Start course
Overview
Difficulty
Intermediate
Duration
2h 42m
Students
56
Ratings
5/5
starstarstarstarstar
Description

In this course, we'll cover a range of topics designed to help you enhance your Linux scripts. We'll start off by looking at case statements, which are used to make a decision based on the value of a given variable. We'll cover functions before moving and then move on to how to process command-line options using the shell built-in getopts.

In the second part of the course, we'll look at managing users including how to disable, delete, and archive users on a Linux system. We'll then do a walkthrough exercise showing you how to delete a user, which you can follow along with.

This course is part of the Linux Shell Scripting learning path. To follow along with this course, you can download all the necessary resources here.

Learning Objectives

  • Learn about case statements and functions to make your scripts more efficient
  • Process command line options using getopts
  • Manage users in Linux

Intended Audience

  • Anyone who wants to learn Linux shell scripting 
  • Linux system administrators, developers, or programmers

Prerequisites

To get the most out of this course, you should have a basic understanding of the Linux command line.

Transcript

Let's say we want to archive or store a user's home directory just in case we need it. One way to do that would be with a tar command. The tar command originally stood for tape archive, but you can also use it to archive files to any storage device, not just tapes. So we'll use type against tar, shows that it's a file on the file system which is executable here in the user bin directory. And let's look at the man page for this command. I'm just gonna move down here a little bit and look at these examples here. So these three examples are the three main uses of the tar command. Here you can see how to create an archive, how to view the contents of an archive, and how to extract the contents of an archive. So the -c option is used to create an archive. The -f option allows you to specify the file name of the archive file. The -t option is to list the contents of an archive. The -v option is for verbose, and the -x option is for extract. So let's try these commands out. And by the way, if you wanna get details on those, of course, they are listed here in all the options in the man page. First let me create a directory and some files in there, so we have something to play with and something to create an archive with. So let's say we have some cat videos. Let's do this, cat videos admiral cat bar. Let's call this one darthpaw. What else can we come up with here? Let's do Luke Claw Walker. One or two more here. Obi wan catnobi. How about padme amikitty.mp4 here. So if we look at our cat videos director here, we have some empty files that were created with the touch command. Now let's go ahead and create an archive of these cat videos. - C to create, f specify a file. We'll say cat videos.tar since that is the convention. You don't have to do this, but this is a good practice. And then we'll give it a path to archive. So we're just going to archive cat videos, that directory and all of its contents. So if I do ls here, we have cat videos.tar, so the archive was created. Now, let's look at the contents of that archive. We'll do tar -t to list the contents of the archive and we have to give it a f a file, which is cat videos.tar. So that shows us the contents within that tar file. If you wanted to see the files as they were being added to the archive, you can use the verbose option. So let's go ahead and just delete this tar file here now and do this c verbose and then do cat videos.tar and the cat videos directory. By the way, you can off or leave off or leave on the directory separator. Either one is fine, I'm just gonna leave it on for this time. So this time, instead of silently creating our tar file, the -v option allowed us to see what was being added to our archive as it was happening. Let's go ahead and restore the files from this archive somewhere. Let's go ahead and just create a restore directory, change into that directory. And then what we're going to do is do an x for extract and then we gotta give it a file. And of course, .. is a parent directory of where we were just at, and then / is a directory separator, and then cat videos.tar is the name of the file. So again, here, ../, I'm not doing anything that's necessarily related to tar. This is just a Linux convention of relative pathing. How to get to a file that's in the parent directory of your current directory. By the way, I could have used the full path. I could have used home vagrant cat videos.tar but I'm not gonna do that. Okay, so we have our command here, -x for extract, - f for the file that we're going to use as our archive file which is ../catvideos.tar, and I'm going to hit enter. So now if we look in this new directory here, we have the cat videos directory created. And if we list the files in that cat videos directory, we get all those files that were inside that archive. I'm just gonna go ahead and remove all this right here. So this time, let's use the verbose option to see exactly what we're extracting. And this is typically what I like to do. Okay, this shows that it's created the directory and then put those several files within that directory. Okay, I don't need that directory right now, so I'm just gonna delete it. And I'm going to type CD.. to move back up to the parent directory, and I'm back in my home directory. One of the common things people do with archives is actually compress them to save some disc space. Now, this can be a two-step process, like so. Let me go ahead and just remove the tar file that was there. Let's create a cat videos.tar file. And now let's gzip it. By the way, the gzip command compresses the original file and depends a .gz to the end of the file name. So if you see a file that ends in tar.gz, it's a compressed archive. Now, to decompress or uncompress a gzip file, use g unzip. Now we have our uncompressed original tar file back. Let's go ahead and delete that. You can perform the compression on the archive when you create it if you use the -z option to tar. So what we can do here is do tar-z for compress, and c, we're going to create an archive. V is gonna be verbose. F is we're going to give a file name, and then a path to what we're going to put in our archive. Here we can see, we have our cat videos.tar.gz file, and if we want to view the contents of a compressed archive, we also need the -z option. So we'll do tar-z, t to list verbose, f for file. You know the drill, we'll give it the path and then we can see the contents of that archive without actually extracting it. So let me go ahead and just delete that right now. Sometimes people like to use the file extension of .tgz to represent a compressed archive. So we can do this, tar zcvf cat videos.tgz, and then give it a path. So it's just something to keep in mind that .tar.gz and .tgz are really the same thing. Let's go ahead and restore this compressed archive. We'll go into our restore directory here and there's nothing in our folder. So let's use the -z option 'cause we're working with a compressed file, and then provide the path to that file. Actually, this time I'm going to use the full path. Okay, it displays what was extracted. And if we look, sure enough, we see the contents of that archive has been extracted on disc. I wanna point out something that's kind of important here. The tar command really like most Linux commands treats you like a knowledgeable, sane, responsible adult. So it will happily overwrite anything in its way. It assumes you know what the heck you're doing. So let's add some text to a file just to change it up a little bit. So let's just put some text in here into our cat videos file of darthpaw. And if we cat the contents of that file, we'll see it says what we piped into it. By the way, !$ takes the very last argument on the previous command line, and places it on the current command. So again, we're using what's called advent designators in bash, and you can see that !$ expanded to the last thing on our previous command line here, cat videos darthpaw, and then it displays the entire command that's gonna be executed. And then it displays the result of that command, just another quick tip for you there. Anyway, back to the original point, let's go ahead and extract this archive again. Let's execute the most recent command that starts with c which is cat. And if we cat cat videos/darthpaw, nothing is returned. And if we look at a long listing of that file, it now has a file length of zero. And so nothing is in it, which is the original file or the file that was in the archive. So again, tar there just overwrote the file. It didn't ask for a confirmation or anything like that. It didn't give you a warning. It just went ahead and overwrote the file. So be very careful when you're extracting archives and just be conscious of what you're doing. This also goes without saying, but tar relies on file permissions like any other command does. So, if you try to create and archive files that you don't have permissions to, well, that's just not gonna work. Also, if you try to extract the contents of an archive to somewhere where you don't have permissions to, that's not going to work either. So if you wanna be sure you have permissions, you sudo or become the root user first. For example, let's say you want to back up the contents of the /etc directory that contains pretty much all of your configuration files on the system. You'll need root privileges to do that, because there are some files in etc that a normal user just does not have access to. So let's get back to our home directory and do sudo tar-z. I'm gonna leave out the verbose option, cf. We'll create the etc.tgz file, and we'll archive the contents of /etc. Here, like I just said, I didn't use the verbose option mainly because I wanted you to see this particular message. Tar says, I'm removing the leading / from member names. If we look at the contents of the archive, we'll see that there is no leading slash either. So we'll do this tar ztvf etc tgz. So we can see the last file there, etc nanorc is not /, etc/nanorc, it's just etc/nanorc. This allows you to extract the contents of the archive into a different location without overwtriting all the existing files. So if it preserved the leading slash/etc/nanorc, if we tried to extract this archive, then it would overwrite that file. And that may be not what you want to do. For example, maybe you want to extract this into a different sub-directory and then compare the contents of that archive or that backup to what is currently on disc instead of just blowing right over it and overriding it right away. So more or less here, tar is going to remove the absolute pathing indicated by the leading /, and it's just going to leave you with a relative path within your archive. There is one more thing I'd like to talk about with the tar command. Now, you may have seen people leave off the leading hyphen and do things like this. Tar zcvf catvideos.tgz and then give a path. So this is an older type of syntax that is still supported to this very day. So I just want you to be aware of it. And some documentation you might actually see mention of "old options." Well, this is what they're talking about. If you use this old option syntax, all the options must be written together as a single clumped set without spaces separating them or dashes proceeding them. Also, this set of letters must be the first to appear on the command line after tar itself, and then some wide space. Old options cannot appear anywhere else, they must immediately follow tar with some wide space. The letter of an old option is exactly the same letter as the corresponding short option. For example, the old option t is the same as the short option -t. When options require an argument, those arguments have to be presented in the same order. This can get pretty confusing if you use multiple options that have an argument. So instead of worrying about how to do it correctly, honestly my advice is to simply use a hyphen before your options. That way you can do things like this, tar space-f, and then provide whatever option you want to after that command, and then all your other options, they can be -c, -v, and so on. And here's a path and we do this and sure enough we get our archive created. If we wanted to do it with the "old style syntax" we'd have to do this tar cvf catvideos.tar, and then the path. So that works. So we couldn't do something like this, for example, tar f cat videos, and then do c and v out here, because what it's gonna try to do is interpret c and v as files that are to be added to the archive and not as options. So again, it gets kinda confusing when this old syntax, so just really treat tar like any other command and start using dashes. If some old grumpy Unix guy with suspenders and a beard yells at you for doing this, well, just tell him that I told you to do it and put the blame on me.

About the Author
Avatar
Jason Cannon
Founder, Linux Training Academy
Students
3386
Courses
60
Learning Paths
8

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.

Covered Topics