Shell Navigation
Shell Navigation
Those new to the command line will need to pay close attention
to this lesson since the concepts will take some getting used to.
this:
One important difference between Windows and Unix-like
operating systems such as Linux is that Linux does not employ
the concept of drive letters. While Windows drive letters split the
file system into a series of different trees (one for each device),
Linux always has a single tree. Different storage devices may be
different branches of the tree, but there is always just a single
tree.
pwd
Since the command line interface cannot provide graphic pictures
of the file system structure, we must have a different way of
representing it. To do this, think of the file system tree as a
maze, and that we are standing in it. At any given moment, we
are located in a single directory. Inside that directory, we can see
its files and the pathway to its parent directory and the pathways
to the subdirectories of the directory in which we are standing.
[me@linuxbox me]$ ls
Desktop Downloads foo.txt Pictures
Templates
Documents examples.desktop Music Public Videos
We will come back to ls in the next lesson. There are a lot of fun
things you can do with it, but we have to talk about pathnames
and directories a bit first.
cd
To change the working directory (where we are standing in the
maze) we use the cd command. To do this, we type cd followed
by the pathname of the desired working directory. A pathname is
the route we take along the branches of the tree to get to the
directory we want. Pathnames can be specified two different
ways; absolute pathnames or relative pathnames. Let's look with
absolute pathnames first.
The "." notation refers to the working directory itself and the ".."
notation refers to the working directory's parent directory. Here is
how it works. Let's change the working directory to /usr/bin
again:
me@linuxbox bin]$ cd ..
me@linuxbox usr]$ pwd
/usr
Two different methods with identical results. Which one should
we use? The one that requires the least typing!
A Few Shortcuts
If we type cd followed by nothing, cd will change the working
directory to our home directory.
Looking Around
Now that we know how to move from working directory to
working directory, we're going to take a tour of our Linux system
and, along the way, learn some things about what makes it tick.
But before we begin, we have to learn about some tools that will
come in handy during our journey. These are:
ls
The ls command is used to list the contents of a directory. It is
probably the most commonly used Linux command. It can be
used in a number of different ways. Here are some examples:
Command Result
ls /bin List the files in the /bin directory (or any other directory we care to
specify)
ls -la .. List all files (even ones with names beginning with a period character,
which are normally hidden) in the parent of the working directory in long
format
File Name
The name of the file or directory.
Modification Time
The last time the file was modified. If the last modification
occurred more than six months in the past, the date and
year are displayed. Otherwise, the time of day is shown.
Size
The size of the file in bytes.
Group
The name of the group that has file permissions in addition
to the file's owner.
Owner
The name of the user who owns the file.
File Permissions
A representation of the file's access permissions. The first
character is the type of file. A "-" indicates a regular
(ordinary) file. A "d" indicates a directory. The second set of
three characters represent the read, write, and execution
rights of the file's owner. The next three represent the rights
of the file's group, and the final three represent the rights
granted to everybody else. We'll discuss this in more detail
in a later lesson.
less
less is a program that lets us view text files. This is very handy
since many of the files used to control and configure Linux are
human readable.
What is "text"?
There are many ways to represent information on a computer. All
methods involve defining a relationship between the information
and some numbers that will be used to represent it. Computers,
after all, only understand numbers and all data is converted to
numeric representation.
less text_file
Controlling less
Once started, less will display the text file one page at a time.
We can use the Page Up and Page Down keys to move through
the text file. To exit less, we type "q". Here are some commands
that less will accept:
Command Action
q Quit
file
As we wander around our Linux system, it is helpful to determine
what kind of data a file contains before we try to view it. This is
where the file command comes in. file will examine a file and
tell us what kind of file it is.
file name_of_file
GNU tar archive A tape archive file. A common way of no, use tar tvf to
storing groups of files. view listing.
gzip compressed data An archive compressed with gzip no
Manipulating Files
This lesson will introduce the following commands:
These four commands are among the most frequently used Linux
commands. They are the basic commands for manipulating both
files and directories.
Wildcards
Before we begin with our commands, we'll first look at a shell
feature that makes these commands so powerful. Since the shell
uses filenames so much, it provides special characters to help you
rapidly specify groups of filenames. These special characters are
called wildcards. Wildcards allow you to select filenames based on
patterns of characters. The table below lists the wildcards and
what they select:
Wildcard Meaning
[characters] Matches any character that is a member of the set characters. The set of
characters may also be expressed as a POSIX character class such as one of
the following:
POSIX Character Classes
[:digit:] Numerals
[:upper:] Uppercase alphabetic characters
Pattern Matches
* All filenames
cp
The cp program copies files and directories. In its simplest form,
it copies a single file:
Command Results
cp -i file1 Like above however, since the "-i" (interactive) option is specified,
file2 if file2 exists, the user is prompted before it is overwritten with the
contents of file1.
mv
The mv command moves or renames files and directories
depending on how it is used. It will either move one or more files
to a different directory, or it will rename a file or directory. To
rename a file, it is used like this:
Command Results
mv -i file1 Like above however, since the "-i" (interactive) option is specified,
file2 if file2 exists, the user is prompted before it is overwritten with the
contents of file1.
rm
The rm command removes (deletes) files and directories.
[me@linuxbox me]$ rm file...
Command Results
rm file1 Delete file1 and file2.
file2
rm -i file1 Like above however, since the "-i" (interactive) option is specified,
file2 the user is prompted before each file is deleted.
mkdir
The mkdir command is used to create directories. To use it, you
simply type:
Command Results
cp *.txt Copy all files in the current working directory with names ending
text_files with the characters ".txt" to an existing directory named text_files.
mv dir1 Move the subdirectory dir1 and all the files ending in ".bak" in the
../*.bak current working directory's parent directory to an existing directory
dir2 named dir2.
rm *~ Delete all files in the current working directory that end with the
character "~". Some applications create backup files using this
naming scheme. Using this command will clean them out of a
directory.
Further Reading
Chapter 4 of The Linux Command Line covers this topic in
more detail