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.
- Learn about case statements and functions to make your scripts more efficient
- Process command line options using getopts
- Manage users in Linux
- Anyone who wants to learn Linux shell scripting
- Linux system administrators, developers, or programmers
To get the most out of this course, you should have a basic understanding of the Linux command line.
In this lesson, you'll learn how to disable, delete, and archive users on a Linux system. Now I've already booted up our localuser's virtual machine here, and I've logged into it. I'm still in vagrant's home directory because I haven't changed into the shared directory of /Vagrant just yet. And we'll do that later in the lesson. I just wanna work in my home directory here for now. So far, we've been using the example of creating users as an excuse to learn all kinds of shell scripting concepts and techniques. The last few lessons I've actually been preparing you to write a script around deleting users. The main point isn't to have scripts that can create or delete users. It's to learn techniques that you can use in any shell script. Having said that, you are going to write a script in the next assignment that will have you deleting users. So you need to know how to delete users. So let's go ahead and cover that now. The command to delete a user from the system is userdel. Now, if you use our normal type command as a regular user, you're probably going to get an error. So if I do this type -a userdel, it says, "userdel not found." And let's see what happens with the which command. Which userdel. And it says, "There is no userdel" and then it gives our path there. Which tells us that there's no userdel and usr/local/bin, usr/bin, usr/local/sbin, et cetera. It is our path that it's displaying there. So just as the which command tells us the userdel command is not in our path. We talked about paths earlier in the course. At this point, there really is two options. One, the userdel command really doesn't exist. It does not exist. Or two, it in fact does exist, but it just exists outside of our current path. So I quickly wanna cover how to find a command that is not in your path, because you may run into this from time to time. Now, most Linux systems have locate installed and configured. The locate command searches an index that is created by the updatedb command. The updatedb command is typically scheduled to run once a day. This means that locate doesn't have up to the minute information, but on the other hand, it's a very fast. It finds things in that pre-created or pre-populated database or index very fast. So if we do this, if we type in locate userdel, we'll get some results here. And if we look at the top of our command, we can actually see that the command resides in /user/sbin/userdel, that's actually where the command lives. Now, just to prove that locate isn't using real time data, let's create a file called userdel, and then try to find it with locate. So I'm just going to touch userdel right here in my home directory. Again, the touch command creates a file if it doesn't exist. Or if the file exists, it touches it and updates it's timestamp. So sure enough, userdel is on our system. And if we type a locate userdel, we don't see anything about vagrant's home directory. So if we force an index update, we should be able to find that userdel file that we placed into Vagrant's home directory by using the touch command. And by the way, to run updatedb, you need root privileges because what it does is searches the entire file system and you don't have permissions to every single file as a regular user, such that Vagrant is. So we'll do this, sudo updatedb, and let that run for a second here. And once that is done, now we can do locate userdel. And now at the very top, there at the output you see /home Vagrant userdel, that file we just created. Let me take a step back. So I would first use the locate command. And actually since I'm looking for a binary or an executable file, I would limit my search to only include things in the bin directory. Let's now take the standard output of the locate command and send it as standard input to the grip command. The grip command displays matches to a pattern and discards everything else. So if we do locate userdel, pipe that into grep and our search pattern is simply going to be the characters B-I-N, and hit enter. So this really, narrows it down for us. We see two possible options and obviously, the one at the bottom there, user sbin, userdel, is what we're looking for. Sometimes the file you're looking for is not in a place where you have read permissions. The locate command honors those permissions. So if we do something like this, locate.bashrc and hit enter, we only see etc/skel/.bashrc, and then the .bashrc in our home directory. Now let's run that same command with root privileges. Now we see all the .bashrc files on the system. As the Vagrant user, we don't have permission to look inside root's home directory, for example. So if we do this ls -l/root/.bashrc, we get an error of permission denied. We can't look at anything in root's home directory by default. So if we use root permissions, we can now see the file. By the way, that quick little thing I did there the double exclamation marks or bang bang, actually represents the most recently executed command. So if you want to execute the most recent command with root privileges, use the shortcut sudo space, bang, bang. The command that will be executed actually gets displayed to the screen first. And then any output generated by that command is displayed. So here we can see that sudo bang bang expands to sudo space ls -l/root/-bashrc, and then the output of that command follows. So that's just another quick shell tip for you right there, thrown in the middle of this lesson. Anyway, my point here is that, sometimes you need to use root privileges to find a file because, you do not have permissions to view that file as your own normal regular user. Let's say locate isn't installed or configured. So what would you try next? Well, I would use my knowledge of the Linux file system hierarchy and then start looking in places where the file might live. For example, if I'm looking for a configuration file, I would start looking in /etc or /E-T-C. But since we're looking for an executable, also called a binary, I would look in bin directory. So let's do this. Here, it looks like /bin is assembling to userbin, and sbin is actually assembling to user sbin. On most modern Linux systems, this will be the case. But on some older Linux distros or in some Unix systems, you might find that they are actually different directories with different contents. So let's go ahead and look in usr/bin and see if userdel is there. Nope, no such file or directory. And by the way, when you see no such file or directory, please believe it. It's telling you that the file doesn't exist, or the file that you specified does not exist. Anyway, let's try it and usr/sbin/userdel. Here, we don't get an error. We get some valid LS output that shows us that the file does exist. Actually, I probably could have shortened up my search just a little bit here, because userdel is a system administration type of command. Now system admin commands are usually found in sbin directories. Normal commands that all users can run are found in bin directories. For example, LS is a command that everyone needs, not just a system administrator. So it's found in user bins. There is another way to find files. And that is with the find command. Unlike locate, find looks at files in real time, which makes it slower than locate, but yet up to date. There are a lot of options to the find command, and I'm not going to go through each and every one of them. But here's just a very quick crash course in how to use the find command. Now, the format of the find command is find followed by the path to search in, followed by any options, expressions, or search patterns. If you don't give find a path, it starts searching in your current directory. So let's look in the usr/bin directory. So if you use find, follow that with a path, and then hit enter. Here, it just lists all the files in that directory because we haven't narrowed down our search. And by the way, it not only lists the files in that directory, it would list any files of any sub-directories and any files within those sub-directories and so on. To say it another way, find searches recursively. Now let's tell it the name of the file we're looking for. So we can do this. Find usr/sbin, and there's an option called -name. And then we supply that with the name of the file we're looking for. We're looking for userdel. Okay admittedly, that was too easy because we already knew where to find the userdel command and, I was cheating a little bit there. But if you have no idea where the file exists, you can search the entire file system starting at the root. I don't necessarily always recommend this approach especially if you're working on a system that has a lot of files on it, but it will work. So let's do find/ with a -name of userdel. Here, we're seeing a lot of permission denied errors. There are two ways around this. One, is just to send all those error messages to dev/no, so we don't have to look at them. And let's try that here now. So we know that error messages are displayed on standard error, and standard error is represented by a file descriptor two. So we'll use file descriptor two, and redirect that to the bit bucket also called /dev/no, and hit enter. Now we don't get any of those error messages to our screen because they were redirected into dev/no, which does nothing with them it just those them away. And we're left with all the matches to the userdel name without any of the errors. So in this particular case, we find the file that we're looking for. But what if the file we were looking for is really somewhere where we need root permissions or a place where our particular user does not have permissions to view? In that particular case, you want to run the command as a root. So we can do this sudo find / and the -name of userdel. There is so much more to the find command and there's so much more you can do. But this little crash course will really get you started and pointed in the right direction. So at this point, we've proven that userdel exists, even if the type in which commands do not provide any information on those commands due to the settings of our path. However, userdel will be in root's path. So I'm going to switch to the root user. Here on the Vagrant system, by the way, the root password is also Vagrant. V-A-G-R-A-N-T, and hit enter and now I'm the root user. And if I'd hype type -a userdel, I'm going to see where the userdel command resides. All right, I'm gonna get out of the root account here and back to Vagrant.
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.