Due Thursday, January 20 11:59 P.M.
This lab is intended to introduce you to the most commonly used UNIX
commands and test your ability to use them. Each command is followed
by a description of its purpose and examples of its usage.
- After you log in, locate the terminal icon near the bottom of the
screen. Click this icon to start a shell. If this icon does not appear,
you will need to enter the start menu (the RedHat button) and select
"Run Program", then type "xterm".
- The UNIX shell reads commands from the keyboard and executes them.
The shell signals that it is ready for additional commands by providing
a prompt, a short string of characters usually ending with a #, $, :, or >.
- For each of the command descriptions that follows, example command
usage is provided in a fixed-width font set off from the
description by two blank lines. If you are not already familiar with
UNIX, you should practice each command (with one exception) by typing
in the examples and reading any output.
- The man command displays help on using each command.
Type space to view additional pages of documentation, and type q
when you are done reading. Read the documentation about the man
command now:
man man
- Many UNIX shell commands read from standard input and write to
standard output. By default, standard input is whatever you type at
the keyboard, and standard output is displayed on the screen.
- Most UNIX shell commands require one or more words to follow the
command name. These words are called arguments and are used to specify
the exact behavior of the command.
- The cat command is used to concatenate
the files given as arguments (or standard input if no arguments are
given). Note that concatenation means adding one file to the end of
another. Since cat displays to standard output by default,
you can use cat to display short textfiles to the screen.
Practice using cat to display and combine the files /etc/fstab
and /etc/hosts:
cat /etc/fstab
cat /etc/hosts
cat /etc/fstab /etc/hosts
You may also want to read the documentation for the cat command:
man cat
- Since cat reads from standard input by default, running
cat without any arguments will display whatever you type at the
keyboard back to the screen (this is called echoing). Practice using
cat this way and type CTRL-D (which we will abbreviate as ^D)
to signal the end of standard input when you are finished:
cat
this is my input from the keyboard...
^D
- The sort command performs alphabetic sorting of standard
input or the files given as arguments. Practice sorting the password
file and compare with the unsorted version:
sort /etc/passwd
cat /etc/passwd
- UNIX shells provide two facilities for changing the behavior of
standard input and output: redirection and pipes. The input redirection
operator < is used to change standard input to a file rather
than the keyboard. The output redirection operator > is
used to change standard output to a file, which will be overwritten if
it already exists. The output redirection operator >> is similar,
but appends files instead of overwriting them. Lastly, a pipe |
between two commands changes the standard output of the command on the
left side of the pipe to the standard input of the command on the right
side of the pipe. Practice combining two files and saving them to a
third file, sorting/storing the output of cat, and viewing a
file by redirecting standard input:
cat /etc/fstab /etc/hosts > out1
cat out1 | sort > out2
cat < out2
- The current working directory is the default location for any files
input or output by a shell command. Use pwd to display the
current working directory. When you first log in to your account and
open a terminal window, the current working directory will usually be
set to a special directory called your home directory. Try displaying
the name of your home directory now:
pwd
You can refer to your home directory with this longer name or by
using the tilde (~) symbol, which the shell understands to be
synonymous with your home directory. You may refer to another user's
home directory using the tilde followed by their username. For example,
my username is gbutler, so you can access my home directory using
~gbutler. Every directory is a subdirectory of (i.e. contained
in) another directory called its parent directory. You can refer to the
parent of the current working directory using two periods (..).
The directory / is called the root directory and contains all
other directories.
- The cd command is used to change the current working
directory to the directory specified on the command line, or to your home
directory if no argument is given. If you specify a directory beginning
with a /, you must specify the full directory name. Otherwise,
the directory you specify is assumed to be a subdirectory of the current
working directory. Practice changing your directory to /etc
three different ways and then back home again:
cd /etc
pwd
cd
pwd
cd /
pwd
cd etc
pwd
cd ~
pwd
cd /bin
pwd
cd ..
pwd
cd etc
pwd
cd
pwd
- The ls command is used to display information about the
files given as arguments, or about the current working directory if no
files are given. Use ls to display the contents of your home
directory and /etc two different ways:
cd
ls
ls /etc
cd /etc
ls ~
ls
cd
In addition to file names, ls may take one ore more arguments
beginning with a dash that modify the information provided (these are
called flags). A few of these are listed below:
| Flag | Purpose |
| -l | Long output format (extra information) |
| -R | Recursive listing (list directory and all its subdirectories) |
| -d | List directory only, not its contents |
| -a | List all files, even if they are hidden |
| -F | Add a character to the end of each filename specifying the file type |
Practice using ls to get extra information about the files you
created while practicing cat, and to get a list of all files
beneath your home directory:
ls -l out1 out2
ls -R -a ~
- The rm command is used to remove (delete) files. Make sure
you are in your home directory, then delete the files out1
and out2, and check that the files no longer exist:
cd
ls -l
rm out1 out2
ls -l
Think carefully about what you are doing when using rm.
File deletion in UNIX is irreversible.
- The mkdir and rmdir commands are used to make
and remove directories. Use these commands to create and then remove
a directory called dir1 in your home directory with two
subdirectories sub1 and sub2:
cd
mkdir dir1
cd dir1
mkdir sub1
cd
mkdir dir1/sub2
ls dir1
rmdir dir1/sub1
cd dir1
rmdir sub2
cd ..
rmdir dir1
ls dir1
Note that the last command should give you an errors message, since the
directory dir1 no longer exists.
- The touch command is used to create empty files and to set
the timestamp for files that already exist. Use touch to create
three empty files called e1, e2, and e3:
touch e1
touch e2 e3
- The mv command is used to rename files or move them into
new directories. The cp command is used to make copies of
existing files. Use these commands to make three copies of the file
/etc/hosts called c1, c2, and c3 in your home directory:
cd
cp /etc/hosts ~
ls
mv hosts c1
ls
cp c1 c2
ls
cp c2 c3
ls
Now move these newly created files into a new subdirectory called
host_copies:
mkdir host_copies
mv c1 c2 c3 host_copies
ls host_copies
The cp command has an optional flag -r that tells
it to recursively copy all subdirectories beneath a named directory.
The optional -R flag to the ls command enables recursive
display of all subdirectories. The rm command has two optional
flags -r and -f that tell it to recursively/forcibly
delete all subdirectories. These commands can be used to quickly copy,
display, and destroy entire directory hierarchies:
cp -r /etc/skel skel_copy
ls -a -R skel_copy
rm -r -f skel_copy
ls -a -R skel_copy
Note that multiple flags may usually be combined into a single argument
for convenience:
cp -r /etc/skel skel_copy
ls -aR skel_copy
rm -rf skel_copy
ls -aR skel_copy
You should be very careful about using any recursive options to commands. For example,
rm -rf ~
- The grep command prints every line in a file matching a
specified pattern. For example the following command prints every line
containing the phrase local from the file /etc/hosts:
grep local /etc/hosts
- The chmod command controls how other people can access your files. The first argument to chmod tells how to change the file permissions, and consists of
- any of the letters u (for user/owner), g (for group) o (for all others), or a (for all users), followed by
- one of the letters + (to add a permission), - (to take a permission away), or = (to set permissions), followed by
- any of the letters r (for read permission), w (for write permission), or x (for execute permission)
The chmod command takes several optional flags, one of which
(-R) enables recursion. Any additional arguments are assumed to
be the names of files for which permissions should be altered. Note that
ls -l lists files with the user/group/other permissions in the
first few columns of each output line. Practice using the following
sequence of commands, which creates a file, makes it inaccessible to
everyone, then adds full permissions for the owner, execute permissions
for everyone, and read permission for group members:
touch newfile
ls -l newfile
chmod ugo-rwx newfile
ls -l newfile
chmod u=rwx newfile
ls -l newfile
chmod a+x newfile
ls -l newfile
chmod g+r newfile
ls -l newfile
- The tar and gzip/gunzip commands provide
functionality similar to zipfiles under MS-windows. These commands
have extensive options and flags as documented in the online manual,
but the following flags will provide most of the functionality you need:
| Flag | Purpose |
| -z | Automatically compress/uncompress tarfile |
| -x | Extract tarfile |
| -c | Create tarfile |
| -f | Store/Extract tarfile to/from specified file instead of standard I/O |
| -v | Provide verbose information on what tar is doing |
The following three uses will be the most common for this class:
- Create a compressed tarfile called skel.tar.gz containing all the
files in the directory /etc/skel (the tarfile is automatically
filtered through gzip):
tar -zcvf skel.tar.gz /etc/skel
- Display the contents of the compressed tarfile skel.tar.gz
(the compressed tarfile is automatically filtered through gunzip):
tar -ztf skel.tar.gz
- Extract the contents of the compressed tarfile skel.tar.gz
(the compressed tarfile is filtered as above):
tar -zxvf skel.tar.gz
Note that it is also possible to perform compression/decompression
manually using gzip/gunzip, in which case you will need to leave off
the -z option from the tar commands:
tar -cvf skel.tar /etc/skel
gzip skel.tar
ls -l skel.tar.gz
gunzip skel.tar.gz
ls -l skel.tar
tar -tf skel.tar
tar -xvf skel.tar
| Category | Command | Description/Purpose |
|---|
| Command Interpreters (shells) | bash | The bourne-again shell |
| csh | C-shell |
| ksh | Korn shell |
| sh | Bourne shell |
| zsh | Z-Shell |
| Documentation | man | UNIX online manual |
| info | GNU info pages |
| apropos | lookup shell commands |
| whatis | print command summary |
| which | displays location of commands |
| Directory Navigation | cd | change working directory |
| pwd | print working directory |
| ls | list file information |
| File Manipulation | cp | copy files |
| touch | create or backdate files
|
| mv | move files
|
| rm | remove files
|
| mkdir | make directories
|
| rmdir | remove directories
|
| ln | make links (shortcuts) |
| File Access Control | chmod | change file permissions |
| umask | change default file permissions |
| Text Processing | cat | concatenate files |
| more | pager |
| less | alternate pager |
| diff | display differences between two files |
| grep | find lines in a file matching a regular expression |
| egrep | find lines in a file matching an extended regular expression |
| fgrep | find lines in a file matching a fixed string |
| vi | programmer's text editor |
| emacs | alternate text editor |
| pico | simple text editor |
| ispell | interactive spell checker |
| fmt | justifies text |
| expand | convert tabs to spaces |
| unexpand | convert spaces to tabs |
| sort | sorts text |
| Compression/Archiving | tar | archiving program originally designed for tape drives |
| gzip | GNU zip |
| gunzip | GNU unzip |
| zip | MS-Windows compatible zip |
| unzip | MS-Windows compatible unzip |
| Process Information/Control | ps | list currently running processes |
| top | continuously updated list of currently running processes |
| kill | end processes |
| nice | set new process priority |
| renice | set existing process priority |
| Account Information/Control | chsh | change default shell |
| passwd | change password |
| whoami | display name of current user |
| System Information | du | display disk usage |
| find | find files |
| locate | find files quickly |
| finger | display information about a user |
| last | display previous logins to this machine |
| uname | display operating system information |
| who | display who is currently logged in to this machine |
| Network Programs | lynx | console-mode web browser |
| mozilla | graphical web browser |
| wget | automatic web downloader |
| pine | console-mode email client |
| ssh | secure shell (remote logins) |
| Calculators and Mathematical Packages | maple | console-mode interface to the Maple Computer Algebra System (CAS) |
| xmaple | graphical interface to Maple |
| matlab | interactive interface to the matlab (MATrix LABratory) linear algebra programming language |
| latex | mathematical type setter |
| dvips | converts output of latex to a printable format (postscript) |
| dc | console-mode reverse polish notation calculator |
| bc | console-mode ordinary calculator |
| units | convert units |
| Compilers/Software Development | g77 | GNU Fortran-77 compiler |
| ifort | Intel Fortran 90/95 compiler |
| gcc | GNU C compiler |
| g++ | GNU C++ compiler |
| icc | Intel C/C++ compiler |
| perl | Perl scripting language interpreter |
| python | Python scripting language interpreter |
| make | separate compilation utility (speeds up/automates compiling large software projects) |
| Miscellaneous | echo | displays arguments to standard output |
| history | displays recently executed commands |
| tset | controls terminal display settings |
| reset | resets terminal display settings |
| date | displays current date and time |