You are on page 1of 29

EXPLORING LINUX

FILESYSTEMS
Linux Directory Structure
◦ Fundamental to using the Linux operating system is an understanding of how
Linux stores files on the file system. Typical Linux systems could have thousands
of data and program files; thus, a structure that organizes those files is necessary
to make it easier to find and manipulate data and run programs.
◦ Reminder of the previous chapter that Linux uses a logical directory tree to
organize files into directories (aka Binder).
◦ When a user saves files to a specific directory, the files are physically saved in
the file system of a specific partition on a hard drive or SSD in the computer.
◦ Each file system on a hard disk partition is referenced by a drive letter (such as C:
or D:) and has a root (indicated by the \ character) that contains Subdirectories
that together form a hierarchical tree.
Linux Directory Structure
Linux Directory Structure
◦ Linux uses a similar directory structure, but with no drive letters.
◦ Structure contains a single root (denoted by the character /), with several active
file systems mounted (or mounted) hard disk partitions in different folders in this
folder tree.
◦ The folder in which each file system is mounted is transparent to the user. An
example of a Linux directory structure similar to the example Windows directory
the shaft. Notice that the subfolder named “Root” differs from the root (/)
directory.
Linux Directory Structure
Linux Directory Structure
◦ In the above figure, the absolute path name for the color folder is / windows /
color and the absolute path name for the sue directory is / home / sue.
◦ In other words, you are referring to in the / windows / color folder as a color
folder in the windows folder under the root of the system (the / sign). Similarly,
refer to / home / sue directory as the sue directory in the home directory at the
root of the system.
Changing directories
◦ When you log in to a Linux system, you are in your home directory, i.e. a location unique to
your user account to store personal files.
◦ Normal users usually have a home directory named after their user account in the /home
directory like in /home/sue.
◦ However, the root user has a home directory named root under root System directory
(/root).
◦ Whatever your username, you can always refer to your own home directory using the ~
metacharacter.
◦ To confirm which system directory you are in, just observe the name at the bottom of the
shell prompt or run the pwd (print working directory) command at a command line
prompt. If you are logged in as root user, the following output is displayed on the terminal
screen:
Changing directories
◦ [root@server1 ~]# pwd
◦ /root
◦ [root@server1 ~]#_

However, if you are logged in as the user sue, you see the following output:
◦ [sue@server1 ~]$ pwd
◦ /home/sue
◦ [sue@server1 ~]$_
Changing directories
◦ To change directories, you can issue the cd (change directory) command with an argument specifying the
destination directory. If you do not specify a destination directory, the cd command returns you to your
home directory:
◦ [root@server1 ~]# cd /home/mary
◦ [root@server1 mary]# pwd
◦ /home/mary
◦ [root@server1 mary]# cd /etc
◦ [root@server1 etc]# pwd
◦ /etc
◦ [root@server1 etc]# cd
◦ [root@server1 ~]# pwd
◦ /root
◦ [root@server1 ~]#_
Changing directories
◦ You can also use the ~ metacharacter to refer to another user’s home directory by appending a username at
the end:
◦ [root@server1 ~]# cd ~mary
◦ [root@server1 mary]# pwd
◦ /home/mary
◦ [root@server1 mary]# cd ~
◦ [root@server1 ~]# pwd
◦ /root
◦ [root@server1 ~]#
Changing directories
◦ In many of the examples discussed above, the argument is specified after the cd
command is an absolute path to a directory, which means the system has all
information needed to find the destination folder because the path name begins
from the root (/) of the system.
◦ However, in most Linux commands you can also do this a relative path instead of an
absolute path to reduce typing.
◦ A member of the family pathname is the path name of a destination file or folder
relative to the current folder in the tree.
◦ To specify a folder in the current folder, refer to that folder by name (do not start the
path name with a / character)
◦ Pointing a step towards a directory closer to the root of the tree (also known as the
mother map), use a colon (..). An example of using relative path names to navigate
the directory tree is shown below:
Changing directories
◦ [root@server1 ~]# cd /home/mary
◦ [root@server1 mary]# pwd
◦ /home/mary
◦ [root@server1 mary]# cd ..
◦ [root@server1 home]# pwd
◦ /home
◦ [root@server1 home]# cd mary
◦ [root@server1 mary]# pwd
◦ /home/mary
◦ [root@server1 mary]#_
Changing directories
◦ The preceding example used “..” to move up one parent directory and then used the
word “mary” to specify the mary subdirectory relative to the current location in the
tree; however, you can also move more than one level up or down the directory tree:
◦ [root@server1 ~]# cd /home/mary
◦ [root@server1 mary]# pwd
◦ /home/mary
◦ [root@server1 mary]# cd ../..
◦ [root@server1 /]# pwd/
◦ [root@server1 /]# cd home/mary
◦ [root@server1 mary]# pwd
◦ /home/mary
◦ [root@server1 mary]#_
Changing directories
• Although absolute pathnames can easily be used as arguments to Relative
commands when specifying the location of a specific file or directory, Path
names can save you a lot of typing and reduce the risk of error when the current
directory is remote from the root directory.
• Suppose, for example, that the current directory is
/home/sue/projects/acme/plans and you need to change it
/home/sue/projects/acme directory. If you are using an absolute path name,
type cd / home / sue / projects / acme; however, if you are using a relative path
name, you just need to type cd .. to do the same task because the directory
/home / sue / projects / acme is a parent directory above the current position in
the directory tree.
Changing directories
◦ If there is more than one possible match, the completion of the card warns you with
an acoustic signal; pressing the Tab key again after this beep will give you a list of
possible files or folders. To use the tab completion feature, change the current
directory to /home/sue, type cd /h, then press Tab.
◦ This will change the previous characters on the terminal screen to include cd /home/
(The BASH shell was able to enter the correct information because the /home
directory is the only directory under the / directory that starts with the letter "h").
◦ So you could add an s character to the command to make the command line appear
cd /home/s and press Tab again to allow the shell to populate the file remaining
letters. This will bring up the command cd /home/sue/ on the terminal screen (the
sue directory is the only directory that comes with the s under the /home directory).
At this point you can press Enter to run the command and change the current
directory to /home/sue.
Viewing File Directories
• The point of a directory structure is to organize files into an easy-to-use format. In order to locate
the file, you need to execute, view, or edit, you need to be able to display a list of the contents of a
particular directory.

File Types
• A Linux system can have several types of files; the most common include the following:
• Text files
• Binary data files
• Executable program files
• Directory files
• Linked files
• Special device files
• Named pipes and sockets
Viewing File Directories
◦ Most file-locations on a Linux system that contain configuration information are text file-locations. Another
type of file-location is a program that exists on disk.
◦ Another type of file is a program that exists on the hard drive before it is executed in memory to become
processes. A program is typically associated with several supporting binary data files that store information
such as common functions and graphics.
◦ A program is typically associated with several supporting binary data files that store information such as
common functions and graphics.
◦ Linked files are files that have an association with one another; they can represent the same data, or they can
point to another file (also known as a shortcut file).
◦ Special device files represent different devices on the system, such as hard disks and serial ports.
◦ Named pipe files identify channels that pass information from one process in memory to another, and in
some cases, they can be mediated by files on the hard drive.
◦ Socket files allows a process on another computer to write to a file on the local computer while another
process reads from that file.
Filenames
◦ Files are recognized by their filenames, which can include up to 255 characters,
yet are rarely longer than 20 characters on most Linux systems.
◦ Filenames used by the Windows operating system typically end with a period and
three characters that identify the file type—for example, document.txt (a text file)
and server.exe (an executable program file).
◦ However, most files on the hard drive of a Linux system do not follow this
pattern, although some files on the Linux filesystem do contain characters at the
end of the filename that indicate the file type.
◦ These characters are commonly referred to as filename extensions.
Filenames
Listing Files
◦ Linux hosts a variety of commands that can be used to display files and their types in various directories on
hard drive partitions. By far, the most common method for displaying files is to use the ls command.

◦ The ls command displays all the files in the current directory in columnar format; however, you can also
pass an argument to the ls command indicating the directory to list if the current directory listing is not
required.
Listing Files
◦ Recall from the previous chapter that you can use switches to alter the behavior of commands. To view a list
of files and their type, use the –F switch to the ls command:

◦ The ls –F command appends a special character at the end of each filename displayed to indicate the type of
file. In the preceding output, note that the filenames current, Desktop, myprogram, and myscript have special
characters appended to their names.
◦ The @ symbol indicates a linked file, the * symbol indicates an executable file, the / indicates a
subdirectory, the 5 character indicates a socket, and the | character indicates a named pipe.
Listing Files
◦ Although the ls –F command is a quick way of getting file type information in an easy-to-read format, at
times you need to obtain more detailed information about each file. The ls –l command can be used to
provide a long listing for each file in a certain directory.
Listing Files
Each file listed in the preceding example has eight components of information listed in columns from left to
right:
1 A file type character:
◦ The d character represents a directory.
◦ The l character represents a symbolically linked file
◦ The b or c characters represent special device files
◦ The n character represents a named pipe.
◦ The s character represents a socket.
◦ The – character represents all other file types (text files, binary data files).
2 A list of permissions on the file (also called the mode of the file).
3 A hard link count
4 The owner of the file
5 The group owner of the file
6 The file size.
7 The most recent modification time of the file (or creation time if the file wasnot modified following creation)
Listing Files
8 The filename. Some files are shortcuts or pointers to other files and indicated with an arrow ->, as with the
file called “current” in the preceding output; these are known as symbolic links.
For the file named "Project" in the previous example, you can see that this file is a normal file because its long
list starts with a - character, permissions are enabled file is rw-r--r--, hard link count is 1, file owner is root user
who The group owning the file is the root group, the file size is 71 bytes, and the file has been last
modification on April 7 at 9:58 a.m.
◦ The ls –F and ls –l commands are valuable to a user who wants to display file types; however, neither of
these commands can display all file types using special characters. To display the file type of any file, you
can use the file command; you give the file command an argument specifying what file to analyze. You can
also pass multiple files as arguments or use the * metacharacter to refer to all files in the current directory.
An example of using the file command in the root user’s home directory is as follows:
Listing Files
Listing Files
Listing Files
◦ Recall that filenames for hidden files start with a period character ( . ). To view them, pass the –a option
to the ls command. Some hidden files that are commonly seen in the root user’s home directory are
shown next:
Listing Files
◦ As discussed earlier, the ( . ) character refers to the current working directory and the ( .. ) character
refers to the parent directory relative to your current location in the directory tree.
◦ Each of these pointers is seen as a special (or fictitious) file when using the ls –a command, as each starts
with a period.
◦ You can also specify several options simultaneously for most commands on the command line and
receive the combined functionality of all the options. For example, to view all hidden files and their file
types, you could type:
Listing Files
◦ The options to the ls command ( –l,
–F, –a ) are the most common
options you would use when
navigating the Linux directory tree;
however, many options are
available in the ls command that
alter the listing of files on the
filesystem. Table 3-2 lists the most
common of these options and their
descriptions.

You might also like