UAH

Linux Tutorial

Linux is an operating system (like Windows), but instead of being controlled by a graphical user interface (i.e., pointing and clicking) it is primarily controlled by the command line.   A list of common linux commands can be found in your folder.    This tutorial is meant to introduce you to the most basic commands you will need to use this summer.  Read the text, then try things for yourself by entering the commands after the prompt ">".  (On your screens, a prompt is a ‘$’.)  If you do not see a terminal on your screen with a prompt, click on the picture of a black TV screen on the toolbar on the bottom of your screen.

Note: The following is a linux tutorial adapted from some of the tutorials on LowFatLinux.com.  Please see website for additional information.

I. File Systems:

One basic skill is learning how to navigate the linux file system.  The file system is a hierarchical system with the main directory called "root" and having the symbol "/".   Students will primarily use their home directories "/home/yourlastname/" this summer.

Below is a diagram of  a typical linux file system:

                        /                  <-- The root of the file system

 

                        |   

bin  dev   etc   home   sbin   root   usr   <-- Directories below the root

                        |  

                yourlastname    <-- User home directories

 

The first thing you need to learn is how to figure out which directory you are in, change between the directories, make new directories, and remove old directories. 

TASK: Learn how to navigate the Linux file system (go climb a tree!) 

To complete this task, read the instructions.  Everywhere you see a ">", enter the command at the command line.  Remember, your command lines ends with a “$” instead of a “>”.

You can move from one directory to another using the “cd” command (cd stands for change directories). For example, if you are in your home directory (/home/yourlastname) and want to switch to the bin directory (/bin), the following command does the trick:

> cd /bin

To switch back to your home directory, you could type

> cd /home/yourlastname

If you are unsure of which directory you are in, you can always find out using the pwd command

> pwd

A shortcut you will find useful is to use the double-dot (..) notation, as in

> cd ..

This will move you one level up in the directory tree.   Use

> pwd

to verify this. 

Another shortcut to get your home directory is to use a tilda ("~") before the user name.  Try:

> cd ~yourlastname

Also any time you want to return to your home directory, you can use

> cd

with no input.  Use

> pwd

to verify that you have returned to your home directory. 

Now that you are in your home directory, let's make a new directory for task updates.  Let's call this directory "tasks"

> mkdir tasks

To see if this directory was created, list the contents of the directory

> ls

You should see a list of files, “tasks” should be one of the files. Now change to the tasks directory.

> cd tasks

Verify that you are there using

> pwd

Now you are ready for the next section.

TIP: One common mistake people make in navigating Linux directories is using a slash in front of file names when it is not needed.  If you in your home directory

/home/yourlastname and you want to change to the tasks directory, but you enter 

> cd /tasks

the computer would try to change to a directory under root directory—oops.  (Linux understands the slash before "tasks" as telling it to go to the root directory first and then to the tasks directory just below the root.) The correct way to reach the tasks directory under /home/yourlastname is to type from within your home directory 

> cd /home/yourlastname

> cd tasks

Don't use a leading slash unless you're sure you want to start at the top (root) of the file tree.

II. Managing Files

Another skill students must acquire is how to make new files, edit existing files, move files to new locations, and copy files.  Most of the new files you will want to create are text files.  To create them, you will need to use an editor, something like Microsoft word or WordPad on MS systems.  Common Linux editors include vi, and emacs.  Most people new to Linux find a graphical editor easiest to use because it launches a graphical user interface similar to Wordpad.  On your system, the graphical editor is called leafpad.  If you already have experience with another editor, please feel free to use them. 

TASK: Start writing!

Make sure you are still in your /home/yourlastname/tasks directory using

> pwd

If your are not, cd to that directory.  Open a new file called "week1" using leafpad.

> leafpad week1.txt

Note the ".txt" on the end of the filename.  This is to signify this is a text file.  Also note that the Linux command line is now not usable.  Try to type something into your Linux window and you will see that you cannot.  More on how to avoid that later.

A new window should have opened. The leafpad window has "buttons" across the top similar to buttons you would see on WordPad.  Into this new window, type

"I am learning LINUX!"

Click on the "File" button and then click on "Save".  Then click on "File" and click "Quit".  This will close the window and give you a prompt in the command line window. 

List the files in the directory

> ls

to see that you have created the new file.  To see the contents of the new file printed to the screen, do

> more week1.txt

“I am learning LINUX" should have printed to the screen.

Try this again.  Make a new file called "summer.txt".  Open the file with leafpad, but this time add an ampersand ("&") after the command

> leafpad summer.txt &

The ampersand tells the computer to run leafpad in the background.  This doesn't change how leafpad runs, but it gives you back a prompt in your linux window. Type something in your summer.txt file (nothing too important), save it and exit leafpad.  Verify that whatever you typed was saved:

> more summer.txt

Now we are going to remove summer.txt (aren't you glad you didn't type anything important?)  To remove:

> rm summer.txt

To verify it was removed, list the files in the directory:

> ls

Remember, once a file is removed, it cannot be recovered so use “rm” with caution!

In the last task, we learned how to make a directory with the "mkdir" command, you can remove directories with the "rmdir" command.

First make a new directory inside your tasks directory, let's call it "oldstuff".

> mkdir oldstuff

> cd oldstuff

Before you move on, sketch out your directory tree, starting with root.   Put your home directory, your "tasks" directory and your "oldstuff" directory on the tree.

Now make a new file in oldstuff called test.txt using leafpad. 

> leafpad test.txt &

Write something in the file (nothing too important), save it and exit leafpad. Now change back to your tasks directory.  You should know several ways to do that now, the easiest is:

> cd ..

(you could also have typed "cd /home/yourlastname/tasks" or  "cd ~yourlastname/tasks").  Try to remove the "oldstuff” directory with:

> rmdir oldstuff

You should get an error saying that "oldstuff" is not empty.  All directories have to be emptied before they can be removed.  To empty it, change to that directory and remove the file "test.txt".  Then cd back to tasks, and try to remove again. 

> cd oldstuff

> rm test.txt

> cd ..

> rmdir oldstuff

It should have worked this time. 

III. Moving Files Around

Often you will create a file in one directory, then later want to relocate the file to another directory.  Alternatively, you may want to copy a file someone else has, rename it and modify for your own uses.  You will learn how to do these simple tasks below.

First lets make a data directory in your home directory and make some new files.

> cd /home/yourlastname

> mkdir data

> cd data

To see where you are, do

> pwd

Now lets copy the file we made in your task directory here.  That file was called week1.txt.  To copy it, we use the cp command. 

> cp  /home/yourlastname/tasks/week1.txt  /home/yourlastname/data/.

The first name we provided was the file we wanted to copy, the second name was the location we wanted to copy it to.  Note the "." at the end of the command.  This means "put the file in this location with the same name".  Alternatively, we could have chosen a different name for the file, for instance

> cp /home/yourlastname/tasks/week1.txt /home/yourlastname/new.txt

If you want to move the file permanently, use the “mv” command instead of the “cp command.

IV. Ownership and Permissions

You "own" the data directory that you made on /home/yourlastname.  To see who owns what directory, do

> cd /home/yourlastname

> ls -l

(the “-l” stands for “long”.) You should see something like this (note the following text is from lowfatlinux.com):

Permissions     User     Group      Size Date         Name

-rw-r-----   1 hermie   users      64183 Feb 14 22:07 cow_info

-rw-r-----   1 hermie   users     115032 Jan 06 11:14 dog_info

-rw-r--r--   1 hermie   users        248 Jan 16 09:18 pig_info

-rw-r--r--   1 hermie   users      45090 Mar 23 23:17 cat_info

-rwx--x---   1 hermie   users      45198 Jan 23 11:14 zippity

drwxr-x---   1 hermie   friends     1024 Feb 28 06:12 slugs


For each file you see listed a set of permissions; the owning user; a group name; and the size, creation date, and name of the file. We'll focus on the permission first by dissecting the file-access permissions for the cow_info file. Specifically, these permissions are shown in the string of characters preceding the file in the first column: -rw-r-----. Note that the permissions data is made up of ten characters, each of which has meaning.

To understand how to read file permissions, let's start by splitting apart those ten characters for cow_info:

Directory?       User's Access   Group Access    Others' Access

-                          r w -                  r - -                      r - -

                            

The character in the first position, a hyphen (-), indicates that this is a file and not a directory. Directories are marked with a d, as in drwxr-x--- (this precedes the directory slugs).

The next three characters (rw-) tell us whether the file's owner (hermie) can read, write, and execute the file. An r in the first position means that the file can be read; a w in the second position means that the file can be written to (updated); and an x in the third position means that the file can be executed (run). In all three cases, if a hyphen appears in place of an r, w, or x, that specific privilege is removed. For example, rw- means that the file can be read and written to, but not executed.

The next sets of three characters define read, write, and execute access for the users in a particular group (the users group, in this case), along the same lines as above. For example, the characters r-- that appear in these positions for cow_info tell us that the users group can read this file but can't write to or execute it.

The final set of three characters--all hyphens, in this case--defines access for those who are not the owner or in the listed group. This one's easy: No one outside the listed group has any kind of access to this file.

Note: Groups are a convenient way to give a set of users the same access to a bunch of files. Only a superuser can add to or remove users from groups. To find out what groups you belong to, use the groups command.

In sum, access to the cow_info file is controlled like so: The user (hermie) can read and update the file, but cannot execute it. People in the users group can only read the file, and everybody else on the system gets no access at all.

Here's another example:

-rwx--x--- 1 hermie users 45198 Jan 23 11:14 zippity

 

The characters that precede the file name zippity tell us that this file is readable, writable, and executable by hermie; only members of the users group can execute it; and others outside the users group have no access to it.

Note: You can give execute permission to any file, but it doesn't make sense to do so unless the file is actually a program.

Look at the listing for slugs:

drwxr-x--- 1 hermie friends 1024 Feb 28 06:12 slugs

 

You can see first that it's a directory (signified by the d in the first position). User hermie has read and write access, which in the case of a directory translates into the ability to list files and to create and delete files. Hermie also has execute access, which in the case of a directory means the ability to use cd to change to it. Those in the friends group can list files in the directory and use cd to make it the current directory, but others have no access whatsoever to the directory.

TASK: Setting permissions

The last thing you need to learn about is how to set and change the permissions of your files.  If you made a new file or directory, it has a standard set of permissions attached to it.  Generally the owner can read and write to the file, while group members and others can only read a file.

Verify that you are in your home directory.  If not,

> cd /home/yourlastname

Make a new file in this directory.

> leafpad test.txt

Write something to the file, then save and exit leafpad.  To find out the permissions on the file, do

> ls -l

Do you see how the permissions are set?  If not, go back and read the introduction to this section. 

Sometimes you may need to change the permissions to a file to allow other people to write to the file or to restrict other people from reading the file. To change the permission, you use the command "chmod".  The general form of the chmod command is

chmod <permission flags> <file or directory name(s)>

To tell chmod the new permissions for a file, you can use any combination of these permission flag characters:

WHO IT APPLIES TO          ACCESS CHANGE ACCESS TYPE

(pick one or more)                       (pick one)                (pick one or more)

u For the owner                           + Grant access         r For read access

g For the group                           - Deny access          w For write access

o For all others                                                              x For execute access

 

For instance, to give group members the ability to write to your file, you would do

> chmod g+w test.txt

Check and see how that changed the permissions.

> ls -l

To give everyone full privileges on the file, you can

> chmod ugo+rwx test.txt

See how that worked:

> ls -l

To remove privileges of "other", do

> chmod o-rwx test.txt

See if that worked:

> ls -l

Note: The superusers (i.e., REU administrators) can see anything that is on your computer account, no matter what your permissions.  These are university computers, please do not put any files on them that are inappropriate.

V.Miscellaneous

There are many more things you will need to know about running Linux, the tasks above have just hit on the tasks you will be using most often.  Below are a few more tips you may find useful.

A. PDF and Office Files.  Often times you will need to open or print a file that is in a pdf or office format (office formats include .doc which are generated by MS Word, .xls which are generated by MS Excel, or .ppt which are generated by MS Powerpoint.)  To open pdf files, use acroread:

> acroread

To read MS Office files, use the LibreOffice package. This package can be accessed from the “Applications” button on the upper left hand of your screen.  Scroll up to Office.  “Writer” is analogous to Word, “Calc” is analogous to Excel and “Impress” is analogous to Powerpoint.  Sometimes when viewing files created on Windows systems, however, the formatting can be off.

B. Web Browsing.  Linux offers several web browsers for you to choose from, including firefox.  To open one, simply type “firefox &” at the Linux prompt or click on the world symbols on the toolbar on the bottom of your screen.

C.  More information.  Most Linux commands have “man” (short for manual) pages written about them.  To learn more about the “cd” command, for instance, type:

> man cd

D. Logging out.  To logout of your computer, go to “Applications”, then “Logout”.  Alternatively, you can lock the screen when you leave.