Linux Command-Line Fundamentals
From ALICE Documentation
This tutorial teaches you about the Linux command line and shows you some useful commands. It also shows you how to get help in Linux by using the man and apropos commands.
For more training and practice using the command line, you can find many great tutorials. Here are a few:
Unix is an operating system that comes with several application programs. Other examples of operating systems are Microsoft Windows, Apple OS and Google's Android. An operating system is the program running on a computer (or a smartphone) that allows the user to interact with the machine -- to manage files and folders, perform queries and launch applications. In graphical operating systems, like Windows, you interact with the machine mainly with the mouse. You click on icons or make selections from the menus. The Unix that runs on ALICE clusters gives you a command-line interface. That is, the way you tell the operating system what you want to do is by typing a command at the prompt and hitting return. To create a new folder you type
mkdir. To copy a file from one folder to another, you type
cp. And to launch an application program, say the editor
emacs, you type the name of the application. While this may seem old-fashioned, you will find that once you master some simple concepts and commands you are able to do what you need to do efficiently and that you have enough flexibility to customize the processes that you use on ALICE clusters to suit your needs.
Common Tasks on HPC Clusters
What are some common tasks you will perform on ALICE clusters? Probably the most common scenario is that you want to run some of the software we have installed on our clusters. You may have your own input files that will be processed by an application program. The application may generate output files which you need to organize. You will probably have to create a job script so that you can execute the application in batch mode. To perform these tasks, you need to develop a few different skills. Another possibility is that you are not just a user of the software installed on our clusters but a developer of your own software -- or maybe you are making some modifications to an application program so you need to be able to build the modified version and run it. In this scenario, you need many of the same skills plus some others. This tutorial shows you the basics of working with the Unix command line. Other tutorials go into more depth to help you learn more advanced skills.
The Kernel and the Shell
You can think of Unix as consisting of two parts -- the kernel and the shell. The kernel is the guts of the Unix operating system -- the core software running on a machine that performs the infrastructure tasks like making sure multiple users can work at the same time. You don't need to know anything about the kernel for the purposes of this tutorial. The shell is the program that interprets the commands you enter at the command prompt. There are several different flavours of Unix shells -- Bourne, Korn, Cshell, TCshell and Bash. There are some differences in how you do things in the different shells, but they are not major and they shouldn't show up in this tutorial. However, in the interest of simplicity, this tutorial will assume you are using the Bash shell. This is the default shell for HPC users. Unless you do something to change that, you will be running the Bash shell when you log onto ALICE.
The Command Prompt
The first thing you need to do is log onto the ALICE cluster. If you do not know how to do this, you can find help at this ALICE wiki page. This should help you get started. Once you are logged in look for the last thing displayed in the terminal window. It should be something like
with a block cursor after it. This is the command prompt -- it's where you will see the commands you type in echoed to the screen. In this tutorial, we will abbreviate the command prompt with just the dollar sign - $. The first thing you will want to know is how to log off. You can log off of the cluster by typing "
exit" then typing the <Enter> key at the command prompt:
$ exit <Enter>
For the rest of this tutorial, when commands are shown, the <Enter> will be omitted, but you must always enter <Enter> to tell the shell to execute the command you just typed.
First Simple Commands
So let's try typing a few commands at the prompt (remember to type the <Enter> key after the command):
$ date $ cal $ finger $ who $ whoami $ finger -l
That last command is finger followed by a space then a minus sign then the lower case L. Is it obvious what these commands do? Shortly you will learn how to get information about what each command does and how you can make it behave in different ways. You should notice the difference between "
finger" and "
finger -l" -- these two commands seem to do similar things (they give information about the users who are logged in to the system) but they print the information in different formats. try the two commands again and examine the output. Note that you can use the scroll bar on your terminal window to look at the text that has scrolled off the screen.
man" command is how you find out information about what a command does. Type the following command:
It's kind of a smart-alecky answer you get back, but at least you learn that "
man" is short for "manual" and that the purpose is to print the manual page for a command. Before we start looking at manual pages, you need to know something about the way Unix displays them. It does not just print the manual page and return you to the command prompt -- it puts you into a mode where you are interactively viewing the manual page. At the bottom of the page, you should see a colon (:) instead of the usual command prompt (-bash-3.2$). You can move around in the man page by typing things at the colon. To exit the man page, you need to type a "q" followed by <Enter>. So try that first. Type
$ man finger
then at the colon of the man page type
You do not have to type <Enter> after the "q" (this is different from the shell prompt.) You should be back at the shell prompt now. Now let's go through the man page a bit. Once again, type
$ man finger
Now instead of just quitting, let's look at the contents of the man page. The entire man page is probably not displayed in your terminal. To scroll up or down, use the arrow keys or the <Page Up> and <Page Down> keys of the keyboard. The <Enter> and <Space> keys also scroll. Remember that "q" will quit out of the man page and get you back to the shell prompt.
The first thing you see is a section with the heading "NAME" which displays the name of the command and a short summary of what it does. Then there is a section called "SYNOPSIS" which shows the syntax of the command. In this case, you should see
SYNOPSIS finger [-lmsp] [user ...] [user@host ...]
Remember how "
finger" and "
finger -l" gave different output? The [-lmsp] tells you that you can use one of those four letters as a command option -- i.e., a way of modifying the way the command works. In the "DESCRIPTION" section of the man page, you will see a longer description of the command and an explanation of the options. Anything showed in the command synopsis which is contained within square brackets ([ ]) is optional. That's why it is ok to type "finger" with no options and no user. What about "user" -- what is that? To see what that means, quit out of the man page and type the following at the command prompt:
Let's say your username is alice0000. Then the result of the "
whoami" command is alice0000. Now enter the following command (but replace alice0000 with your username):
$ finger alice0000
You should get information about yourself and no other users. You can also enter any of the usernames that are output when you enter the "
finger" command by itself. The user names are in the leftmost column of the output. Now try
$ finger -l alice0000 $ finger -lp alice0000 $ finger -s alice0000 alice0001
For the last command, use your username and the username of some other username that shows up in the output of the "
finger" command with no arguments.
Note that a unix command consists of three parts:
You don't necessarily have to enter an argument (as you saw with the "finger" command) but sometimes a command makes no sense without an argument so you must enter one -- you saw this with the "man" command. Try typing
$ man man
and looking briefly at the output. One thing to notice is the synopsis -- there are a lot of possible options for the "man" command, but the last thing shown in the command synopsis is "name ..." -- notice that "name" is not contained in square brackets. This is because it is not optional -- you must enter at least one name. What happens if you enter two names?
$ man man finger
The first thing that happens is you get the man page for the "
man" command. What happens when you quit out of the man page? You should now get the man page for the "
finger" command. If you quit out of this one you will be back at the shell prompt.
You can "pipe" the output of one command to another. First, let's learn about the "
$ man more
Read the "DESCRIPTION" section -- it says that more is used to page through text that doesn't fit on one screen. It also recommends that the "
less" command is more powerful. Ok, so let's learn about the "
$ man less
You see from the description that "
less" also allows you to examine text one screenful at a time. Does this sound familiar? The "man" command actually uses the "less" command to display its output. But you can use the "
less" command yourself. If you have a long text file named "foo.txt" you could type
$ less foo.txt
and you would be able to examine the contents of the file one screen at a time. But you can also use "less" to help you look at the output of a command that prints more than one screenful of output. Try this:
$ finger | less
finger" followed by a space followed by the vertical bar (shifted backslash on most keyboards) followed by a space followed by "
less" followed by <Enter>. You should now be looking at the output of the "
finger" command in an interactive fashion, just as you were looking at man pages. Remember, to scroll use the arrow keys, the <Page Up> and <Page Down> keys, the <Enter> key or the space bar; and to quit, type "
Now try the following (but remember to replace "alice0000" with your actual username):
$ finger | grep alice0000
grep" command is Unix's command for searching. Here you are telling Unix to search the output of the "
finger" command for the text "alice0000" (or whatever your username is.)
If you try to pipe the output of one command to a second command and the second is a command which works with no arguments, you won't get what you expect. Try
$ whoami | finger
You see that it does not give the same output as
$ finger alice0000
whoami" returns alice0000.)
In this case, what you can do is the following:
$ finger `whoami`
finger" space backquote "
whoami" backquote. The backquote key is to the left of the number 1 key on a standard keyboard.
Enter the following command:
$ man apropos
As you can see, the apropos searches descriptions of commands and finds commands whose descriptions match the keyword you entered as the argument. That means it outputs a list of commands that have something to do with the keyword you entered. Try this
Ok, you need to enter an argument for the "
$ apropos calendar
Now you see that among the results are two commands -- "
cal" and "
difftime" that have something to do with the keyword "