Linux Basics
Contents
Introduction
This section reviews a few basic linux commands.
If you are already familiar with logging in to the SCRC computers and using Linux and would like to learn more about submitting jobs to the Stern GRID see GRID processing at Stern.
Logging In
To log in you must have first installed a secure shell program. To install a secure shell program and to log in to the Stern computing environment see Connecting to SCRC Computers.
Changing your password
All password changes are made through SIMON.
Tutorial
This tutorial presents the minimum Linux command set one would need to use and run programs on a Linux computer.
The SCRC Linux computers use a command line interface so the first thing you see after logging in is the command prompt. For example, Linus Torvalds’ prompt might look something like this:
Your home directory
Once logged in, you are deposited into your home directory. Every user has a home directory.
Show the name of the current directory
So, to start, your current directory is your home directory. To see the complete name and path of the current working directory type the command pwd (print current working directory), e.g.,
/homedir/it/torvalds
This is the current directory path. Here, Linus’ home directory name is “torvalds” which is contained within a directory called “it” which is inside the directory “homedir”. The Linux directory structure is a hierarchical tree structure.
Show the contents of a directory
To see a list of the files and directories in the current directory type ls -l (list directory contents using long format), e.g.,
total 180
drwxr-xr-x 3 torvalds nobody 4096 Apr 3 2009 angel2
drwxr-xr-x 3 torvalds nobody 4096 Apr 6 2009 angel3
drwxr-xr-x 3 torvalds nobody 4096 Apr 3 2009 angle
-rw-r–r– 1 torvalds nobody 0 Nov 18 2009 prog.sas7bdat
-rw-r–r– 1 torvalds nobody 1421 Nov 18 2009 prog.sas7bdat.log
.
.
.
ls is the command and -l is, in this example, the option for the command. The -l option formats the results into long format. Long format lists more than just the names of the files which is what ls alone would show. In this example, the output shows 3 directories – “angel2″, “angel3″, and “angle” (notice the d at the beginning of the line), and 2 files – “prog.sas7bdat” and “prog.sas7bdat.log”. Some other information we see, e.g., the size of the log file is 1421 bytes, it’s owner is torvalds, it’s last modification date is Nov 18 2009.
Create a subdirectory
Say you would like to work on a project. It is probably convenient to create a subdirectory to hold the project’s files, i.e., program, data and results. To create a directory use the mkdir (make directory) command, e.g.,
This command creates a new directory called “costProject” inside the current directory.
Verify this by typing ls -l.
Note: Linux is case sensitive, so for example “costproject” is not the same as “costProject”.
Change to a subdirectory
To change into the costProject directory, i.e., make it the current directory, use the cd (change directory) command, e.g.,
Verify that we are in the “costProject” directory using pwd.
/homedir/it/torvalds/costProject
Create or edit a file
nano is a simple text editor used to create or edit a data or program file, etc. For example, to edit a file called calcAvg.c type,
Figure 1 shows the nano editor editing the file “calcAve.c”. Along the bottom of the nano editor are the editor’s commands. The character ^ is the ctrl key, so for example, to save the file type ctrl-o and to exit nano type ctrl-x.
Delete a file
To delete a file use the rm (remove) command, e.g.,
Note: There is no undo in Linux therefore once a file has been deleted there is no easy way to recover it.
List the contents of a file
To list the contents of a file, use the more command, e.g.,
If the file contents are larger than can be shown in one page, the more command shows you the first page and halts. To see the next page press the space bar and to quit type q.
Breaking out
If you get stuck or “hung” after typing a command, etc., then use crtl- to break out or cancel your current command. ctrl-c returns you to the command prompt.
The BASH Shell
The BASH shell is a command line interface where you enter Linux commands but it is also a scripting language that may be used to produce structured programs. BASH provides a number of convenient features that you may find useful when working in Linux. By default, BASH is invoked when you login to your Linux account.
Some BASH Commands
Most Linux commands are short, typically an abbreviation or mnemonic for what the command does. For example, the command to delete a file is rm which is short for remove. Recall: Linux is case-sensitive, i.e., it distinguishes between lowercase and uppercase (recognizing commands in lowercase only).
Most commands follow a common syntax:
command -options arguments
Here are some examples of basic Linux commands and command syntax:
lists all of the files in your current directory.
gives a long listing of the files in your current working directory including information on size, date of modification and permissions.
copies file “data1″ to a new file named “data2″. If “data2″ is the name of a subdirectory then file “data1″ will be copied into subdirectory “data2″ retaining the name “data1″.
removes (deletes) the file named “dataold”.
removes all files that have names beginning with a “c” and ending in “.dat”. The asterisk (*) matches any one or more characters.
renames (moves) the file “my.dat” to “new.dat”.
displays the contents of the file “my.dat”, a screenfull at a time. Press the space bar to move forward screen by screen until the end of the file is reached or press the letter q, for quit.
makes a subdirectory named “study1″.
changes your current directory to the subdirectory “study1″. Using cd with no directory name will return you to your home directory.
removes the directory “study1″. A directory must be empty for rmdir to remove it.
identifies the formats of all files in your current directory, (i.e. ascii text, English text, FORTRAN, directory).
Redirecting Input and Output
Commands usually display their results to the screen. Also, commands normally operate on data as you type it in from the keyboard. A right angle-bracket > (called an “into”) on the command line indicates that the next word is the name of a file or device in which to place, or redirect the output of a command, e.g.,
will place the output of the ls command in a file named “list”. If a file named “list” existed before you entered this command, any previous contents will be written over.
You can append to the end of a file using a double right angle-bracket >> (called an “onto”). For example, if the next command entered was:
The output of the date command would be added to the bottom of the file list.
Pipes
The output of one command can be fed as input into to another command. The symbol for the input/output (I/O) connection is a vertical bar | called a pipe.
For example, a directory containing many files will scroll off the screen if just ls is entered. If a pipe is used to direct the output of ls to be the input of more, then the directory listing will be shown a screenfull at a time and will not scroll off the screen.
Online Help
To get on-line help in Linux, use the man command. It provides access to a comprehensive online Linux manual. Type man followed by a command or topic on which you want information:
will display the pages of the online manual that explain the man command itself. To make the online manual more helpful, an index is provided. The index is accessed with the -k option of the man command. For example:
will display a one-line synopsis of all manual pages having to do with directories.
The Linux File System
Linux uses a hierarchical file structure, which is made up of files, directories and subdirectories. A file can hold text, data, or a program. Directories contain files and sometimes subdirectories. A subdirectory is a directory that has been created within another directory.
Permissions
Since Linux is set up to let users share files, you have the option of allowing or denying access to others on the system. Permissions determine who may access your files and directories or what may be done with a file or a directory. Use ls -l to see what permissions your files and directories have.
-rwx------ 1 torvalds devel Aug 19 15:23 a.out
-r-xr-xr-x 1 torvalds devel Aug 28 09:48 ls-list
drwx------ 1 torvalds resch Aug 27 15:45 sas-one/
drw------- 1 torvalds resch Aug 27 15:45 tex/
The characters “d”, “r”, “w”, “x”, and “-” at the far left indicate the permission of each file and directory. There are 10 positions. Position 1 is the directory indicator. Positions 2,3,4 apply to the owner (creator) in this case torvalds. Positions 5,6,7 apply to the group. Here, there are two different groups shown, “devel” and “resch”. Positions 8,9,10 apply to all users.
The meaning of the letters are:
d (directory)
If the first letter is a “d”, the file is a directory. If the first character is a hyphen (“-”), then it is a regular file.
r (readable)
A file must be readable to be looked at or copied. A directory must be readable for you to list its contents.
w (writable)
A file must be writable in order for you to modify it, remove it, or rename it. A directory must be writable in order for you to add or delete files in it.
x (executable)
A file with executable permissions is one you can run, such as a program or a shell script. A directory must be executable in order for you to move into it (using the cd command), list its contents, or create or delete files there.
The hyphen (-) appears when the permission is switched off. For instance, if a hyphen (“-”) appears in place of an “r”, then the file or directory is not readable.
Some Examples
The file is read/write for owner, read only for others.
The file is read/write/execute for owner only.
The directory is read/write/search for owner, read/search for group, searchable only for others.
Changing File and Directory Permissions
The file and directory permission levels in Linux can be changed to allow or deny access to other users. The chmod command is used to set the protection level. You can add or remove protection levels by using either + (add) or - (remove) with chmod and a letter signifying a class of users:
u for the file owner
g for a system defined group
o for users in neither u or g
a for all users
and the desired access settings:
r for read access
w for write access
x for execute access
For example to change the file “my.dat” with the permissions:
to read/write/execute for all users (owner, group, and other) type the command:
The resultant permissions would be:
Now, change the permissions of “my.dat” so that only the owner can read, modify or delete it, i.e.:
chmod g-rwx my.dat
chmod o-rwx my.dat
The resulting permissions are:
History
The most recent commands typed at the command line interface are saved and can be viewed or retrieved for re-use. Access the history by pressing the up arrow. To search for a command in the history type ctrl-r, type the search text, then press enter to re-execute or ctrl-c to abort
Basic Cursor Movement, Cut, Paste and Undo
| ctrl-a | move cursor to beginning of line |
| ctrl-e | move cursor to end of line |
| ctrl-k | cut everything after the cursor |
| ctrl-y | paste the last thing cut |
| ctrl-_ | undo |
Auto Completion
Press the tab key after enough of the word you are trying to complete has been typed in. If when hitting tab the word is not completed there are probably multiple possibilities for the completion. Press tab again and it will list the possibilities.
Command Summary
| cat | display contents of a file |
| cd dir | moves you to directory called dir |
| pwd | diplay the current working directory |
| chmod permissions | sets permissions on a file |
| clear | clear screen |
| cp | copies a file |
| diff file1 file2 | compare two files |
| exit | exit Linux shell |
| file | lists file type of given file |
| find –name filename | search for file called filename |
| grep | search for files containing search-string |
| cat file | grep searchstring | search file for the text searchstring |
| head | displays top lines of a file |
| history | displays recently entered commands |
| ls | lists the contents of a directory |
| ls -l | like ls, but in long format |
| man cmd | displays manual pages about cmd |
| mkdir dir | creates a directory |
| more | displays the contents of a file |
| mv | moves or renames a file |
| nano filename | launches a baskic text editor to edit filename |
| pwd | tells you which directory you’re in |
| rm filename | removes a file |
| rmdir dirname | removes a directory |
| sort | sort content of files |
| tail | displays the last lines of a file |
