|
|
Directory and
File Commands
Linux Tips by Burleson Consulting |
In Linux, all entities are
described in files so learning how to examine and manipulate them is a good
place to start. End users are most familiar with files containing data that
they work with on a daily basis. Files can also include such things as end-user
programs in addition to operating system programs, program control parameters,
hardware device descriptions, and system status information.
Files are stored in
directories, which are referred to as folders in a GUI environment. Directories
are arranged in a hierarchical tree structure. Everyday workings within a Linux
environment will require that the user understand where their files are stored,
how to navigate through the directory tree, and how to manipulate files within
the directory structure.
This chapter will introduce the commands needed to
manipulate files and directories from the Linux command line.
Directory Structure
As mentioned, Linux stores files in directories
(folders) which are arranged in a hierarchical or tree structure. The highest
level directory is called root and is identified by a slash character (/). All
other directories in Linux stem from the root directory. It is admittedly
easier to envision the overall structure of the server?s directories using a GUI
interface such as Gnome. Figure 4.1 is an example GUI display of the directory
structure of a Linux server. The figure indicates that the user is in the /boot
directory which contains two subdirectories called /grub and /lost+found
displayed as folders and numerous files displayed as icons depending upon file
type.
Figure 4.1: GUI display of Linux directory structure
Again, this book is written with the assumption that a
GUI environment is not available, and that the user is limited to command line,
but if you do have a GUI available it may help you understand the directory
structure.
The tree command can be used to display the directory
tree from the command line, but the displayed output can be rather cumbersome to
decipher. By default, the tree command starts in the current working directory
and creates a graphic display of all child directories and the files in each
directory.
tree.
|-- System.map -> System.map-2.4.9-e.25
|-- System.map-2.4.9-e.25
-- System.map-2.4.9-e.3
|-- boot.b
|-- chain.b
|-- grub
| |-- device.map
| |-- e2fs_stage1_5
| |-- fat_stage1_5
| |-- ffs_stage1_5
| |-- grub.conf
| |-- menu.lst -> ./grub.conf
| |-- minix_stage1_5
| |-- reiserfs_stage1_5
| |-- splash.xpm.gz
| |-- stage1
| |-- stage2
| `-- vstafs_stage1_5
|-- initrd-2.4.9-e.25.img
|-- initrd-2.4.9-e.3.img
|-- kernel.h -> kernel.h-2.4.9
|-- kernel.h-2.4.9
|-- lost+found [error opening dir]
|-- message
|-- module-info -> module-info-2.4.9-e.25
|-- module-info-2.4.9-e.25
|-- module-info-2.4.9-e.3
|-- os2_d.b
|-- vmlinux-2.4.9-e.25
|-- vmlinux-2.4.9-e.3
|-- vmlinuz -> vmlinuz-2.4.9-e.25
|-- vmlinuz-2.4.9-e.25
`-- vmlinuz-2.4.9-e.3
2 directories, 31 files
To limit the display to
just directories without the files contained in each directory, the ?d option
can be used. The following is an example of the output from the tree command
with and without the ?d option.
$ tree ?d
.
|-- grub
`-- lost+found [error opening dir]
2 directories
Though tree will give us a quick overview of everything
in a directory we are often only concerned with certain directories or files.
Later in this chapter we'll talk about how to examine a more specific subset of
a directory.
Directory Commands
Here we'll talk a bit about navigating around the Linux
filestructure.
What Directory am I In?
When navigating around in Linux it is very easy to
forget what directory you're in. Don't worry, there's a command to help you
find out.
The directory that the user is currently in is referred
to as the working directory and can be displayed using the pwd (print working
directory) command. The pwd command displays the absolute path name for the
current working directory as demonstrated here:
$ pwd
/home/tclark
The directory /home/tclarc is an absolute path because
it begins with a slash. Absolute paths will refer to the exact same location no
matter who types them or where they type them from because the slash specifies
that this path is described from the root directory. The opposite of an
absolute path is a relative path which would refer to a subdirectory of the
working directory. Relative paths cannot begin with slashes.
In order to eliminate the need to frequently issue the
pwd command to determine the current working directory, many Linux users choose
to display the working directory within the Linux command prompt. Some Linux
administrators even provide this service for their users when they create their
Linux accounts. If a user?s command prompt does not contain the working
directory, the command prompt can be modified by changing the prompt string 1
(PS1) shell variable as demonstrated here:
$ PS1="[\u@\h \w]\\$ "
[tclark@appsvr /home/tclark]$
This example of setting the PS1 variable also adds the
username (\u) and hostname (\h) to the prompt. This can be very useful if you
frequently connect to different hosts and as different users.
In order to avoid having to modify the prompt at each
login, the following line of code can be placed within the appropriate
configuration file, such as .bash_profile for the bash shell, within the home
directory. To do this you can use a text editor like vi to add the following
line to your .bash_profile file:
$ export PS1="[\u@\h \w]\\$ "
Note: Files that begin with a period will not appear
when you list the contents of a directory. To see these hidden files use the
command ls ?a
There are even more options which you can put into your
PS1 prompt. While it's nice to keep your prompt fairly short you may find some
of the other options useful. Table 4.1 contains a list of values that can be
displayed within the PS1 and/or PS2 prompt strings:
Symbol |
Displayed Value |
\! |
History number of current command |
\# |
Command number of current command |
\d |
Current date |
\h |
Host name |
\n |
Newline |
\s |
Shell name |
\t |
Current time |
\u |
User name |
\W |
Current working directory |
\w |
Current working directory (full path) |
Table 4.1: PS1 prompt string values
This is an excerpt from "Easy
Linux Commands" by Linux guru Jon Emmons. You can purchase it for only
$19.95 (30%-off) at
this link.