Managing The Linux File System
Managing The Linux File System
system
Managing the Linux file system
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
touch new_file
Creating Files and Directories
mkdir new_directory
Viewing Text File Contents
cat
less
head
tail
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.
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.
head
The head filename command is used to
display the first couple of lines of a text file on
the screen.
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/schedule.txt to your home
directory, you could enter
cp /tmp/schedule.txt ~.
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 mylog.txt from /tmp to /var/log, you
would enter mv /tmp/mylog.txt /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 schedule.txt to
schedule.old, you would enter
mv schedule.txt schedule.old.
Creating Links
Hard
Symbolic
Hard
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
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
Running Executables
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 snmpd.conf,
you could enter locate snmpd.conf
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
grep
options:
–i Ignores case when searching for the
search text.
–l Doesn’t display the actual matching line of
/media/cdrom
/media/cdrecorder
/media/dvd
On distributions such as Red Hat or Fedora, your
directories for mounting optical
devices are located in /mnt.
To mount a CD on a Linux system, insert it in your
CD drive. Change to your root account and then
enter mount –t iso9660 device mount_point. For
example, mount –t iso9660 /dev/hdc /media/cdrom
As with any mounted file system, you should use
umount to unmount a CD or DVD before ejecting it.
• Working with USB and FireWire
Devices
Using tar
Using cpio
Using tar
The tar utility has been around for a very long
time and is a very commonly used Linux
backup tool. The acronym “tar” stands for tape
archive. The tar utility takes a list of specified
files and copies them into a single archive file
(.tar). The .tar file can then be compressed
with the gzip utility on your Linux system,
resulting in a file with a .tar.gz extension.
tar
Syntax: tar –cvf filename directory
The –c option tells tar to create a new archive.
The –v option tells tar to work in verbose
mode, displaying each file being backed up on
screen.
The –f option specifies the name of the tar
archive to be created.
Ex: tar –cvf /media/usb/backup.tar /home
To restore a tar archive, simply enter
tar –xvf filename
ex: tar –xvf /media/usb/backup.tar
If the archive has been zipped, you can also
use the –z option to unzip the archive before
extracting it
Using cpio
The cpio utility can also be used to make archive
files just like tar. A key difference between tar and
cpio is the fact that you must provide cpio with a
list of files and directories to back up from the
standard input
Syntax: cpio -ov file,directory > file.cpio
The –o option tells cpio to create a new archive.
The –v option simply tells cpio to run verbosely,
displaying the name of each file and directory as
it’s processed
we could extract the archive we just created
by entering cpio –iv < file.cpio