Index->Common UtilitiesCommon Utilities
Overview and Goals
At the completion of this lab, you will be able to...
- Find information on commands using the built-in help system.
- Navigate through the filesystem.
- Copy, rename, and delete a file.
- Look at the contents of a file in various ways.
- Print out a file.
The following utilities are covered in this lab:
man(1),
pwd(1),
ls(1),
cd(1),
mkdir(1),
rmdir(1),
cp(1),
mv(1),
rm(1),
cat(1),
less(1),
lpr(1),
lpq(1),
lprm(1).
The numbers between the parentheses indicates the section number where
the manual page resides.
The rest of this lab assumes that you are logged in and have opened a
terminal / shell window for typing Unix commands.
Linux Philosophy
The basic philosophy of Linux (which it inherited from Unix) is to provide
utilities, each of which does one specific thing, and allows those utilites to
be combined in various ways. The alternative philosophy is to build giant
applications that attempt to do everything. The benefits of the Linux
philosophy is that you can combine utilities in ways that the original
designers never thought of. The drawback is that you must be familiar with
what utilities are available, so you can combine them with others. It is this
relatively steep learning curve that makes learning Linux somewhat of a
challenge.
man Help System
Linux has a built-in help system (in fact, it has several), that describes
the commands that you can use. The command that accesses the help system is
man, which stands for "manual page", i.e., a page from the system
manual. To find help on a particular command, just type "man
command", where command is the command that you
want help on. For example, try typing
man ls
You should see a complete description of the ls(1) command
(don't worry what ls does, we'll cover it later). To exit the
help system, just hit the "Q" key (remember "Q"uit). To browse a manual
page forward and backward, use the space and "B" keys
The man help system is broken into sections. For example, section
1 contains common commands, like the one's used in this lab, section 2
contains C system calls, etc. It is common to write the section number after a
command's name. So, for example ls(1) means the ls command,
which can be found documented in section 1 of the manual page system. It
usually doesn't matter what section a command is found in, but this is a
convention that you should get used to.
The online manual pages can also be accessed through a web browser.
Perhaps to some of you this is more convenient than the text interface.
The URL for the web version of the manual pages is
Filesystem Basics
The Unix filesystem is arranged hierarchically into directories If
you're familiar with Window's "folder" concept, directories are
basically the same thing.
The hierarchical organization of directories resembles an upside
down tree structure. It is upside down because the root is at the
top and the leaves are at the bottom. In Linux (or any Unix variant)
every system has a unique root directory named as "/" (a single
forward slash character). The root directory is different from
your home directory. In fact, your home directory is just one of the
descendants of the root directory.
To see how "deep" the current directory is connected to the root
directory, you can use the pwd commands. When you are in your
home directory, pwd
command will print the absolute path of the current directory and
the output may look something like
/home/johnuser
The above output tells you that:
- "johnuser" is a child directory of "home" and
- "home" is a child directory of "/" (the root
directory)
In the above example the directory "johnuser" is a grandchild
of root.
You should notice that in the above absolute path name the
leftmost slash is the root directory while the other slash(es) is
just delimiter between directory names.
By the way, "pwd" stands for "print working directory". Its output
is called absolute path because it shows the path from the root
directory (the leftmost "/") to the current directory.
| Question(s)
|
- Type the pwd command in a terminal shell window. What is the
absolute path of your home directory?
- How deep is your home directory from the root?
|
The root directory (/) is the great ... great
grandparents that contains everything
in the filesystem. Several subdirectories can be found under root, and
sub-subdirectories can be found beneath those, ad infinitum. Unlike
Windows, Unix does not define anything about logical drives (A:, C:, N:...).
All files and directories are descendant of the root directory.
Each time you log in, the system will automatically put you in your
home directory. You own everything in this directory, and everything
that you create in this directory. All of your personal files will be
kept in this directory. Your home directory is located at
/home/username. A shorthand way of specifying your home
directory is by using the tilde ("~") character. ~
means the same thing as "/home/username".
Files and directories have names. However, unlike other operating systems,
Linux puts no limits on what your filenames look like (from now on, when
"filename" is mentioned, it also applies to the names of directories). So,
you can have a filename like "hello.THIS+is-A.filename". There are
certain characters you cannot use, but if you stick with alphanumeric
characters, ".", "_", and "-", you'll be safe. You should also be aware that
filenames are case sensitive. That means "file" is not the
same thing as "FILE".
Most directories have files in them. To see what files exist in the
current working directory, use the ls(1) command. Try typing
"ls" now. If you're account is new, you may not have any files in
your home directory. File names starting with "." will be hidden from
the normal output of "ls". To show these "hidden" files, use
the "ls -a" command.
In order to distinguish regular files from directory in the "ls"
output, you can type the "-F" option.
| Question(s)
|
- Try both commands "ls" and "ls -a" from your home directory.
Mention three hidden files whose name contains other special
(non-alphabenumerical) characters besides the first dot.
|
There are two ways to locate files and directories in Linux:
absolute and relative paths.
An "absolute path" tells Linux how to find a file/directory from the root
directory while a relative path tells Linux how to find a
file/directory from the current directory.
For example /home/johnuser/tmp/file.c is an absolute path to
the file file.c that lives in the tmp directory of the
johnuser directory, of the home directory under root
(/).
The main difference is that relative paths don't begin with /.
Assuming the current working directory is /home/johnuser,
the pathname tmp/file.c will access the file file.c in the
tmp subdirectory (the same file accessed in the previous
paragraph).
You can navigate through the filesystem using the cd
command. Typing "cd path" will take you to the
directory accessed by the given path. path can be
either absolute or relative. Try typing "cd /home". You will
be taken to that directory. Try typing "cd ~", and you should
be returned to your home directory. There is a special directory
shortcut ".." that will move you up one directory. Try typing
"cd ..", followed by "pwd". You should see that
you've been moved from your home directory to the /home
directory. Return to your home directory.
| Question(s)
|
- What is the difference between "/home" and your home directory?
- Pick a file in a subdirectory under your home directory. Write
both the absolute and relative path name of the file.
|
You can create a subdirectory by using the mkdir(1) command. You
can remove empty subdirectories by using the rmdir(1) command. Try
the following sequence of commands:
| cd ~ | Go home |
| mkdir foo |
Make a subdirectory called foo |
| ls | Make sure foo was created |
| cd foo | Move to your new subdirectory |
| ls | Is there anything there? |
| cd .. | Go up one directory to your home |
| rmdir foo | Remove the subdirectory |
File Utilities
Now that you know how to get around the filesystem, let's look at files and
utilities commonly used for file manipulation.
Browsing
Two common utilities you can use to browse the content of a file are
"cat" and "less". Use "cat" when the file has small number of lines
(shorter the height of your terminal window). For browsing longer files,
use "less". This utility allows you (among other things) to scroll
forward / backward, jump to a certain line number or jump by percentage.
| Question(s)
|
- Use "less" to browse a file of your choice and type the following
command:
less the-name-of-the-file
While browsing the file:
- press spacebar to scroll forward a page
- pressa 'b' key to scroll backward a page
- press "Enter" to scroll forward 1 line.
- type "7g" to jump to the 7-th line
- type "50p" to jump to the middle of the file (50p = 50
percents)
- press 'q' to quit browsing.
|
Copying
You copy files using the cp command. The format is
cp source_path destination_path
For the paths you can specify either an absolute or a relative path.
Look at the following examples:
- cp file.c file.c.backup
- will make a copy of the file file.c as the file
file.c.backup (both are in the current directory).
- cp file.c ..
- will copy file.c (in the current directory)
to the parent directory. The copy will have the same name.
- cp file.c ../filemod.c
- similar to the previous command, except the copy has a new
name filemod.c
- cp /etc/services .
- copy a file ("services") under the "/etc/" directory to the
current directory. The "." is a shorthand for "current directory".
This last example the source file is specified using its absolute
path name.
| Question(s)
|
- Suppose you are in a subdirectory "~/Projects" (recall that "~"
refers to your home directory), and you want to copy a file
"data.txt" to a sibling directory "~/Data".
Write four different possible ways of using "cp" to do the task (Use
a combination of absolute, relative path, or ".." to specify the file
name).
|
Renaming
To move or rename a file, using the mv command. It functions
in the same way as cp, but moves a file instead of copying it.
Look at the following examples:
- mv file.c file.c.backup
- will rename the file file.c to a new name
file.c.backup.
- mv file.c ..
- will move file.c (in the current directory)
to the parent directory
- mv file.c ../filemod.c
- similar to the previous command, except we rename the
file to a new name filemod.c
Removing
To remove a file, use rm. The format is
rm path/to/the/file/to/delete
BE CAREFUL when using this command, Unix
does not have "undelete" utility. To prompt for confirmation use "rm -i":
rm -i path/to/the/file/to/delete
Printing
To print a file, use the command lpr. The format is
"lpr pathname". This will send the given file to
the printer.
The printer is shared among all the other computers in the lab, and if the
printer is currently busy, your computer will queue up the file and wait until
the printer is free. To see what file(s) you have waiting to be printed use
the lpq command. This shows you all the print jobs that
are waiting to finish printing.
If you want to abort a print job, use the lprm command.
For this you will need a job's number, which can be found in the
middle of the output of lpq. lpq shows its output in a
7-column format. Job numbers are shown in the fourth column under "Job"
heading. To stop a job from printing, use the command "lprm
job_num", where job_num is the job number
that you want to stop/cancel.
Top
|