Tutorial #1: Linux Shell


Linux File System

I. Warmup

Log into your Linux machine and start a Terminal (Application - Accessories - Terminal). The prompt should look something like rong@sjtu-rong:~$, which is composed of your Username and Hostname:

rong@sjtu-rong:~$

II. Looking Around

When you log in, the system sets your current working directory to your home directory, your personal name space where you can create your own files, directories and links. You can view the contents of your current working directory with the ls command.

$ ls
To learn more about the contents of the directory, you can use ls with the -l option or any of the other options listed in the ls manual page; you can read the manual by typing man ls.
$ ls -l
$ man ls (read the man page)

Use the pwd command to learn the absolute path of your current working directory. This will tell you where you are in the directory name space even if you move around in the directories.

$ pwd
The output of pwd reveals your home directory. The output should look something like /home/rong. For non-root user, the home directory should under /home directory.

III. Creating Directories and Files

Now create a directory named ics-tot in your home directory using the mkdir command. You can learn more about the mkdir command with man mkdir.

$ mkdir ics-tot

Use ls to verify that the new directory exists. Now change your current working directory to your new ics-tot directory using the cd command and verify that your working directory has changed using pwd.

$ cd ics-tot
$ pwd
View the contents of your new directory using ls -a -l. ls normally hides the directories "." and "..", but the -a option forces it to show them.
$ ls -a -l

Question 1: Change to the '.' entry in your new directory. What happens to your working directory? Next, change to the '..' entry. What happens to your working directory?

Question 2: Describe a scenario where you might need to use the '.' directory.

Change your current directory back to your new ics-tot directory and stat the current directory; Now create a couple files in your new directory using the touch command.

$ touch foo bar
$ ls

Now create a subdirectory baz in ics-tot.

$ mkdir baz

The echo command can output a line of text your provided, and the cat command can print a file on the standard output. Read the man page for more information.

$ man echo
$ man cat

Now append a word hello to foo file using the echo command, and show the content of foo file using the cat.

$ echo hello >> foo
$ cat foo

V. Running Programs

The Linux shell has a configuration variable named PATH that tells the shell where to look in the file system for programs we type on the command line. You can see your PATH variable with this command:
$ echo $PATH
If you want run the program in your current directory, it is a bad idea of have '.' in your PATH, because it is easy to run the wrong programs by accident. Instead, you can use '.' explicitly to run programs in your working directory like this:
$ cp /bin/ls ./myls
$ ./myls

Question 3: How long did this assignment take you to complete up to this point?



Linux Shell

I. Warmup

We'll start off with an extremely simple example that most of you are probably familiar with already:

$ cd /bin
$ ls -1 | more
Here, we are first changing into the /bin directory, which contains many of the executable commands for the system. The command ls -1 gives us a listing of all the files in the current directory with one file per line. (Note that -1 is the numeral "one", not the letter "L".) We then pipe the output from ls to the command more, which displays the results one page at a time. (Press the space bar to show the next page.) You can refer to the manual pages for ls and more to see more details and options for each command. Manual pages let you read information about various commands on UNIX systems; to use them, run
$ man command

where command is the command you are interested in. If you are unfamiliar with manual pages, you may want to try running

$ man man
for information on the man command itself. Keep in mind that the manual pages for basic commands vary from system to system (much as the commands themselves do).

Now, try this:

$ cd /bin
$ ls -1 | grep p | more
This runs the same ls -1 command, but only lists the executable files which happen to contain the letter "p" somewhere in their names.

The point here is to observe that you can chain together multiple commands using the pipe character ( | ), and the output from each command will be passed to the input of the next, in left-to-right order. This allows you to treat any command that uses standard input and output as a primitive from which you can build more complex and useful commands.

II. Building Blocks

Now, we'd like you to figure out on your own how to solve some problems by chaining different commands together.

If you aren't already familiar with these commands, you may want to briefly skim through their man pages to familiarize yourself with what they do. You will probably need to use some of the options for the different commands in order to solve these problems.

Here are the commands you may find useful:


cat
chmod
echo
grep
head
ls
ps
sort 
wc

III. Questions

For each of the outputs listed below, find one sequence of commands connected by pipes that produces the output. For each problem, turn in the command sequence that you used to generate the requested output. (Do NOT turn in the output itself.)

  1. A listing of all processes that you are currently running on the machine you are using, sorted by the command name in alphabetical order (i.e. a process running acroread should be listed before a process running bash). The output should consist only of the processes you are running, and nothing else (i.e. if you are running 6 processes, the output should only have 6 lines).

  2. The number of words in the file /usr/share/dict/words (*) which start with the letter a.
    [* Note: On some Unix/Linux systems, the dictionary has the filename /usr/dict/words]

  3. A "long" listing of the smallest 5 files in the /etc directory whose name contains the string ".conf", sorted by increasing file size.

  4. An executable file, named "echo.sh", with one sequence of commands to print hello world! on screen.




Go to Top // ICS Home Page
Questions or comments regarding ICS course? Send e-mail to the course Staffs or TAs.
Last updated: Wed Feb 8 2012
Acknowledges the MIT 6.033 course (Computer System Engineering)