Introduction to Linux
Managing the Linux file
system
1. Objectives
– Describe the Linux file system
– Navigate through the File System
– Complete common file system tasks
– File System Security
– Manage disk partitions
– Use removable media
– Back up data
The Linux file system
1. Describe the Linux file
system
- The Role of the Linux File System
- The Hierarchical Structure of the Linux File
System
-
- Types of Files Used by Linux
• The Role of the Linux File
System
- Means by which data is organized and can be
easily located
- The data can be easily retrieved at any later
point in time
-Preserves the integrity of the data
• The Hierarchical Structure
of the Linux File System
• Linux File System
• Linux File System
Notice that the topmost directory in the
structure is the / directory, also called the
root directory.
Beneath the root directory are a series of
sub-directories.
Specifications for how these directories are to
be named are contained in the File system
Hierarchy Standard (FHS)
/bin
This directory contains executable files
necessary to manage and run the Linux
system, including shells (such as bash) and
file system management utilities such as cp
and rm.
List down any 10 of these executable s
/boot
This directory contains your boot loader files,
which are required to boot your system.
List any five of these files
/dev
This directory contains special files that are
used to represent the various hardware
devices installed in the system
EX: hda, hdb ...
/etc
This directory contains text-based
configuration files used by the system as well
as services running on the system.
You can edit these files with a text editor to
customize how Linux behaves
/home
This directory contains subdirectories that
serve as home directories for each user
account on your Linux system.
It is the place where all user work is saved
/lib
This directory contains code libraries used by
programs in /bin and /sbin.
Your kernel modules are also stored in the
modules subdirectory of /lib.
List any five of the code libraries.
/media
This directory is used by some Linux
distributions (such as SUSE Linux) to mount
external devices, including CD drives, DVD
drives, and floppy drives.
/mnt
This directory is used by some Linux
distributions (such as Fedora or Red Hat) to
mount external devices, including CD drives,
DVD drives, and floppy drives
/opt
This directory contains files for some programs
you install on the system
/proc
This directory is a little different from the other
directories in this list.
/proc doesn’t actually exist in the file system
Within /proc are a number of different
subdirectories,Notice that each of these
subdirectories is identified with a number, not
a name. These numbers correspond to the
process ID (PID) number of theassociated
process running on the system
/root
This directory is the root user’s home
directory. Notice that it is located separately
from the home directories for other users in
/home.
/sbin
This directory contains important system
management and administration files, such as
fdisk, fsck, ifconfig, init, mkfs, shutdown, and
halt.
/srv
This directory contains subdirectories where
services running on the system (such as httpd
and ftpd) save their files.
/sys
This directory contains information about the
hardware in your system.
/tmp
This directory contains temporary files created
by you or by the system.
/usr
This directory contains application files. In fact,
most of the application files used on your
system are saved in a subdirectory of /usr
/var
This directory contains a variety of variable
data, including your system log files
• Types of Files Used by
Linux
With a Windows file system you basically
have two entry types in the file system:
Directories & Files. You can have normal
files, hidden files, shortcut files, word
processing files, executable files, and so on.
With Linux, however, there are a variety of
different file types used by the file system.
Linux File Types
Regular files: These files are similar to those
used by the file systems of other operating
systems - forexample, executable files,
[Link] files, images, text
configuration files, etc.
Directories: Files used to contain pointers to
other files
Links : These files are pointers that point to
other files in the file system.
Linux File Types
FIFOs: FIFO stands for First In First Out. These
are special files used to move data from one
running process on the system to another. A FIFO
file is basically a queue where the first chunk of
data added to the queue is the first chunk of data
removed from the queue. Data can only move in
one direction through a FIFO.
Sockets: Sockets are similar to FIFOs in that they
are used to transfer information between
sockets( Networks). With a socket, however, data
can move bi-directionally.
2. Complete Common File
System Tasks
Navigating the file system
Managing files and directories
Running executable files
Searching the file system
With Linux, however, you should become
proficient with the shell commands first
and then use the GUI utilities for
convenience
Reasons?
Most employers and co-workers won’t take
you seriously as a Linux administrator if you
can’t use the shell prompt. It just goes with the
territory.
Many Linux systems, especially those
deployed as servers, don’t run X Windows.
Supporting a GUI environment requires a lot
of CPU overhead.
Many server admins prefer to devote those
CPU cycles to system services instead of
moving the mouse cursor on the screen.
In this situation, you need to know how to do
things from the shell prompt.
You need to know how to complete these
tasks from the shell prompt to pass your
Linux+ exam.
• Navigating the File System
pwd (Present Working Directory)
cd
ls
The cd Command
If you enter cd at the shell prompt without
specifying a path, it will automatically change
directories to the home directory of the
currently logged-in user.
The ls Command
–a Displays all files, including hidden files.
–l Displays a long listing of the directory
contents. This is a very useful option. You can
use it to see the file names, ownership,
permissions, modification date, and size.
–R Displays directory contents recursively;
that is, it displays the contents of the current
directory as well as the contents of all
subdirectories.
Managing files and directories
Creating files and directories
Viewing file contents
Deleting files and directories
Copying and moving files and directories
Creating links
Creating Files and
Directories
touch new_file
gedit new_file
vi new_file
echo ....... > new_file
mkdir new_directory
Viewing Text File Contents
Commands to use to view File content
– cat
– less
– head
– Tail
– sort
– more
cat
The cat filename command will display the
specified text file on screen.
This command doesn’t pause the output, so if
you use it to view a long file, you may need to
append |more to the command to pause the
output a page a time.
cat options-
less
The less filename command can also be used
to display the specified text file on screen,
much like cat.
However, the less command automatically
pauses a long text file one page at time.
cat options-: Many Options
head
The head filename command is used to
display the first couple of lines of a text file on
the screen.
head options
tail
The tail filename command is used to display
the last couple of lines of a text file on screen.
The tail command is particularly useful when
displaying a log file on screen.
When viewing a log file, you probably only
want to see the end of the file.
The tail command also includes the –f option,
which is very useful. You can use this to
monitor the file specified in the command
Deleting Files and
Directories
rmdir
rm
rmdir
This utility can be used to delete an existing
directory. To use it, simply enter rmdir
directory_name—for example,
rmdir MyFiles. Be aware, however, that rmdir
requires that the directory be empty before it
will delete it.
rm
The rm utility is a more powerful deletion utility
that can be used to delete either a file or a
populated directory. To delete a file, simply
enter rm filename. To delete a directory, enter
rm –r directory_name.
rm
Be careful with rm! By default, it won’t prompt
you to confirm a deletion operation. It assumes
that you really meant to delete the file or
directory. If you want rm to prompt you before
deleting a file or directory, include the –i
option.
Copying and Moving Files
and Directories
cp
mv
cp
This utility is used to copy files or entire
directory structures from one location in the file
system to another. For example, to copy a file
named /tmp/[Link] to your home
directory, you could enter
cp /tmp/[Link] ~.
mv
The mv command is used much like cp.
However, it will copy the specified file to the
new location in the file system and then delete
the original. For example, to move a file
named [Link] from /tmp to /var/log, you
would enter mv /tmp/[Link] /var/log
mv
The mv command is also used to rename files.
Simply enter mv followed by the file to be
renamed and then the new file name. For
example, to rename [Link] to
[Link], you would enter
mv [Link] [Link].
Creating Links
Two types of Links
Hard
Symbolic
Hard Link
A hard link is a file that points directly to the
inode of another file.
An inode stores basic information about a file
in the Linux file system, including its size,
device, owner, and permissions.
Because the two files use the same inode, you
can’t tell which file is the pointer and which is
the pointee after the hard link is created.
Symbolic Link
A symbolic link file also points to another file in
the file system.
However, a file that is a symbolic link has its
own inode.
Because the pointer file has its own inode, the
pointer and the pointee in the file system can
be easily identified.
For example, in the previous chapter, you saw
that the vi file is symbolic-linked to the vim file.
syntax
ln pointee_ file pointer_file
Using ln without any options creates a hard
link
If you want to create a symbolic link, you use
the –s option
ln pointee_ file pointer_file
Running Executables
For example, you’ve used the man, vi, and cp
programs to accomplish various tasks on your
system.
These programs are executable files that exist in
the Linux file system and are loaded into memory
when you call them from the shell prompt
which command
Searching the File System
Using find
Using locate
Using grep
Using find
The find utility is fantastic tool that you can use
to search the Linux file system.
To use find, enter at the shell prompt
find path –name “filename”
For example, suppose you wanted to find all of
the log files stored in your file system that
have a .log extension. You could enter
find / –name “*.log”
The find utility is flexible. You can also use the
–user “username” option to search for files
owned by a specific user, or use the
–size “size” option to search for files of a
specified size.
You can use a + sign before the size value to
search for files larger than the specified size,
or a – sign before the size value to search for
files smaller than the specified size. The find
utility has many other options
Using locate
The locate utility functions in much the same
manner as find.
However, it has one distinct advantage over
find.
Whenever you execute a search with find, it
manually walks through each directory in the
path you name in the command looking for the
specified files.
This process can take some time.
locate
Alternatively, the locate utility builds an index
of the files in the file system.
Then, when you execute a search, locate
simply runs a query of the index.
It doesn’t actually search the file system
directly.
The result is that locate runs much faster than
find in most situations.
To use locate, you must first install the
findutils-locate package on your system.
With the index updated, you can search for
files by simply entering:
locate filename at the shell prompt.
For example, if you wanted to search for a file
named [Link], you could enter:
locate [Link]
Using grep
Linux also provides a utility called grep that you
can use to search for content within a file. Using
grep, you can search through a file for a
particular text string. To use grep, you would
enter grep search_text file. For example, let’s
suppose you want to want to search through
your /var/log/messages file for any log entries
related to the VNC service running on your
Linux system. You would enter
grep vnc /var/log/messages
root
Data
etc bin
var usr
home
Jane Walter
James Peter Joyce Wayne
January Feb Mar April
Expenses [Link]
Food
Food Education Transport
[Link] [Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
Exercise
Use the diagram Above to answer the question that
follows. All the rectangles represent directories and the
text plain files
– List other other top level directories that are note
indicated.
– One directory is never show on the directory tree.
Name it and discuss its role
– How many user are in the system
– How many top level directories has James created
– How many sub directories
– Who created all the users
– What happens when user james log into the
Exercise
Use the diagram Above to answer the question
that follows.
– Write the set of full commands User James
would have used to create the directories and
file as they are in his home directory.
– User James is scheduled to go for a one
month leave. It is decided that during this
period all James work be accessible to Jane
who can be able to use and even modify
content. What is simplest way to give Jane
this ability and by who?