Linux Filesystem Management

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

LINUX FILE SYSTEM

MANAGEMENT
The filesystem hierarchy system
◦ To simplify the task of finding specific files, the Filesystem Hierarchy Standard (FHS) was created.
◦ FHS defines a standard set of directories for use by all Linux and UNIX systems as well as the file and
subdirectory contents of each directory.
◦ This ensures that, because the filename and location follow a standard convention, a Fedora Linux user
will find the correct configuration file on an openSUSE Linux or Hewlett-Packard UNIX computer with
little difficulty.
◦ The FHS also gives Linux software developers the ability to locate files on a Linux system regardless of
the distribution, allowing them to create software that is not distribution-specific.
The filesystem hierarchy system
The filesystem hierarchy system
Managing files and directories
◦ As mentioned earlier, using a Linux system involves navigating several directories and manipulating the
files inside them.
◦ Thus, an efficient Linux user must understand how to create directories as needed, copy or move files
from one directory to another, and delete files and directories.
◦ These tasks are commonly referred to as file management tasks. Following is an example of a directory
listing displayed by a user who is logged in as the root user:
Managing files and directories
◦ As shown in the preceding output, two executable files (myprogram and myscript), and several project-
related files (project*) exist on this example system.
◦ Because several project files are in the root user’s home directory in the preceding output, you could
create a subdirectory called proj_files to contain the project-related files and decrease the size of the
directory listing.
◦ To do this, you use the mkdir (make directory) command, which takes arguments specifying the
absolute or relative pathnames of the directories to create.
◦ To create a proj_files directory under the current directory, you can use the mkdir command with a
relative pathname:
Managing files and directories
◦ Now, you can move the project files into the proj_files subdirectory by using the mv (move) command.
The mv command requires two arguments at minimum: the source file/directory and the target
file/directory. For example, to move the /etc/sample1 file to the /root directory, you could use the
command mv /etc/sample1 /root.
◦ Note that both the source (or sources) and the destination can be absolute or relative pathnames and the
source can contain wildcards if several files are to be moved.
◦ For example, to move all of the project files to the proj_files directory, you could type mv with the
source argument project* (to match all files starting with the letters “project”) and the target argument
proj_files (relative pathname to the destination directory), as shown in the following output:
Managing files and directories
◦ In the preceding output, the current directory listing does not show the project files anymore, yet the
listing of the proj_files subdirectory indicates that they were moved successfully.
◦ Another important use of the mv command is to rename files, which is simply moving a file to the same
directory but with a different filename.
◦ To rename the myscript file from earlier examples to myscript2, you can use the following mv command:
Managing files and directories
◦ The first argument specifies the source file/directory to be copied and the second argument specifies the
target file/directory.
◦ If several files need to be copied to a destination directory, specify several source arguments, with the
final argument on the command line serving as the target directory.
◦ Each argument can be an absolute or relative pathname and can contain wildcards or the special
metacharacters “.” (which specifies the current directory) and “..” (which specifies the parent directory).
Managing files and directories
◦ You can also make copies of files in the same directory. To make a copy of the hosts file called hosts2 in
the current directory and view the results, type the following commands:

◦ Despite their similarities, the mv and cp commands work on directories differently.


◦ The mv command renames a directory, whereas the cp command creates a whole new copy of the
directory and its contents.
Managing files and directories
◦ To copy a directory full of files in Linux, you must tell the cp command that the copy will be recursive
(involve files and subdirectories too) by using the –r option.
◦ The following example demonstrates copying the proj_files directory and all of its contents to the
/home/user1 directory without and with the –r option:
Managing files and directories
◦ When you type mv, you are actually
running the mv command with the –i
option without realizing it.
◦ If the target file already exists, both the mv
command and the mv command with the –
i option interactively prompt the user to
choose whether to overwrite the existing
file.
◦ Similarly, when you type the cp command,
the cp –i command is actually run to
prevent the accidental overwriting of files.
◦ To see the aliases in your current shell,
type alias, as shown in the following
output:
Managing files and directories
◦ If you want to override this interactive option, which is known as interactive mode, use the –f (force)
option to override the choice, as shown in the following example.
◦ In this example, the root user tries to rename the hosts file using the name “hosts2,” a name already
assigned to an existing file.
◦ The example shows the user attempting this task both without and with the –f option to the mv
command:
Managing files and directories
◦ To remove files or directories, you must use either the rm command or the rmdir command.
◦ The rm (remove) command takes a list of arguments specifying the absolute or relative pathnames of
files to remove.
◦ As with most commands, wildcards can be used to simplify the process of removing multiple files.
◦ After a file has been removed from the filesystem, it cannot be recovered.
◦ As a result, the rm command is aliased in Fedora Linux to the rm command with the –i option, which
interactively prompts the user to choose whether to continue with the deletion.
◦ Like the cp and mv commands, the rm command accepts the –f option to override this choice and
immediately delete the file.
◦ An example demonstrating the use of the rm and rm –f commands to remove the current and hosts2 files
is shown here:
Managing files and directories
Managing files and directories
◦ To remove a directory, you can use the rmdir (remove directory) command; however, the rmdir
command only removes a directory if it contains no files.
◦ To remove a directory and the files inside, you must use the rm command and specify that a directory full
of files should be removed.
◦ As explained earlier in this chapter, you need to use the recursive option (–r) with the cp command to
copy directories; to remove a directory full of files, you can also use a recursive option (–r) with the rm
command.
◦ If, for example, the root user wants to remove the proj_files subdirectory and all of the files within it
without being prompted to confirm each file deletion, they could use the command rm –rf proj_files, as
shown in the following example:
Managing files and directories
Managing files and directories
◦ The aforementioned file management commands are commonly used by Linux users, developers, and
administrators alike. Table 4-2 shows a summary of these common file management commands.

You might also like