Navigating the Linux File System - Linux Desktops - Linux All-in-One For Dummies, 5th Edition (2014)

Linux All-in-One For Dummies, 5th Edition (2014)

Book II. Linux Desktops

Chapter 4. Navigating the Linux File System

In This Chapter

arrow Finding out about the Linux file system

arrow Navigating the system with Linux commands

arrow Understanding file permissions

arrow Manipulating files and directories with Linux commands

To use files and directories well, you need to understand the concept of a hierarchical file system. Even if you use the GUI file managers (addressed in the next chapter) to access files and folders (folders are also called directories), you can benefit from a lay of the land of the file system.

This chapter introduces the Linux file system and shows you how to work with files and directories using several Linux commands.

Understanding the Linux File System

Like any other operating system, Linux organizes information in files and directories. A directory is a special file that can contain other files and directories. Because a directory can contain other directories, this method of organizing files gives rise to a hierarchical structure. This hierarchical organization of files is the file system.

The Linux file system gives you a unified view of all storage on your PC. The file system has a single root directory, indicated by a forward slash (/). Within the root directory is a hierarchy of files and directories. Parts of the file system can reside in different physical media, such as a hard drive, an external drive, and a CD-ROM. Figure 4-1 illustrates the concept of the Linux file system (which is the same in any Linux system) and how it spans multiple physical devices.


Figure 4-1: The Linux file system provides a unified view of storage that may span multiple storage devices.

If you’re familiar with other operating systems such as Windows, you may find something missing in the Linux file system: You don’t find drive letters in Linux. All disk drives and CD-ROM drives are part of a single file system.

remember.eps In Linux, you can have long filenames (up to 256 characters), and filenames are case-sensitive. Often these filenames have multiple extensions, such as sample.tar.Z. Unix filenames can take many forms, such as the following: index.html, Makefile, binutils-, vsftpd_2.0.3-1_i386.deb, .bash_profile, and httpd_src.tar.gz.

To locate a file, you need more than just the filename. You also need information about the directory hierarchy. The extended filename, showing the full hierarchy of directories leading to the file, is the pathname. As the name implies, it’s the path to the file through the maze of the file system. Figure 4-2 shows a typical pathname for a file in Linux.


Figure 4-2: The path for the file shows the sequence of directories leading up to the file.

As Figure 4-2 shows, the pathname has the following parts:

· The root directory, indicated by a forward slash (/) character.

· The directory hierarchy, with each directory name separated from the previous one by a forward slash (/) character. A / appears after the last directory name.

· The filename, with a name and one or more optional extensions. (A period appears before each extension.)

The Linux file system has a well-defined set of top-level directories, and some of these directories have specific purposes. Finding your way around the file system is easier if you know the purpose of these directories. You also become adept at guessing where to look for specific types of files when you face a new situation. Table 4-1 briefly describes the top-level directories in the Linux file system.

Table 4-1 Top-Level Directories in the Linux File System




Base of the file system. All files and directories are contained logically in the root, or /, directory, regardless of their physical locations.


Executable programs that are part of the Linux operating system. Many Linux commands, such as cat, cp, ls, more, and tar, are located in /bin.


Linux kernel and other files that the LILO and GRUB boot managers need. (The kernel and other files can be anywhere, but placing them in the /boot directory is customary.)


Special files that represent devices attached to the system.


Most system configuration files and the initialization scripts (in the /etc/rc.d subdirectory).


Home directories of all users. User edulaney’s home directory, for example, is /home/edulaney.


Library files for all programs stored in /sbin and /bin directories (including the loadable driver modules) needed to start Linux.


Lost files. Every disk partition has a lost+found directory.


The /media/cdrom or /media/cdrom0 directory is for mounting the CD/DVD-ROM drive. If you have a CD/DVD recorder, you find a /media/cdrecorder directory instead of /media/cdrom and may also find /media/DVD. Used for mounting file systems on removable media, such as CD/DVD-ROM drives, flash drives, external drives, floppy disks, and Zip drives. If you have a very old machine that still has a floppy drive on it, then the /media/floppy directory will also exist for mounting floppy disks.


Temporarily mounted file systems.


Storage for large application software packages. For example, some distributions install the Office suite in the /opt directory.


Various information about the processes running in the Linux system.


Home directory for the root user.


Executable files representing commands typically used for system administration tasks and used by the root user. Commands such as halt and shutdown reside in the /sbin directory.


Data for services (such as web and FTP) offered by this system.


Information about the devices, as seen by the Linux kernel.


Temporary directory that any user can use as a scratch directory, meaning that the contents of this directory are considered unimportant and usually are deleted every time the system boots.


Subdirectories for many important programs, such as the X Window System (in the /usr/X11R6 directory) and the online manual. (Table 4-2 shows some of the standard subdirectories in /usr.)


Various system files (such as logs), as well as directories for holding other information, such as files for the web server and anonymous FTP server.

The /usr and /var directories also contain a number of standard subdirectories. Table 4-2 lists the important subdirectories in /usr. Table 4-3 shows a similar breakdown for the /var directory.

Table 4-2 Important /usr Subdirectories




Executable files for many more Linux commands, including utility programs that are commonly available in Linux but aren’t part of the core Linux operating system.


Some old Linux games.


Header files (filenames ending in .h) for the C and C++ programming languages and the X11 header files in the /usr/include/X11 directory and the Linux kernel header files in the /usr/include/linux directory.


Libraries for C and C++ programming languages and many other libraries, such as database libraries and graphical toolkit libraries.


Local files. The /usr/local/bin directory, for example, is supposed to be the location for any executable program developed on your system.


Many administrative commands, such as commands for e-mail and networking.


Shared data, such as default configuration files and images for many applications. For example, /usr/share/gnome contains various shared files for the GNOME desktop, and /usr/share/doc has the documentation files for many Linux applications (such as the bash shell, the Sawfish window manager, and The GIMP image processing program).


Online manual (which you can read by using the man command).


Source code for the Linux kernel (the core operating system).

Table 4-3 Important /var Subdirectories




Storage area for cached data for applications.


Information relating to the current state of applications.


Locked files to ensure that a resource is used by one application only.


Log files organized into subdirectories. The syslogd server stores its log files in /var/log, with the exact content of the files depending on the syslogd configuration file /etc/syslog.conf. For example, /var/log/messages is the main system log file; /var/log/secure contains log messages from secure services (such as sshd and xinetd); and/var/log/maillog contains the log of mail messages.


User mailbox files.


Variable data for packages stored in /opt directory.


Data describing the system since the time it was booted.


Data that’s waiting for some kind of processing.


Temporary files preserved between system reboots.


Network Information Service (NIS) database files.

Navigating the File System with Linux Commands

Although GUI file managers such as Nautilus (in GNOME) or Konqueror (in KDE) are easy to use, you can use them only if you have a working GUI desktop. Sometimes you may not have a graphical environment to run a graphical file manager. For example, you may be logged in through a text terminal, or X may not be working on your system. In those situations, you have to rely on Linux commands to work with files and directories. You can always use Linux commands, even in the graphical environment — all you have to do is open a terminal window and type the Linux commands.

In this section, I briefly describe some Linux commands for moving around the Linux file system.

Commands for directory navigation

In Linux, when you log in as root, your home directory is /root. For other users, the home directory is usually in the /home directory. My home directory (when I log in as edulaney) is /home/edulaney. This information is stored in the /etc/passwd file. By default, only you have permission to save files in your home directory, and only you can create subdirectories in your home directory to further organize your files.

Linux supports the concept of a current directory, which is the directory on which all file and directory commands operate. After you log in, for example, your current directory is the home directory. To see the current directory, type the pwd command.

To change the current directory, use the cd command. To change the current directory to /usr/lib, type the following:

cd /usr/lib

Then, to change the directory to the cups subdirectory in /usr/lib, type this command:

cd cups

Now, if you use the pwd command, that command shows /usr/lib/cups as the current directory.

These two examples show that you can refer to a directory’s name in two ways:

· Absolute pathname: An example is /usr/lib, which is an exact directory in the directory tree. Think of the absolute pathname as the complete mailing address for a package that the postal service will deliver to your next-door neighbor.

· Relative directory name: An example is cups, which represents the cups subdirectory of the current directory, whatever that may be. Think of the relative directory name as giving the postal carrier directions from your house to the one next door so the carrier can deliver the package.

If I type cd cups in /usr/lib, the current directory changes to /usr/lib/cups. However, if I type the same command in /home/edulaney, the shell tries to change the current directory to /home/edulaney/cups.

tip.eps Use the cd command without any arguments to change the current directory back to your home directory. No matter where you are, typing cd at the shell prompt brings you back home!

technicalstuff.eps The tilde character (~) refers to your home directory. Thus you can change the current directory to your home directory by using the command cd ~. You can refer to another user’s home directory by appending that user’s name to the tilde. Thus cd ~superman changes the current directory to the home directory of superman.

tip.eps Wait, there’s more. A single dot (.) and two dots (. .) — often cleverly referred to as dot-dot — also have special meanings. A single dot (.) indicates the current directory, whereas two dots (. .) indicate the parent directory. For example, if the current directory is/usr/share, you go one level up to /usr by typing the following:

cd . .

Commands for directory listings and permissions

You can get a directory listing by using the ls command. By default, the ls command — without options — displays the contents of the current directory in a compact, multicolumn format. For example, type the next two commands to see the contents of the /etc/X11 directory:

cd /etc/X11

The output looks like this. (On the console, you see some items in different colors.)

X Xsession.d cursors rgb.txt xkb
XF86Config-4 Xsession.options default-display-manager rstart xserver
Xresources Xwrapper.config fonts sysconfig xsm
Xsession app-defaults gdm xinit

From this listing (without the colors), you can’t tell whether an entry is a file or a directory. To tell the directories and files apart, use the -F option with ls, like this:

ls -F

This time the output gives you some more clues about the file types:

X@ Xsession.d/ cursors/ rgb.txt xkb/
XF86Config-4 Xsession.options default-display-manager rstart/ xserver/
Xresources/ Xwrapper.config fonts/ sysconfig/ xsm/
Xsession* app-defaults/ gdm@ xinit/

The output from ls -F shows the directory names with a slash (/) appended to them. Plain filenames appear as is. The at sign (@) appended to a filename (for example, notice the file named X) indicates that this file is a link to another file. (In other words, this filename simply refers to another file; it’s a shortcut.) An asterisk (*) is appended to executable files. (Xsession, for example, is an executable file.) The shell can run any executable file.

You can see even more detailed information about the files and directories with the -l option:

ls -l

For the /etc/X11 directory, a typical output from ls -l looks like the following:

total 84
lrwxrwxrwx 1 root root 20 Jul 15 20:32 X -> /usr/bin/X11/XFree86
-rw-r--r-- 1 root root 2878 Jul 16 14:50 XF86Config-4
drwxr-xr-x 2 root root 4096 Jul 15 20:32 Xresources
-rwxr-xr-x 1 root root 3456 Jun 1 01:59 Xsession
drwxr-xr-x 2 root root 4096 Jul 15 20:34 Xsession.d
-rw-r--r-- 1 root root 217 Jun 1 01:59 Xsession.options
-rw------- 1 root root 771 Jul 15 20:32 Xwrapper.config
drwxr-xr-x 2 root root 4096 Jul 15 20:35 app-defaults
. . . lines deleted . . .

This listing shows considerable information about every directory entry — each of which can be a file or another directory. Looking at a line from the right column to the left, you see that the rightmost column shows the name of the directory entry. The date and time before the name show when the last modifications to that file were made. To the left of the date and time is the size of the file in bytes.

The file’s group and owner appear to the left of the column that shows the file size. The next number to the left indicates the number of links to the file. (A link is like a shortcut in Windows.) Finally, the leftmost column shows the file’s permission settings, which determine who can read, write, or execute the file.

technicalstuff.eps The first letter has a special meaning, as the following list shows:

· l: The file is a symbolic link to another file. In other words, it’s a shortcut to something else.

· d: The file is a directory. It will appear as a folder in a GUI.

· - (hyphen): The file is normal. By normal, I mean that it isn’t a directory, a link, or anything else odd. Most of the items on your system are just normal files.

· b: The file represents a block device, such as a disk drive.

· c: The file represents a character device, such as a serial port or a terminal.

technicalstuff.eps After that first letter, the leftmost column shows a sequence of nine characters, which appear as rwxrwxrwx when each letter is present. Each letter indicates a specific permission. A hyphen (-) in place of a letter indicates no permission for a specific operation on the file. Think of these nine letters as three groups of three letters (rwx), interpreted as follows:

· Leftmost rwx group: Controls the read, write, and execute permission of the file’s owner. In other words, if you see rwx in this position, the file’s owner can read (r), write (w), and execute (x) the file. A hyphen in the place of a letter indicates no permission. Thus the string rw- means the owner has read and write permission but not execute permission. Although executable programs (including shell programs) typically have execute permission, directories treat execute permission as equivalent to use permission: A user must have execute permission on a directory before he or she can open and read the contents of the directory.

· Middle rwx group: Controls the read, write, and execute permission of any user belonging to that file’s group.

· Rightmost rwx group: Controls the read, write, and execute permission of all other users (collectively thought of as the world).

Thus, a file with the permission setting rwx------ is accessible only to the file’s owner, whereas the permission setting rwxr--r-- makes the file readable by the world.

An interesting feature of the ls command is that it doesn’t list any file whose name begins with a period. To see these files, you must use the ls command with the -a option, as follows:

ls -a

Try this command in your home directory and then compare the result with what you see when you don’t use the -a option:

1. Type cd to change to your home directory.

2. Type ls -F to see the files and directories in your home directory.

3. Type ls -aF to see everything, including hidden files.

tip.eps Most Linux commands take single-character options, each with a hyphen as a prefix. When you want to use several options, type a hyphen and concatenate (string together) the option letters, one after another. Thus ls -al is equivalent to ls -a -l as well as ls -l -a.

Commands for changing permissions and ownerships

You may need to change a file’s permission settings to protect it from others. Use the chmod command to change the permission settings of a file or a directory.

To use chmod effectively, you have to specify the permission settings. A good way is to concatenate letters from the columns of Table 4-4 in the order shown (Who/Action/Permission).

Table 4-4 Letter Codes for File Permissions




u (user)

+ (add)

r (read)

g (group)

– (remove)

w (write)

o (others)

= (assign)

x (execute)

a (all)

s (set user ID)

technicalstuff.eps You use only the single character from each column — the text in parentheses is for explanation only.

For example, to give everyone read access to all files in a directory, pick a (for all) from the first column, + (for add) from the second column, and r (for read) from the third column to come up with the permission setting a+r. Then use the set of options with chmod, like this:

chmod a+r *.

On the other hand, to permit everyone to execute one specific file, type

chmod a+x filename

tip.eps Suppose you have a file named mystuff that you want to protect. You can make it accessible to no one but you if you type the following commands, in this order:

chmod a-rwx mystuff
chmod u+rw mystuff

The first command turns off all permissions for everyone, and the second command turns on the read and write permissions for the owner (you). Type ls -l to verify that the change took place. (You see a permission setting of -rw-------.)

Sometimes you have to change a file’s user or group ownership for everything to work correctly. For example, suppose you’re instructed (by a manual, what else?) to create a directory named cups and give it the ownership of user ID lp and group ID sys. How do you do it?

Well, you can log in as root and create the cups directory with the command mkdir:

mkdir cups

If you check the file’s details with the ls -l command, you see that the user and group ownership is root root.

tip.eps To change the owner, use the chown command. For example, to change the ownership of the cups directory to user ID lp and group ID sys, type

chown lp.sys cups

Commands for working with files

To copy files from one directory to another, use the cp command. If you want to copy a file to the current directory but retain the original name, use a period (.) as the second argument of the cp command. Thus the following command copies the Xresources file from the /etc/X11directory to the current directory (denoted by a single period):

cp /etc/X11/Xresources .

The cp command makes a new copy of a file and leaves the original intact.

tip.eps If you want to copy the entire contents of a directory — including all subdirectories and their contents — to another directory, use the command cp -ar sourcedir destdir. (This command copies everything in the sourcedir directory to destdir.) For example, to copy all files from the /etc/X11 directory to the current directory, type the following command:

cp -ar /etc/X11 .

tip.eps To move a file to a new location, use the mv command. The original copy is gone, and a new copy appears at the destination. You can use mv to rename a file. If you want to change the name of today.list to old.list, use the mv command, as follows:

mv today.list old.list

On the other hand, if you want to move the today.list file to a subdirectory named saved, use this command:

mv today.list saved

An interesting feature of mv is that you can use it to move entire directories (with all their subdirectories and files) to a new location. If you have a directory named data that contains many files and subdirectories, you can move that entire directory structure to old_data by using the following command:

mv data old_data

To delete files, use the rm command. For example, to delete a file named old.list, type the following command:

rm old.list

warning.eps Be careful with the rm command — especially when you log in as root. You can inadvertently delete important files with rm.

Commands for working with directories

To organize files in your home directory, you have to create new directories. Use the mkdir command to create a directory. For example, to create a directory named images in the current directory, type the following:

mkdir images

After you create the directory, you can use the cd images command to change to that directory.

tip.eps You can create an entire directory tree by using the -p option with the mkdir command. For example, suppose your system has a /usr/src directory and you want to create the directory tree /usr/src/book/java/examples/applets. To create this directory hierarchy, type the following command:

mkdir -p /usr/src/book/java/examples/applets

When you no longer need a directory, use the rmdir command to delete it.

remember.eps You can delete a directory only when the directory is empty.

To remove an empty directory tree, you can use the -p option, like this:

rmdir -p /usr/src/book/java/examples/applets

This command removes the empty parent directories of applets. The command stops when it encounters a directory that’s not empty.

Commands for finding files

The find command is useful for locating files (and directories) that meet your search criteria.

When I began using Unix many years ago (Berkeley Unix in the early 1980s), I was confounded by the find command. I stayed with one basic syntax of find for a long time before graduating to more complex forms. The basic syntax that I discovered first was for finding a file anywhere in the file system. Here’s how it goes: Suppose you want to find any file or directory with a name that starts with gnome. Type the following find command to find these files:

find / -name "gnome*" -print

If you’re not logged in as root, you may get a bunch of error messages. If these error messages annoy you, just modify the command as follows and the error messages are history. (Or, as Unix aficionados say, “Send ’em to the bit bucket.”)

find / -name "gnome*" -print 2> /dev/null

This command tells find to start looking at the root directory (/) to look for filenames that match gnome* and to display the full pathname of any matching file. The last part (2> /dev/null) simply sends the error messages to a special file that’s the equivalent of simply ignoring them.

You can use variations of this simple form of find to locate a file in any directory (as well as any subdirectories contained in the directory). If you forget where in your home directory you’ve stored all files named report* (names that start with report), you can search for the files by using the following command:

find ~ -name "report*" -print

When you become comfortable with this syntax of find, you can use other options of find. For example, to find only specific types of files (such as directories), use the type option. The following command displays all top-level directory names in your Linux system:

find / -type d -maxdepth 1 -print

You probably don’t have to use the complex forms of find in a typical Linux system. But if you ever need to, you can look up the rest of the find options by using the following command:

man find

tip.eps An easy way to find all files that match a name is to use the locate command, which searches a periodically updated database of files on your system. For example, here’s a typical output I get when I type locate Xresources on a Debian system (the output may differ based on your distribution):


 width= The locate command isn’t installed by default in some Linux distributions. To install it, open the Add/Remove Software application for your distribution and search for locate. Then select the package from the search results and click Accept to install it.

Commands for mounting and unmounting

Suppose you want to access the files on this book’s companion DVD-ROM when you’re logged in at a text console (with no GUI to help you). To do so, you have to first mount the DVD-ROM drive’s file system on a specific directory in the Linux file system.

 width= Start by looking at the /etc/fstab file for clues to the name of the CD-ROM device. For example, some Linux distributions use the device name /dev/cdrom to refer to CD/DVD-ROM drives, whereas others may use device names such as/dev/hdc,/dev/cdroms/cdrom0, or /dev/cdrecorder (for a DVD/CD-R drive). The entry in /etc/fstab file also tells you the directory where that distribution expects the CD/DVD to be mounted. Some distributions use /media/cdrom as the mount point, whereas others use /media/cdrom0, or /media/cdrecorder.

tip.eps It is customary to use the cdrom term to mean both CD-ROM and DVD-ROM.

Log in as root (or type su - to become root), insert the DVD-ROM in the DVD drive, and then type the following command:

mount /dev/hdc /media/cdrom0

This command mounts the file system on the device named /dev/hdc (an IDE DVD/CD-ROM drive) on the /media/cdrom0 directory (which is also called the mount point) in the Linux file system.

After the mount command successfully completes its task, you can access the files on the DVD-ROM by referring to the /media/cdrom0 directory as the top-level directory of the disc. In other words, to see the contents of the DVD-ROM, type

ls -F /media/cdrom0

When you’ve finished using the DVD-ROM — and before you eject it from the drive — you have to unmount the disc drive with the following umount command:

umount /dev/hdc

remember.eps You can mount devices on any empty directory on the file system. However, each distribution has customary locations with directories meant for mounting devices. For example, some distributions use directories in /mnt whereas others use the /media directory for the mount points.

Commands for checking disk-space usage

You can use two simple commands — df and du — to check the disk-space usage on your system. The df command shows you a summary of disk-space usage for all mounted devices. For example, when I type df on a PC with many mounted storage devices, here’s what I get as output:

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hdb6 28249372 2377292 25872080 9% /
tmpfs 383968 12 383956 1% /dev/shm
/dev/hda5 5766924 1422232 4051744 26% /ubuntu/boot
/dev/hda7 6258100 2989200 2951004 51% /debian/boot
/dev/hda9 5766924 1422232 4051744 26% /ubuntu
/dev/hda10 5766924 1872992 3600984 35% /mepis
/dev/hda11 6258100 2989200 2951004 51% /debian
/dev/hdb3 19558500 1370172 18188328 8% /xandros
/dev/hda2 16087676 10593364 5494312 66% /windows/C
/dev/hdb1 107426620 9613028 97813592 9% /windows/D

This table lists the device, the total kilobytes of storage, how much of that memory is in use, how much is available, the percentage being used, and the mount point.

tip.eps To see the output of df in a more readable format, type df -h. The output follows:

Filesystem Size Used Avail Use% Mounted on
/dev/hdb6 27G 2.3G 25G 9% /
tmpfs 375M 12K 375M 1% /dev/shm
/dev/hda5 5.5G 1.4G 3.9G 26% /ubuntu/boot
/dev/hda7 6.0G 2.9G 2.9G 51% /debian/boot
/dev/hda9 5.5G 1.4G 3.9G 26% /ubuntu
/dev/hda10 5.5G 1.8G 3.5G 35% /mepis
/dev/hda11 6.0G 2.9G 2.9G 51% /debian
/dev/hdb3 19G 1.4G 18G 8% /xandros
/dev/hda2 16G 11G 5.3G 66% /windows/C
/dev/hdb1 103G 9.2G 94G 9% /windows/D

If you compare this output with the output of the plain df (the preceding listing), you see that df -h prints the sizes with terms like M for megabytes and G for gigabytes. These are easier to understand than 1K-blocks.

The other command, du, is useful for finding out how much space a directory takes up. For example, type du /etc/X11 to view the contents of all the directories in the /etc/X11 directory. (This directory contains X Window System configuration files.) You see the following:

12 /etc/X11/Xresources
36 /etc/X11/Xsession.d
272 /etc/X11/app-defaults
20 /etc/X11/cursors
12 /etc/X11/xinit
. . . lines deleted . . .
12 /etc/X11/fonts/misc
8 /etc/X11/fonts/100dpi
8 /etc/X11/fonts/75dpi
8 /etc/X11/fonts/Speedo
8 /etc/X11/fonts/Type1
48 /etc/X11/fonts
2896 /etc/X11

Each directory name is preceded by a number — which tells you the number of kilobytes of disk space used by that directory. Thus the /etc/X11 directory uses 2896KB (or about 2.9MB) of disk space. If you simply want the total disk space used by a directory (including all the files and subdirectories contained in that directory), use the -s option, as follows:

du -s /etc/X11
2896 /etc/X11

The -s option causes du to print just the summary information for the entire directory.

tip.eps Just as df -h prints the disk-space information in megabytes and gigabytes, you can use the du -h command to view the output of du in more readable form. For example, here’s how I combine it with the -s option to see the space that I’m using in my home directory (/home/edulaney):

du -sh /home/edulaney
645M /home/edulaney