CSE1020 Guided Tour Part 1: Linux and the Command Line

Using the command-line

Quoting the Introduction to user commands manual entry:

"Linux is a flavour of Unix, and as a first approximation all user commands under Unix work precisely the same under Linux (and FreeBSD and lots of other Unix-like systems).

Under Linux there are GUIs (graphical user interfaces), where you can point and click and drag, and hopefully get work done without first reading lots of documentation. The traditional Unix environment is a CLI (command line interface), where you type commands to tell the computer what to do. That is faster and more powerful, but requires finding out what the commands are."

The lab computers you are using run a Linux operating system called CentOS. The default desktop environment is called Xfce.

The basic funtionality of the desktop environment is similar to that provided by Microsoft Windows and Mac OS X.

 

The Terminal program provides a command-line interface called a shell where you type commands to tell the computer what to do. Start the Terminal program using the shortcut from the bottom panel (red arrow in image to right) , or by using the menu CSE Menu > Accessories > Terminal (white arrow in image to right).

 

A window similar to that shown to the right should appear.

The prompt miss01 % indicates that the name of this computer is miss01. You will see a different name corresponding to the name of your computer. You will also see a number beside the name (probably the number 1); the number is the history number of the current command line.

 

In CSE1020, it is important that you become comfortable working with files from the command line because the processes of creating, running, and testing a Java program all rely on manipulating files from the command line. Also, the command line is used for submitting your program files for grading!

You are probably already familiar with the notion of a computer file that represents some piece of information on a storage device (e.g., MP3 music files, JPEG picture files, word processor documents).

You are probably also aware that you can use folders to organize your computer files (e.g., under Windows you have Desktop, Documents, and Favorites folders). You can even put folders inside of other folders.

You might also know that every file and folder has a unique name called the absolute path or absolute pathname. The absolute path is simply the full list of folders that you need to traverse to reach a particular file or folder. For example, in Windows 7 the the absolute path C:\Users\Bob\Music refers to Bob's Music folder, and the backslashes \ are used to separate folder names.

In Linux, it is more common to use the term directory instead of folder.

Your home directory is where all of your CSE account files are stored. Of course, you can (and should) create directories inside of your home directory to organize your files. Every person with a CSE account has their own home directory.

No matter which lab computer you use, your home directory always has the same absolute pathname.

 

By default, the shell will start in your home directory.

To see the full pathname of your home directory, invoke the pwd command by typing it in the terminal and pressing the Enter key. You should get a slightly different result than that shown to the right.

pwd prints the full path of the current working directory.

Notice that the terminal you are using has been pre-configured to display the current working directory in the title bar.

 

Linux uses a single hierarchical directory structure to organize all of its files. This is different than Windows where each drive (C:, D:, etc.) has its own separate directory structure.

The top-most directory in Linux is called the root directory, and is identified by the slash character /.

Use the change directory command cd to switch to the root directory.

 

Use the command ls ("ell-ess" not "one-ess") to list the files contained in the current working directory.

The names listed in blue are all directory names. Many of these directories are common to all standard Linux distributions, and are described by the Filesystem Hierarchy Standard.

 

Notice that the root directory contains other directories. Those directories in turn contain other directories, and so on. One of the directories in the root directory is named cse.

Use the change directory command to switch to the cse directory.

Notice that you did not need to specify the full path /cse because you were already in the root directory / (you can always use the full path if you want).

 

The cse directory contains all of the files and directories belonging to people in the Department of Computer Science and Engineering.

Use the command ls to list the files contained in the current working directory.

 

Many of the Linux command-line programs allow the user to specify optional flags that control how the program should behave. The flags are often (but not always) specified using a hyphen -.

Use the long format flag -l ("hypen-ell") with ls to display extra information about the files in the current working directory.

The long format listing produces a lot of output, most of which won't make sense to you at this time.

 

Even expert users have a hard time remembering all of the options for all of the commands. All of the commonly used command-line programs have a manual page that you can access using the man program.

Access the manual for ls using the command man ls

 

You can move forward in the manual by pressing the Space key. Move backwards by pressing the b key. Quit using the q key.

ls has a large number of optional flags. Don't worry about trying to understand them all right now; just get used to navigating the man page, but keep the following questions in mind.

What flag would you use to:

  • list files in reverse order?
  • list one file per line?

Try out your answers; did they do what you expect?

 

Recall that we are currently in the directory /cse.

To move up one directory to the parent directory you use the command cd .. (which in this case will take us to the root directory).

The shell interprets .. as being the parent directory to the current working directory.

 

The ls command by itself will list the contents of the current working directory. If you want to list the contents of a different directory, you can specify the name of the directory.

ls bin will list the contents of the bin directory.

The directory /bin contains many of the essential command-line programs.

 

There are quite a few files in /bin. The files listed in green are all programs runnable by the user.

 

Use the change directory command to switch to the bin directory.

 

The ls command lists information about the files you specify. As you have already seen, if you specify no files, then ls lists the files in the current working directory.

Suppose that you want to list all of the files that begin with the letter l. You can do this by using the command ls l*

Most Linux shells will interpret the asterisk * to mean "any sequence of characters"; thus, ls l* means "list all files with a name starting with l followed by any sequence of characters".

Notice that the ls command is found in /bin.

 

You can return to your home directory by using the cd on its own. If you don't specify which directory to change to, cd assumes you want to go to your home directory.

You could also use cd ~ (where ~ is the "tilde" character usually located on the top left key of the keyboard).

 

At this point, you might be wondering if it is possible to list the contents of anyone's directory. Ask the person next to you what their CSE account name is, and try to list the contents of their home directory. Can you list the contents?

(In the example shown, XXXXX is just a placeholder; replace it with an actual CSE account number that isn't yours.)

 

To see why you cannot list the contents of another student's directory, invoke the command shown to the right (again, replace XXXXX with an actual CSE account number that isn't yours.)

 

The output should look something like:

drwx--x--x 35 burtonma ugrad 4096 Sep 1 14:01 ../cseXXXXX

You don't need to know what every piece of information in the output means, but some of the information can be very useful.


The username of the owner of the directory is:

drwx--x--x 35 cseXXXXX ugrad 4096 Sep 1 14:01 ../cseXXXXX


The groupname of the group that owns the directory is:

drwx--x--x 35 cseXXXXX ugrad 4096 Sep 1 14:01 ../cseXXXXX

All CSE undergraduate students belong to the group ugrad.


The letter d indicates that the entry is a directory.

drwx--x--x 35 cseXXXXX ugrad 4096 Sep 1 14:01 ../cseXXXXX


The letters rwx indicates that the user cseXXXXX has:

drwx--x--x 35 cseXXXXX ugrad 4096 Sep 1 14:01 ../cseXXXXX


The letters --x indicates that anyone in the group ugrad has:

drwx--x--x 35 cseXXXXX ugrad 4096 Sep 1 14:01 ../cseXXXXX

This set of permissions explains why you cannot list the contents, or read the files, of another student. You belong to the group ugrad and the group does not have read permission on the directory.


The letters --x indicates that everyone else (not the user and not in the group), such as faculty, has:

drwx--x--x 35 cseXXXXX ugrad 4096 Sep 1 14:01 ../cseXXXXX

This set of permissions means that your files are safe from your professor (but not from the system staff, who have superuser permissions, meaning that they have the ability to read or write any file on the filesystem).

 

We are now going to organize your directory structure for this course. First, list the contents of your home directory.

The Documents directory looks like a good place to put your files.

 

Change to the Documents directory by typing the command shown to the right but do not press the Enter key!

Instead, press the Tab key, and the shell should automatically complete the rest of the name. Now press the Enter key to invoke the command.

The shell can only complete names if you type in enough of the starting characters to uniquely identify the desired name. If you had typed cd Do the shell would not be able to tell if you meant Documents or Downloads.

 

Many of you will be taking more than one CSE course this year; thus, it makes sense to create a separate directory for this course.

You make a new directory using the mkdir command.

 

There will be several lab sessions in this course; thus, it makes sense to create a separate directory for each lab.

Change into your newly created cse1020 directory and make a lab01 ("zero-one") directory.

Change into your newly created lab01 directory.

 

Summary

Command Meaning
pwd Print the full (absolute) pathname of the current working directory.
cd dirname Change to the named directory.
cd .. Change to the parent directory of the current working directory.
cd Change to the user's home directory.
ls List the contents of the current working directory.
ls dirname List the contents of the named directory.
ls -l List using long format the contents of the current working directory.
ls -l dirname List using long format the contents of the named directory.
ls -ld dirname List using long format the name (but not the contents) of the named directory.
mkdir dirname Make a new named directory in the current working directory.

Continue to Part 2...