Lab 1 -- Learning UNIX
Name:
Instructions: Complete all parts of this exercise, checking off each
part as you complete it. If a response is requested, type up the answer and
when you are done, print the lab and submit it.
The following exercises are excerpts from several tutorials that appear in the text,
"Just Enough UNIX." You can read the indicated chapters to learn more about each topic.
Note: Two useful time-savers in UNIX are the Tab key and the UP arrow. The Tab key will
complete a filename, given some prefix of the filename. The UP arrow allows you to retrieve previous
commands (also called history).
Shell Utilities at the command line (Chapter 3)
Log in to your account. Type in the following commands, and record the
output:
- pwd
- who
- whoami
- date
- man cal
(give a description of what you get.)
Files and Directories (Chapters 6-9)
-
To ensure that you are in your home directory, type:
cd
-
Are there any user-defined files in your home directory?
ls
-
How many hidden files and directories do you have in your home directory?
ls -a
What do the hidden directories, . and .. refer to?
-
To view permissions of files and directories, type:
ls -l
What permissions does the following code represent: drwxr--r--
You can also combine options, such as typing: ls -la
-
Create a file by redirection:
cal 2010 > year2010
-
What do you see when you type:
more year2010
-
cal 6 2010 > jun.2010; cal 7 2010 > jul.2010; cal 8 2010 > aug.2010
Do ls to check whether you have indeed created the three desired files.
-
cat jun.2010 jul.2010 aug.2010 > summer.2010
What does the file summer.2010 contain?
-
cp summer.2010 SUM.2010
ls
-
mkdir MyCalendars
-
mv *2010 MyCalendars
(This command will move all files ending in 2010 to the directory MyCalendars. You will
get more practice with wildcards later in this lab assignment.)
-
What happens from the following commands?
cd MyCalendars
pwd
ls
-
rm summer.2010
ls
What is the difference between the results from this ls command and the previous ls command?
-
mkdir Summer
ls
rmdir Summer
Chapter 10 (Pipes, Wildcard, Processes)
What do the following commands do?
-
who | sort
-
cat SUM.2010 | less
-
What do you see from the following command?
ls *.2010
-
To open a web browser, in the background, type:
firefox &
-
What do you see when you type: ps
-
Attempt to close the browser, by typing:
kill pid
where pid stands for the process id number that you should have seen when typing ps.
Chapter 11
There is one new command in Chapter 11 that is very useful: grep searches text files for a pattern
and prints every line in every file that contains the pattern. For example, if you wanted to search
the source code of all of your C++ programs for a date in February, you would type:
grep February *.cpp
-
What do you see when you type:
grep July *.2010
Chapter 30 - Shell Scripts
-
Open a text editor and type in the following. Then, save the file as myscript.exe:
echo 'This is my very first script.'
echo 'The current date is:'
date
echo 'My login is:'
whoami
-
Change the permissions on your file to allow executing the file.
chmod u+x myscript.exe
-
Run the script by typing: myscript.exe on the command line.
What is the output of the script?
One last step
The command script saves all activity in a shell window to a file. Type:
-
script log
-
Do any sequence of three commands that you have learned in this lab. Which three commands did
you choose?
-
Type:
exit
cat log
What do you see?
You will find the script command very useful for supplying the output necessary for your
programming assignments.