Exploring The System: Ls File Less
Exploring The System: Ls File Less
Exploring The System: Ls File Less
see directory contents and determine a variety of important file and directory attributes. As we have seen, we can simply enter ls to see a list of files
and subdirectories contained in the current working directory:
[me@linuxbox ~]$ ls
Desktop Documents Music Pictures
Public
Templates Videos
src
tmp
Templates Videos
We can also change the format of the output to reveal more detail:
[me@linuxbox
total 56
drwxrwxr-x 2
drwxrwxr-x 2
drwxrwxr-x 2
drwxrwxr-x 2
drwxrwxr-x 2
drwxrwxr-x 2
drwxrwxr-x 2
~]$ ls -l
me
me
me
me
me
me
me
me
me
me
me
me
me
me
4096
4096
4096
4096
4096
4096
4096
2012-10-26
2012-10-26
2012-10-26
2012-10-26
2012-10-26
2012-10-26
2012-10-26
17:20
17:20
17:20
17:20
17:20
17:20
17:20
Desktop
Documents
Music
Pictures
Public
Templates
Videos
14
Chapter 3
Well add the long option --reverse to reverse the order of the sort:
[me@linuxbox ~]$ ls -lt --reverse
Long Option
Description
-a
--all
-d
--directory
-F
--classify
-h
--human-readable
-l
-r
--reverse
-S
-t
1
1
1
1
1
1
1
1
1
root
root
root
root
root
root
root
root
root
15
Lets look at the different fields from one of the files and examine their
meanings in Table 3-2.
Table 3-2: ls Long Listing Fields
Field
Meaning
-rw-rr--
root
root
32059
2012-04-03 11:05
oo-cd-cover.odf
When invoked, the file command will print a brief description of the
files contents. For example:
[me@linuxbox ~]$ file picture.jpg
picture.jpg: JPEG image data, JFIF standard 1.01
16
Chapter 3
There are many kinds of files. In fact, one of the common ideas in Unixlike operating systems such as Linux is that everything is a file. As we proceed with our lessons, we will see just how true that statement is.
While many of the files on your system are familiar, for example MP3
and JPEG files, many kinds are a little less obvious, and a few are quite
strange.
17
Once started, the less program allows you to scroll forward and backward through a text file. For example, to examine the file that defines all
the systems user accounts, enter the following command:
[me@linuxbox ~]$ less /etc/passwd
Once the less program starts, we can view the contents of the file. If the
file is longer than one page, we can scroll up and down. To exit less, press
the Q key.
Table 3-3 lists the most common keyboard commands used by less.
Table 3-3: less Commands
Command
PAGE UP
Action
or b
PAGE DOWN
Spacebar
Up Arrow
Down Arrow
1G or g
/characters
Quit less.
LESS IS MORE
The less program was designed as an improved replacement of an earlier Unix
program called more. Its name is a play on the phrase less is morea motto of
modernist architects and designers.
less falls into the class of programs called pagers, programs that allow the
easy viewing of long text documents in a page-by-page manner. Whereas the
more program could only page forward, the less program allows paging both
forward and backward and has many other features as well.
18
Chapter 3
A Guided Tour
The filesystem layout on your Linux system is much like that found on other
Unix-like systems. The design is actually specified in a published standard
called the Linux Filesystem Hierarchy Standard. Not all Linux distributions conform to the standard exactly, but most come pretty close.
Next, we are going to wander around the filesystem ourselves to see
what makes our Linux system tick. This will give you a chance to practice
your navigation skills. One of the things we will discover is that many of the
interesting files are in plain, human-readable text. As we go about our tour,
try the following:
1.
2.
3.
4.
Note: Remember the copy-and-paste trick! If you are using a mouse, you can double-click a
filename to copy it and middle-click to paste it into commands.
As we wander around, dont be afraid to look at stuff. Regular users are
largely prohibited from messing things up. Thats the system administrators
job! If a command complains about something, just move on to something
else. Spend some time looking around. The system is ours to explore.
Remember, in Linux, there are no secrets!
Table 3-4 lists just a few of the directories we can explore. Feel free to
try more!
Table 3-4: Directories Found on Linux Systems
Directory
Comments
/bin
/boot
(continued )
19
Comments
/dev
/etc
20
Chapter 3
/home
/lib
/lost+found
Each formatted partition or device using a Linux filesystem, such as ext3, will have this directory. It is used
in the case of a partial recovery from a filesystem corruption event. Unless something really bad has happened to your system, this directory will remain empty.
/media
/mnt
/opt
Comments
/proc
/root
/sbin
/tmp
/usr
/usr/bin
/usr/lib
/usr/local
/usr/sbin
/usr/share
/usr/share/doc
21
Comments
/var
/var/log
Symbolic Links
As we look around, we are likely to see a directory listing with an entry
like this:
lrwxrwxrwx 1 root root
Notice how the first letter of the listing is l and the entry seems to
have two filenames? This is a special kind of a file called a symbolic link (also
known as a soft link or symlink). In most Unix-like systems it is possible to
have a file referenced by multiple names. While the value of this may not
be obvious now, it is really a useful feature.
Picture this scenario: A program requires the use of a shared resource
of some kind contained in a file named foo, but foo has frequent version
changes. It would be good to include the version number in the filename
so the administrator or other interested party could see what version of foo
is installed. This presents a problem. If we change the name of the shared
resource, we have to track down every program that might use it and change
it to look for a new resource name every time a new version of the resource
is installed. That doesnt sound like fun at all.
Here is where symbolic links save the day. Lets say we install version 2.6
of foo, which has the filename foo-2.6, and then create a symbolic link simply
called foo that points to foo-2.6. This means that when a program opens the
file foo, it is actually opening the file foo-2.6. Now everybody is happy. The
programs that rely on foo can find it, and we can still see what actual version
is installed. When it is time to upgrade to foo-2.7, we just add the file to our
system, delete the symbolic link foo, and create a new one that points to the
new version. Not only does this solve the problem of the version upgrade,
but it also allows us to keep both versions on our machine. Imagine that
foo-2.7 has a bug (damn those developers!) and we need to revert to the old
22
Chapter 3
version. Again, we just delete the symbolic link pointing to the new version
and create a new symbolic link pointing to the old version.
The directory listing above (from the /lib directory of a Fedora system)
shows a symbolic link called libc.so.6 that points to a shared library file called
libc-2.6.so. This means that programs looking for libc.so.6 will actually get the
file libc-2.6.so. We will learn how to create symbolic links in the next chapter.
HARD LINKS
While we are on the subject of links, we need to mention that there is a second
type of link called a hard link. Hard links also allow files to have multiple names,
but they do it in a different way. Well talk more about the differences between
symbolic and hard links in the next chapter.
23