Link to course homepage

Linux commands and concepts

To begin executing programs, click on the icon on the bottom icon bar looking like a computer screen partially covered by a seashell. This starts up a terminal at which you will be able to type commands. You can have many such windows open at any one time.

 

You begin typing at the prompt i.e. after the ‘>’ below

/home/your_user_name>

 

NOTE!!!! These commands are CASE SENSITIVE!!! Thus, taking the first example given below

> ls

will list the files in the current directory but

> LS

will not work

 

Identifying your current environment in the file system

When you are working at the terminal, you are always working in the context of some particular directory on the computer’s  file system. If you type ‘pwd’ at the prompt, you will be told which directory you are currently in.

 

You will often want to know which files are in your current directory. You can do this by typing either

> ls

or

> ls -l   (this is if you want to obtain additional information about your files, such as when they were created )

 

Reading the contents of files

You will also sometimes want to know what is inside a file. You can look (but not change) what is in the file using either

> less filename

or

> tail -n30 filename

 

The programme ‘less’ allows you to scroll around inside a file using the arrow keys, the space bar and the delete button.  The programme ‘tail’ prints out the last lines in a file (by changing the number after the ‘-n’ you can alter how many lines at the end to print out

 

Changing the contents of files

If you want to change the contents of the file, try the following command:

 

> emacs filename &

 

This executes the file-editor called ‘emacs’, opening the file called filename in it to be edited, and by adding the ‘&’ at the end of the command, you allow emacs to run while allowing you to continue typing at the terminal. You will often want to use the ‘&’ at the end of a line when you run an interactive programme but at the same time want to continue typing at the same terminal. For example, when looking at treefiles you will usually type

 

> njplot treefilename &

 

You will need to know a few basic commands in emacs to be able to use it properly.

 

To save what you have in the editor at the moment with the same filename as that with which you executed emacs type

Control-X

and then

Control-S

(usually written “C-X C-S”)

 

To write the contents of the current contents of emacs to a file with a new name type

Control-X

and then

Control-W

(i.e. “C-X C-W”)

 

If you are halfway through a command and you change your mind do

C-G

 

To close the emacs window do

C-X C-C

 

NOTE!!!! It is usually a good idea, if you are going to edit an important file, to make a copy of the original file (e.g. “file1” to a file with name something like “file1_original”, and to only then edit “file1”. That way, if you make a mistake in ‘file1’ you can always  just create again a new copy of “file1” by copying “file1_original” to a file with name “file1”.

Creating a new file

There are many different ways of doing this. One of them is to run

 

> emacs new_filename &

 

This will open emacs with a blank page. You can then type into this page. Then, if you tell emacs to save the file, a new file with the name new_filename will be created.

 

Changing your position in the file system

You will often want to change which directory you are currently working in. To do this use the ‘cd’ command. To do this you need to be aware of several different rules about how the position of files are specified in a linux filesystem.

 

> cd dirname

 

will change your directory to that of directory ‘dirname’ that exists in the directory you are currently in

 

> cd dirname/another_dirname

 

will change your directory to that of directory ‘another_dirname’ that is in the directory ‘dirname’ that is in your current directory

 

> cd ..

 

will change your directory to that of your parent directory i.e. the directory that contains your current directory

 

> cd ../a_third_dirname

 

will change your directory to that of the directory “a_third_dirname” that is in your parent directory

 

> cd

 

will return you to your home directory

 

> cd ~/dir_in_your_home_directory

 

the “~” tells the machine that you want to specify a directory name with respect to your home directory. Thus, you could be in the directory /tmp/dir3, and type the above command, and then move to the directory /home/your_user_name/dir_in_your_home_directory

 

> cd /tmp/dir3

 

From anywhere in the filesystem, this will  place you in the directory /tmp/dir3 - the “/” at the beginning of the directory name indicates that the name of the directory is being specified to the lower-most directory in the filesystem, the so-called ‘root’ directory.

 

> cd /

 

takes you straight to the root directory

 

Copying a file

To create a copy of file “file1” with the name “file2” in the same directory as file1 type

 

> cp file1 file2

 

To create a copy of file1 in your parent directory type

 

> cp file1 ../file2

 

You will notice that the way that you refer to the parent directory here is exactly the same as the way in which you refer to the parent directory when trying to move there (see the section above “changing your position in the filesystem). All the rules used to specify directories in the filesystem that you read there apply here also. Thus, for another example

 

> cp file1 ~/dir_in_home_directory/file2

 

will create a copy of file1 that will be at the position

 

/home/your_user_name/dir_in_home_directory/file2

 

Copying a directory

To create a copy of directory “dir1” in the local directory called “dir2” type

 

> cp -r dir1 dir2

 

Moving a file

To move a file from name “file1” to name “file2” in your current directory type

 

> mv file1 file2

 

Creating a directory

To create a new directory in your current directory type

 

> mkdir new_dir_name

 

Removing files and directories

To remove/delete a file, type

 

> rm filename

 

To remove a directory type

 

> rm -r directory_name

 

BE VERY CAREFUL BEFORE YOU DELETE A FILE!!!!! ALWAYS THINK TWICE BEFORE YOU ISSUE THESE COMMANDS

 

Joining together the contents of several files into a new file

If you want to put the contents of three files ‘file1’, ‘file2’, and ‘file3’ into a fourth file ‘file4’ such that the order of the information in file4 will be the contents of file1 followed by file2 followed by file3 then type

 

> cat file1 file2 file3 > file4

 

 

Printing a postscript or text file
To print a file type

> lpr file_name

This will print the file on printer V111, just around the corner from the teaching lab

 

 

Summary

 

> pwd    

report which directory you are in

 

> ls

list the files in the current directory

 

> ls -l

list the files in the current directory, providing more information than with just ‘ls’

 

> less filename

look at what is inside the file whose name is ‘filename’

 

> tail -n40 filename

print the last 40 lines of the file ‘filename’

 

> emacs filename &

edit (i.e. change the contents of) the file ‘filename’ using the emacs editor, with the editor running in the background

 

> mv file1 file2

move ‘file1’ to ‘file2’ (i.e. delete ‘file1’, and move its contents to a new file called ‘file2’)

 

> cp file1 file2

create a copy of ‘file1’ called ‘file2’ in the current directory

 

> mkdir dirname

create a new directory called ‘dirname’

 

> rm filename

delete a file called ‘filename’

 

> rm -r dirname

delete a directory called ‘dirname’

 

> lpr filename

print the file called ‘filename’