cd
Command
The cd (change directory) command is the basic navigation tool for moving your current
location to different parts of the Linux file system. You can move directly to a directory by
typing the command, followed by a pathname or directory. For example, the following
command will move you to the /usr/bin directory:
# cd /usr/bin
When you’re in that directory, you can move up to the /usr directory with the following
command:
# cd ..
You could also move to the root directory, or /, while in the /usr/bin directory by using the
following command:
# cd ../..
Finally, you can always go back to your home directory (where your files are) by using either
of the following commands:
# cd
or
# cd ~
The
pwd (print working directory)
command tells you where you are, and prints the working
(current) directory. For example, if you execute
# cd /usr/bin
and then type
# pwd
4
Reading and Navigation Commands
47
you’ll see
/usr/bin
Although there’s a man page for the pwd command, chances are that when you use pwd, you’re
using a pwd built into your shell. How do you tell? If you try calling pwd with the following
command, you should see only the current working directory printed:
# pwd --help
Instead, try calling pwd with
# /bin/pwd --help
You’ll see a short help file for the pwd command, and not the current directory.
find Command
The find command is a powerful searching utility you can use to find files on your hard drive.
You can search your hard drive for files easily with a simple find command line. For example,
to search for the spell command under the /usr directory, you would use
# find /usr -name spell -print
You can also use the find command to find files by date; you can also specify a range of times.
For example, to find programs in the /usr/bin directory that you have not used in the last
one hundred days, you can try:
# find /usr/bin -type f -atime +100 -print
To find any files, either new or modified, that are one or fewer days old in the /usr/bin
directory, you can use
# find /usr/bin -type f -mtime -1 -print
whereis Command
The whereis command can quickly find files, and it also shows you where the file’s binary,
source, and manual pages reside. For example, the following command shows that the find
command is in the /usr/bin directory, and its man page is in the /usr/man/man1 directory:
# whereis find
find: /usr/bin/find /usr/man/man1/find.1
You can also use whereis to find only the binary version of the program with
# whereis -b find
find: /usr/bin/find
If whereis cannot find your request, you’ll get an empty return string, for example:
# whereis foo
foo:
Part of the problem could also be that the file is not in any of the directories the whereis
command searches. The directories whereis looks in are hard-coded into the program.
Although this may seem like a drawback, limiting searches to known directories such as /usr/
man, /usr/bin, or /usr/sbin can speed up the task of finding files.
locate Command
One way to speed up searches for files is not to search your file directories! You can do this
by using a program like locate, which uses a single database of filenames and locations, and
which saves time by looking in a single file instead of traversing your hard drive. Finding a
file using locate is much faster than the find command because locate will go directly to the
database file, find any matching filenames, and print its results.
Locate is easy to use. For example, to find all the PostScript files on your system, you can enter
# locate *.ps
Almost instantly, the files are printed to your screen.
whatis command
may be able to help you quickly find out what a program is with a
summary line derived from the program’s manual page. For example, to find out what is
whereis (not whereis whatis!), you can enter
# whatis whereis
whereis (1)- locate the binary, source, and manual page files for a command
apropos command
what if you want to do something and can’t remember which program does
what? In this case, you can turn to the apropos command.
For example, if you can’t remember which command searches for files, you can enter
# apropos search
apropos (1)
- search the whatis database for strings
badblocks (8)
- search a device for bad blocks
bsearch (3)
- binary search of a sorted array.
conflict (8)
- search for alias/password conflicts
find (1)
- search for files in a directory hierarchy
ls Command
The ls (list directory) command will quickly become one of your most often used programs.
In its simplest form, ls lists nearly all of the files in the current directory. But this command,
which has such a short name, probably has more command-line options (more than 75 at last
count) than any other program!
In the simple form, ls lists your files:
# ls
News
author.msg
auto
axhome
documents
mail
nsmail
reading
research
search
vultures.msg
You can also list the files as a single line, with comma separations, with the -m option:
# ls -m
News, author.msg, auto, axhome, documents, mail, nsmail, reading,
̄ research, search, vultures.msg
If you don’t like this type of listing, you can have your files sorted horizontally, instead of
vertically (the default), with the -x option:
# ls -x
News
author.msg
auto
axhome
But are all these just files, or are there several directories? One way to find out is to use the
-F option:
# ls -F
News/
author.msg
auto/
axhome/
documents/
mail/
nsmail/
reading/
research/
search*
vultures.msg
As you can see, the -F option causes the ls command to show the directories, each with a /
character appended to the filename. The asterisk (*) shows that the file search is an executable
program. But are these all the files in this directory? If you want to see everything, you can
use the -a option with -F, as follows:
# ls -aF
./
../
.dt/
.dtprofile*
.neditdb
.netscape/
Long Directory Listing
Would you like even more information about your files? You can view the long format listing
by using the ls -l option, for example:
# ls -l
there are eight different columns. The first column is the file’s permissions
flags, which are covered in Hour 21, “Handling Files.” These flags generally show the file’s
type, and who can read, write (or modify or delete), or run the file. The next column shows
the number of links, which are discussed in Hour 5, “Manipulation and Searching
Commands.” Next is the owner name, followed by group name. Owners and groups are
discussed in Hour 21. The file size is listed next, followed by a timestamp of the file or
directory was created or last modified. The last column, obviously, is each file’s name.
The ls command also supports using wildcards, or regular expressions, which means you can
use options similar to (and much more complex than) the examples you’ve seen with the find
and locate commands. For example, if you only want to search for text files in the current
directory, you can use
# ls *.txt
Finally, if you want to see all of the files on your system, you can use the ls -R option, which
recursively descends directories to show you the contents. Although you can use this
approach to search for files and build a catalog of the files on your system, you should be
warned that it might take several minutes to list your files. The listing may also include files
you don’t want listed, or files on other operating system filesystems, such as DOS or
Windows, especially if you use
# ls -R /
A better approach might be to use the -d option with -R to list only a certain number of
directory levels. For example, the following command will search three directory levels along
the root or / directory:
# ls -Rd /*/*/*
However, there’s a much better utility for getting a picture of the directory structure of your
system, the tree command, which is discussed later in this hour.
The dir command works like the default ls command, listing the files in sorted columns, for
example:
# dir
News
author.msg
auto
axhome
documents
mail
nsmail
reading
research
search
vultures.msg
The vdir command works like the ls -l option, and presents a long format listing by default,
for example:
# vdir
total 15
tree Command
You now know how to list the contents of your directories, but you may also be interested
in the directory structure of your system, or the directory structure of a particular tree of your
system (such as /usr/X11R6). For example, ls -R will recursively print out your directories,
but how are these directories related to each other? If you would like a more direct, graphical
view of your directories, a directory listing utility can help.
cat Command
The cat (concatenate file) command is used to send the contents of files to your screen. This
command may also be used to send files’ contents into other files. Hour 6 covers terms such
as standard input, standard output, and redirection, and this section shows you some basic
uses for this command.
Although cat may be useful for reading short files, it is usually used to either combine, create,
overwrite, or append files. To use cat to look at a short file, you can enter
# cat test.txt
This text file was created by the cat command.
Cat could be the world’s simplest text editor.
# cat -n test.txt
1 This text file was created by the cat command.
2 Cat could be the world’s simplest text editor.
You can also use cat to look at several files at once, because cat accepts wildcards, for example:
# cat -n test*
1 This text file was created by the cat command.
2 Cat could be the world’s simplest text editor.
As you can see, cat has also included a second file in its output, and has numbered each line
of the output, not each file. Note that you could also see both files with
# cat test.txt test2.txt
The output will be exactly the same as if you’d used a wildcard. But looking at several files
is only one way to use cat. You can also use the cat command with the redirection operator
> to combine files. For example, if you would like to combine test.txt and test2.txt into
a third file called test3.txt, you can use
# cat test* > test3.txt
You can check the result with
# ls -l test*
finally, here’s a trick you can use if you want to create a short text file without running a word
processor or text editor. Because the cat command can read the standard input (more about
this in Hour 6), you can make the cat command create a file and fill it with your keystrokes.
Here’s how:
# cat > myfile.txt
Now, enter some text:
# cat > myfile.txt
This is the cat word processor.
This is the end of the file.
Then, when you’re done typing, press Ctrl+D to close the file.
more command is a traditional pager in the sense that it provides the basic features of early
pagers. You can use more on the command line with
# more longfile.txt
1.You can scroll backwards and forwards through text files using your cursor keys.
2.You can navigate through files with bookmarks, by line numbers, or by percentage
of file.
3. Sophisticated searches, pattern options, and highlighting through multiple files.
rm Command
The rm command deletes files. This command has several simple options, but should be used
cautiously. Why? Because when rm deletes a file, it is gone (you may be able to recover portions
of text files,
Always running Linux while logged in as the root operator and using the rm command has
caused many untold tales of woe and grief. Why? Because with one simple command you can
wipe out not only your Linux system, but also any mounted filesystems, including DOS
partitions, flash RAM cards, or removable hard drives, as follows:
# rm -fr /*
This command removes all files and directories recursively (with the -r option), starting at
the root or / directory.
The rm command will delete one or several files from the command line. You can use any of
the following:
# rm file
# rm file1 file2 file2
# rm file*
One of the safer ways to use rm is through the -i or interactive option, where you’ll be asked
if you want to delete the file, for example:
# rm -i new*
rm: remove `newfile’? y
rm: remove `newfile2'? y
You can also force file deletion by using the -f option, as in
# rm -f new*
However, if rm finds a directory, even if it is empty, it will not delete the directory, and
complains, even if you use -f, as in the following:
# rm -f temp*
rm: temp: is a directory
rm: temp2: is a directory
When you combine -f and -r, the recursive option, you can delete directories and all files
or directories found within (if you own them; see Hour 21, “Handling Files”), as in the
following example:
# rm -fr temp*
The -fr option also makes rm act like the rmdir command
mkdir Command
The mkdir command can create one or several directories with a single command line. You
may also be surprised to know that mkdir can also create a whole hierarchy of directories,
which includes parent and children, with a single command line.
This command is one of the basic tools (along with cp and mv) you’ll use to organize your
information. Now, take a look at some examples. The following simple command line creates
a single directory:
# mkdir temp
But you can also create multiple directories with
# mkdir temp2 temp3 temp4
You’d think that you could also type the following to make a directory named child under
temp:
# mkdir temp/child
And you can, because the temp directory exists (you just created it). But, suppose you type
# mkdir temp5/child
mkdir: cannot make directory `temp5/child’: No such file or directory
As you can see, mkdir complained because the temp5 directory did not exist.
rmdir Command
The rmdir command is used to remove directories. To remove a directory, all you have to do
is type
# rmdir tempdirectory
But there’s a catch: the directory must be empty first! If you try to delete a directory with any
files, you’ll get an error message like this:
# rmdir temp5
rmdir: temp5: Directory not empty
In this example, temp5 also contains other directories. The rmdir command would also
complain if a directory contains only files and not directories. You can use the rm command
to remove the files first (remember to be careful if you use the -fr option), or you can move
the files somewhere else, or rename the directory, with the mv command, discussed next.
mv Command
The mv command, called a rename command but known to many as a move command, will
indeed rename files or directories, but it will also move them around your file system.
Actually, in the technical sense, the files or directories are not really moved. If you insist on
knowing all the gory details, read the Linux System Administrator’s Guide, available through
http://sunsite.unc.edu/LDP/LDP/sag/index.html
In its simplest form, mv can rename files, for example:
# touch file1
# mv file1 file2
This command renames file1 to file2 . However, besides renaming files, mv can rename
directories, whether empty or not, for example:
# mkdir -p temp/temp2/temp3
# mv temp newtemp
cp Command
The cp, or copy, command is used to copy files or directories. This command has nearly 40
command-line options. They won’t all be covered here, but you’ll learn about some of the
most commonly used options, which will save you time and trouble.
You’ll most likely first use cp in its simplest form, for example:
# cp file1 file2
This creates file2, and unlike mv, leaves file1 in place. But you must be careful when using
cp, because you can copy a file onto a second file, effectively replacing it! In this regard, cp
can act just like mv .
Hard and Symbolic Links
with the ln Command
Linux supports both hard and symbolic links. Although it is not important that you
understand how links work in Linux, you should understand the difference between these
two types of links and how to use links while you use Linux. To create hard or symbolic links,
you use the ln, or link, command.
The ln command creates both types of links. If you use the ln command to create a hard link,
you specify a second file on the command line you can use to reference the original file, for
example:
# cat > file1
This is file1
# ln file1 file2
If you delete file1 , file2 will remain.
If you make changes to file1, such as adding text, these changes will appear in file2, and
if you make changes to file2, file1 will also be updated. You should also know that although
you can see two files, each 14 characters in size, only 14 characters of hard drive space are used
(OK, technically more than that, but it depends on the block size of the partition or type of
hard drive).
On the other hand, although a symbolic link can be useful, it also has a drawback. The next
examples show you why. First, to create a symbolic link, use the ln command -s option:
# ln -s file1 file2
# ls -l file*
This tells you that file2 is a symbolic link to
file1. Also note that file2 is shorter than file1 . Symbolic links are different from hard links
in that a symbolic link is merely a pathname, or alias, to the original file. Nothing happens
to the original file if you delete the symbolic link. However, if you delete the original file, your
symbolic link won’t help you at all:
# rm -f file1
# cat file2
cat: file2: No such file or directory
mc command
called Midnight Commander, is a graphical interface for handling files.type the following at the command line:
# mc
GREP command
This section introduces you to the family of grep commands. You’ll learn about grep, egrep,
and fgrep. In order to use these commands, you should know how to use some of the pattern-
matching techniques already discussed. You’ll use these commands to search through files
and extract text. Each of these programs works by searching each line in a file. You can search
single files or a range of files.
Regular expressions are patterns, using special syntax, that match strings (usually in text in
files, unless your search is for filenames). There are also extended regular expressions, but the
difference, important for syntax, should not deter you from the valuable lesson of being able
to construct patterns that will accurately match your desired search targets. This is important
if you’re looking for text in files, and critical if you’re performing potentially dangerous tasks,
such as multiple file deletions across your system.
You can build an infinite number of regular expressions using only a small subset of pattern
characters. Here’s a short list of some of these characters. You should be familiar with at least
one (the asterisk) from the previous examples:
* Matches any character
?
or .
[xxx]
or [x-x]
\x
^pattern
$pattern
Matches a character in a range of characters
Matches a character such as ? or \
Matches pattern to the beginning of a line
Matches pattern to the end of a line
Each of the grep commands is basically the same and has nearly 20 different command-line
options. The only real difference is that egrep uses a slightly different syntax for its pattern
matching, whereas the frep command uses fixed strings. You’ll see examples of each program,
using some of the common options. For specific details about the pattern-matching
capabilities of these programs, see the grep manual page.
following syntax:
# grep ^[0-9] guide.txt
1 Introduction to Linux
2 Obtaining and Installing Linux
3 Linux Tutorial
4 System Administration
...
# egrep ^[0-9] guide.txt
1 Introduction to Linux
2 Obtaining and Installing Linux
3 Linux Tutorial
4 System Administration
...
# fgrep ^[0-9] guide.txt
1
40
85
137
1
40
85
137
You can see that grep and egrep returned a search (I’ve deleted all the output except the first
four lines). Notice, however, that fgrep cannot handle regular expressions. You must use
fixed patterns, or strings with the fgrep command, for example: