Actions

Linux Tutorial

From ALICE Documentation

Revision as of 14:38, 21 September 2020 by Schulzrf (talk | contribs) (Also removed step 4 and put it into a new page which is not linked just like step 5)


Work in progress

Description

This tutorial guides you through some of the basics of using Linux through the command line. The goal is for you to become familiar with certain Linux commands. There are other pages on the ALICE wiki that go into the details of submitting a job with a batch script.

Prerequisites

  • Familiarity with a text editor (Emacs, nano, vim)
  • Basic understanding of the UNIX command line

Learning

Goals

  • Create subdirectory's to organize information
  • Create a batch script with a text editor
  • Change the permissions of files
  • Get familiar with some common UNIX commands

Step 1 - Organize your directories

When you first log in to our clusters, you are in your home directory. For the purposes of this illustration, we will pretend you are user alice0001 and your project code is PRJ0001, but when you try out commands you must use your own username and project code.

$ pwd
/users/PRJ0001/alice0001

Note: you will see your user name and a different number after the /users.

It's a good idea to organize your work into separate directories. If you have used Windows or the Mac operating system, you may think of these as folders. Each folder may contain files and sub folders. The sub folders may contain other files and sub folders of their own. In Linux we use the term "directory" instead of "folder." Use directories to organize your work.

Type the following four lines and take note of the output after each one:

$ touch foo1
$ touch foo2
$ ls
$ ls -l
$ ls -lt
$ ls -ltr

The "touch" command just creates an empty file with the name you give it.

You probably already know that the ls command shows the contents of the current working directory; that is, the directory you see when you type pwd. But what is the point of the "-l", "-lt" or "-ltr"? You noticed the difference in the output between just the "ls" command and the "ls -l" command.

Most UNIX commands have options you can specify that change the way the command works. The options can be specified by the "-" (minus sign) followed by a single letter. "ls -ltr" is actually specifying three options to the ls command.

l: I want to see the output in long format -- one file per line with some interesting information about each file

t: sort the display of files by when they were last modified, most-recently modified first

r: reverse the order of display (combined with -t this displays the most-recently modified file last -- it should be BatchTutorial in this case.)

I like using "ls -ltr" because I find it convenient to see the most recently modified file at the end of the list.

Now try this:

$ mkdir BatchTutorial
$ ls -ltr

The "mkdir" command makes a new directory with the name you give it. This is a sub folder of the current working directory. The current working directory is where your current focus is in the hierarchy of directories. The 'pwd' command shows you are in your home directory:

$ pwd
/users/PRJ0001/alice0001

Now try this:

$ cd BatchTutorial
$ pwd

What is the output of 'pwd' now? "cd" is short for "change directory" -- think of it as moving you into a different place in the hierarchy of directories. Now do

$ cd ..
$ pwd

Where are you now?

Step 2 -- Get familiar with some more UNIX commands

Try the following:

$ echo where am I?
$ echo I am in `pwd`
$ echo my home directory is $HOME
$ echo HOME
$ echo this directory contains `ls -l`

These examples show what the echo command does and how to do some interesting things with it. The `pwd` means the result of issuing the command pwd. HOME is an example of an environment variable. These are strings that stand for other strings. HOME is defined when you log in to a UNIX system. $HOME means the string the variable HOME stands for. Notice that the result of "echo HOME" does not do the substitution. Also notice that the last example shows things don't always get formatted the way you would like.

Some more commands to try:

$ cal
$ cal > foo3
$ cat foo3
$ whoami
$ date

Using the ">" after a command puts the output of the command into a file with the name you specify. The "cat" command prints the contents of a file to the screen.

Two very important UNIX commands are the cp and mv commands. Assume you have a file called foo3 in your current directory created by the "cal > foo3" command. Suppose you want to make a copy of foo3 called foo4. You would do this with the following command:

$ cp foo3 foo4
$ ls -ltr

Now suppose you want to rename the file 'foo4' to 'foo5'. You do this with:

$ mv foo4 foo5
$ ls -ltr

'mv' is short for 'move' and it is used for renaming files. It can also be used to move a file to a different directory.

$ mkdir CalDir
$ mv foo5 CalDir
$ ls
$ ls CalDir

Notice that if you give a directory with the "ls" command is shows you what is in that directory rather than the current working directory.

Now try the following:

$ ls CalDir
$ cd CalDir
$ ls
$ cd ..
$ cp foo3 CalDir
$ ls CalDir

Notice that you can use the "cp" command to copy a file to a different directory -- the copy will have the same name as the original file. What if you forget to do the mkdir first?

$ cp foo3 FooDir

Now what happens when you do the following:

$ ls FooDir
$ cd FooDir
$ cat CalDir
$ cat FooDir
$ ls -ltr

CalDir is a directory, but FooDir is a regular file. You can tell this by the "d" that shows up in the string of letters when you do the "ls -ltr". That's what happens when you try to cp or mv a file to a directory that doesn't exist -- a file gets created with the target name. You can imagine a scenario in which you run a program and want to copy the resulting files to a directory called Output but you forget to create the directory first -- this is a fairly common mistake.

Some more information on how to tweak your bashrc: .bashrc

Step 3 -- Environment Variables

Before we move on to creating a batch script, you need to know more about environment variables. An environment variable is a word that stands for some other text. We have already seen an example of this with the variable HOME. Try this:

$ MY_ENV_VAR="something I would rather not type over and over"
$ echo MY_ENV_VAR
$ echo $MY_ENV_VAR
$ echo "MY_ENV_VAR stands for $MY_ENV_VAR"

You define an environment variable by assigning some text to it with the equals sign. That's what the first line above does. When you use '$' followed by the name of your environment variable in a command line, UNIX makes the substitution. If you forget the '$' the substitution will not be made.

There are some environment variables that come pre-defined when you log in. Try using 'echo' to see the values of the following variables: HOME, HOSTNAME, SHELL, TERM, PATH.

Now you are ready to use some of this UNIX knowledge to create and run a script.