Americas

  • United States
sandra_henrystocker
Unix Dweeb

How to learn Unix/Linux

How-To
Mar 29, 20179 mins
Data CenterLinux

Every month or two, someone asks me how they should go about learning Unix. The short answer is always “use it” or maybe as much as “use it — a lot.”

But the detailed answer includes a lot of steps and a good amount of commitment to spending time working on the command line. I may have learned some of the most important parts of making my way on the Unix command line the first week that I used it back in the early 80’s but I had to spend a lot of time with it before I was really good. And I’m still learning new ways of getting work done 30+ years later. So here is my detailed answer.

Get access!

The first thing you really need to do if you want to learn how to be productive on the Unix command line is to get access to a system and start working on the command line. One way to do this is to set yourself up with a “live” distribution of Linux — one that runs from a USB drive or DVD. That way you can both use the Linux desktop and open a terminal window to start trying commands. If you don’t mind sacrificing the existing OS on your system, you can install the OS directly on your disk, but using a live version gives you a chance to sample a number of distributions before you pick the one you want to stick with for a while. Knoppix, Mint, Ubuntu, Fedora, Elementary OS and others are easy to boot and use live. Not sure where to start? Some of the distributions that look really good in 2017 are described in CIO.

You can also get yourself a virtual (AWS) system at Amazon and use PuTTY to connect. AWS gives you some choices on the kind of system you want to set up. This makes trying out commands on the command line dead easy and you can access your AWS system from anywhere and simply cancel your system if at some point you don’t need it anymore.

If the company you work for has Unix systems available, you might be able to get an account to start working on the command line on one of those, but the primary function a system is serving will probably determine whether an account for learning purposes only will be tolerated.

You can also try Bash on Ubuntu on Windows (requires Windows 10) — a configuration that allows you to open a terminal window and run Unix commands while still running Windows.

Download a cheat sheet

Get yourself a Unix command “cheat sheet” or maybe 2-3 of them. This will introduce you to the basic commands. One such sheet is available at Cheat Sheet World. Another is at Fosswire. One of the best and most complete cheat sheets is available at Unix Toolbox. Whichever you choose, put some time aside every day or two to try out the commands on your cheat sheet(s).

Start by using the ls command to see your files and ls -l to see permissions. Read a man page (command help) with the man command, check to see if anyone else is logged into the system with the who command and ask where you are with pwd. Run through all of the commands on your cheat sheet. Look at the man pages and try out some of the various options.

Get comfy with pipes and redirection

One of the most compelling features of the Unix command line is how the use of pipes and redirection makes manipulating data wonderfully easy. Try out some pipes. Here are some examples — counting running process and looking at a partial detailed file listing.

$ ps -ef | wc -l
62
$ ls -al | more
total 26188
drwx------ 32 graycat  graycat      4096 Mar 27 10:27 .
drwxr-xr-x  5 root     root         4096 Apr 21  2016 ..
drwxrwxr-x  5 graycat  graycat      4096 Mar 22 17:00 2017-03-22
drwxrwxr-x  5 graycat  graycat      4096 Mar 23 17:00 2017-03-23
drwxrwxr-x  5 graycat  graycat      4096 Mar 24 17:00 2017-03-24
drwxrwxr-x  5 graycat  graycat      4096 Mar 25 17:00 2017-03-25
drwxrwxr-x  5 graycat  graycat      4096 Mar 26 17:00 2017-03-26
drwxrwxr-w  5 graycat  graycat      4096 Mar 27 17:00 2017-03027

Get comfortable with the concept of sending data from one command to the next. Depending on the command you use on the right side of a pipe, you might be selecting data “horizontally” (i.e., selecting specific rows) as we did above or you might be selecting data “vertically” (i.e., by column). Here are some examples of selecting data vertically.

Print the first, the last, or both the first and last words from a text file:

$ cat Horton | awk '{print $1}'
"Believe
quite
person
Even
know!

"So,
them.
$ cat Horton | awk '{print $NF}'
are
a
two.
we
grow."

disturb
be."
$ cat Horton | awk '{print $1,$NF}'
"Believe are
quite a
person two.
Even we
know! grow."

"So, disturb
them. be."

Learn enough about commands like grep and awk to be comfortable with them. These commands are important to understanding the power of the command line.

Become familiar with file permissions

When you run the ls -l command, you’ll notice all the details about your files — starting with the list of permissions, then the number of links, the owner and group, the file size, the date of the last update and the file name.

If a line starts with -rwxr-x—, that means the file described is a regular file, the owner has all rights (read, write and execute), others in the same user group (if they exist) have read and execute permissions, and anyone else on the system has no rights to the file whatsoever.

Take a look at your environment variables

Environment variables play a role in how your account works. If you use the env command, you’re going to see a lot of settings. Alternately, you can just echo a variable or use the env command to view one by itself.

$ echo $USER
graycat
$ env | grep USER
USER=graycat

Play with redirection

Pipes provide a way to send the output from one command so that it becomes the input for another command. Redirection, on the other hand, takes the output from some command and generally writes it to a file. I say “generally” because you can send the output the what us old time Unix users call the “bit bucket”, /dev/null. This would be like flying your spaceship into a black hole. Poof and it’s gone (the spaceship, not the black hole). And there are times when getting rid of output is preferable to seeing it. Most of the time, however, redirected output either creates or overwrites an existing file (depending on whether the file already exists) or it appends text to the end of a file. And the difference is whether you use a > or >>. Using > overwrites a file; >> appends to it.

$ who > current-users
$ who >> user-list

Try your hand at scripting

In its simplest form, scripting is simply the process of putting commands in a file and making that file executable. With commands as simple as these, you can set yourself up with a very simple one-line script:

$ echo "echo Hello, $USER" > script1
$ cat script1
echo Hello, $USER
$ chmod 750 script1
$ ./script1
Hello, graycat

In the first line above, we create a command line and then use redirection to add it to a (new) file. The backslash character keeps $USER from being interpreted as your username before the line gets added to the file. You can see when we cat (display) the file that it’s still $USER. We then use the chmod command to make it executable. We then run the script and see that it greets us. Of course, if someone else runs the same script, it greets them. That’s the nature of environment variables.

Once you get started with scripting, you’ll quickly learn that there are a lot of options beyond simple commands. You can set up scripts to run tests and generate different output depending on the results of a test. You can build loops that run forever or through a set of values. You can prompt the person running the script to provide responses to questions posed or you can write a script so that it expects arguments to be provided and maybe won’t run if they’re not.

Take a class

If you have the option of taking a class (especially if you can get your employer to pay for it), a week away from your desk and focus just on learning Unix can be tremendously helpful. Just don’t stop practicing when you’re done with the class. You might also consider taking a class at a local community college (I used to teach at one). Several hours of classwork every week and a chance to review and practice in between classes can be an extremely effective way to get up to speed and become confident in your new command line skills.

Read a book

There are also a lot of good books for getting you up to speed. One of my favorites is The Linux Command Line published by no starch press. But don’t just read the book. Do the exercises at the ends of chapters. By the time you’ve used a command several dozen times and explored some of its options, you’re going to be ready to make good use of it.

Never stop googling

Whenever you run into a problem with a command, do some online searching. Almost any time you run into a wall, you’re going to find that a lot of people have run into it before you and a number of them will have written something about how they got over it. And don’t forget that you can email me if you have questions. I won’t know all the answers, but welcome the questions.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author