Stern Center for Research Computing

New York University • Leonard Stern School of Business

Linux Basics


Introduction

This section reviews a few basic linux commands.


Logging In

To log in see Connecting to SCRC Computers.


Changing your password

To change your password go to SIMON.


A Very Simple Tutorial

This tutorial presents the minimum Linux command set one would need to use and run programs on the SCRC Linux computers.

The Linux machines use a command line interface called a shell. The shell is the interface where you enter Linux commands. There are many types of shells. By default, SCRC Linux machines use the BASH shell.

The first thing you see after logging in is the shell’s command prompt. For example, Linus Torvalds’s prompt might look something like this:

>[torvalds@rnd ~]$

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

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.,

pwd
/homedir/it/torvalds

This is the current directory path. Here, Linus’s 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.,

ls -l

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.,

mkdir constProject

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.,

cd costProject

Verify that we are in the “costProject” directory using pwd.

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 costdata.dat type,

nano costdata.dat

The nano editor

Figure 1: The nano editor

Figure 1 shows the nano editor editing the file “costdata.dat”. Along the bottom of the nano editor are the editor’s commands. The character ^ is the ctrl key. To save the file you type ctrl-o and then to exit nano type ctrl-x.

List the contents of a file

To list the contents of a file, use the more command, e.g.,

more costdata.dat

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 ctrl-c to break out or cancel your current command. ctrl-c returns you to the command prompt.


Linux Tutorial Continued


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.

To list all of the files in your current directory, type

ls

To get a long listing of the files in your current working directory including information on size, date of modification and permissions, type

ls -l

To make a copy of a file and give it a new name, type

cp costdata.dat costdata.dat.bak

To remove (delete) a file, type

rm costdata.dat

Note: There is no undo in Linux therefore once a file has been deleted there is no easy way to recover it.

To rename (move) a file, type

mv costdata.dat.bak newcostdata.dat

The more command displays the contents of a file, one screen-full 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.

more newcostdata.dat

To create a subdirectory, type

mkdir study1

To change (move down) into the new directory, type

cd study1

To change (move up) to the previous directory, type

cd ..

To remove a directory, type

rmdir study1

To identify the format of all the files in your current directory, (i.e. ascii text, English text, html, directory), type

file *

To remove ALL files in the current directory whose name begins with “costdata”, type

rm costdata*

The asterisk (*) is a wildcard character that matches one or more characters.


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.,

ls > list

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:

date >> list

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.

ls | more


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:

man man

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:

man -k directory

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.

-rw------- 1 torvalds devel Aug 20 10:15 logon
-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.

-rw-r--r--

The file is read/write/execute for owner only.

-rwx------

The directory is read/write/search for owner, read/search for group, searchable only for others.

drwxr-x--x

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:

-rw-r--r--

to read/write/execute for all users (owner, group, and other) type the command:

chmod a+rwx my.dat

The resultant permissions would be:

-rwxrwxrwx

Now, change the permissions of “my.dat” so that only the owner can read, modify or delete it, i.e.:

chmod u-x my.dat
chmod g-rwx my.dat
chmod o-rwx my.dat

The resulting permissions are:

-rw-------

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