Red Hat Certified System Administration 8



Chapter 03: Essential File Management Tools

Goal

In this chapter, you learn how to work with tools to manage the Linux file system. This is an important skill because everything on Linux is very file system oriented.


Sommaire

  • Working with the File System Hierarchy
  • Managing Files
  • Absolute and relative Paths
  • Using Links
  • Working with Archives and Compressed Files


Working with the File System Hierarchy


File System Organization

Directory Structure

On a Windows system, the top level of the directory structure is called My Computer. Physical devices, such as hard drives, USB drives, network drives, show up under My Computer and are each assigned a drive letter, such as C: or D:. The directory structures shown below are provided as examples only. These directories may not be present within the virtual machine environment of this course.

A visual representation of a Windows directory structure:

Like Windows, the Linux directory structure, typically called a filesystem, also has a top level. However instead of My Computer, it is called the root directory, and it is symbolized by the slash / character. Additionally, there are no drives in Linux; each physical device is accessible under a directory, as opposed to a drive letter.

The following image shows a visual representation of a typical Linux filesystem:

/ Root Directory

/boot/ directory contains static files required to boot the system, for example, the Linux kernel. These files are essential for the system to boot properly.

/dev/ contains device nodes that represent the following device types: devices attached to the system;virtual devices provided by the kernel. These device nodes are essential for the system to function properly 

/etc/ directory is reserved for configuration files that are local to the machine. It should contain no binaries; any binaries should be moved to /bin/ or /sbin/. The /etc/ directory contains configuration files that are needed while your server boots. Putting /etc on a dedicated device would make your server unbootable.

/etc/sysconfig/network-scripts/ explores various interface configuration files, interface control scripts, and network function files

/lib/ directory should only contain libraries needed to execute the binaries in /bin/ and /sbin/. These shared library images are used to boot the system or execute commands within the root file system.

/media/ contains removable devices directories. directory contains subdirectories used as mount points for removable media such as USB storage media, DVDs, CD-ROMs, and Zip disks.

/run/media/username/mount-name contains removable devices directories. directory contains subdirectories used as mount points for removable media such as USB storage media, DVDs, CD-ROMs, and Zip disks.

/home/ Home directories where regular users

/var/ Variable data specific to this system (ex: website content)

/root/ Home directory for the super user, root.

/bin/ Applications binaries

/sbin/ System Applications binaries

/tmp/ Contains temporary files that may be deleted without any warning during boot.

/usr/ Contains subdirectories with program files, libraries for these program files, and documentation about them.

/srv/ May be used for data by services like NFS, FTP, and HTTP.

/sys/ Used as an interface to different hardware devices that is managed by the Linux kernel and associated processes.

/proc/ Processes Infos Files


Managing Files


Managing Files and Directories

[root@lab ~]# cd directory Browse into filesystem , move to a directory
[root@lab ~]# tree directory Directory Subtree
[root@lab ~]# pwd (print working Directory) show current directory
[root@lab ~]# cp file Destination-directory copy a file in a directory
[root@lab ~]# cp -R directory destination_directory copy a directory
[root@lab ~]# touch filename Change file properties. Create new file if filename doesn't exist
[root@lab ~]# vi filename Edit file. Create new file if filename doesn't exist
[root@lab ~]# mkdir Directory Create new directory
[root@lab ~]# rmdir folder delete an empty directory
[root@lab ~]# stat file file statistics and detailed infos
[root@lab ~]# file file Displays the type of a file
[root@lab ~]# mv filepath destination_directory Move a file
[root@lab ~]# mv Old-filename New-filename rename a file

Copying Files and Directories

cp Command

[root@lab ~]# ls –options file or directory show directories contents and files characteristics [+]
[root@lab ~]# ll Same as ls -l
[root@lab ~]# rm –options fichier : delete a file. [+]

Finding Files

[root@lab ~]# find directory-of-search –name filename
[root@lab ~]# find directory-of-search –user username files created by user

[root@lab ~]# locate pattern

Compared to the more powerful find command that searches the file system, locate operates much faster but lacks many features and can search only by the file name.


Absolute and relative Paths


Absolute paths

■ Chemin défini par rapport à la racine
■ Le Chemin absolu commence toujours par un / vu qu'il est défifni par rapport à la racine
■ Le chemin absolu ne change pas quelque soit notre position dans la structure du système de fichiers

Relative paths

■ Chemin défini par rapport à notre position actuelle sur le système de fichier
■ Le chemin relatif ne commence jamais par un /
■ Le chemin relatif change selon notre position dans la structure du système de fichiers

Example

[user1@localhost ~]$ mkdir /home/user1/DATA/PATHS
[user1@localhost ~]$ cd DATA/PATHS/
[user1@localhost PATHS]$ mkdir WORLD
[user1@localhost PATHS]$ mkdir /home/user1/DATA/PATHS/WORLD/AFRICA
[user1@localhost PATHS]$ mkdir WORLD/EUROPA
[user1@localhost PATHS]$ ls WORLD/
AFRICA EUROPA

[user1@localhost AFRICA]$ ls -l /home/user1/DATA/
total 4
drwxrwxr-x. 3 user1 user1 31 Jun 16 16:14 DIR1
-rw-rw-r--. 1 user1 user1 161 Jun 17 13:21 File
drwxrwxr-x. 3 user1 user1 19 Jun 23 15:45 PATHS
drwxrwxr-x. 4 user1 user1 56 Jun 18 06:58 PERMISSIONS
drwxrwxr-x. 2 user1 user1 6 Jun 18 06:22 REP1
drwxrwxr-x. 2 user1 user1 18 Jun 18 10:35 SCRIPTS

[user1@localhost AFRICA]$ ls -l ../../../
total 4
drwxrwxr-x. 3 user1 user1 31 Jun 16 16:14 DIR1
-rw-rw-r--. 1 user1 user1 161 Jun 17 13:21 File
drwxrwxr-x. 3 user1 user1 19 Jun 23 15:45 PATHS
drwxrwxr-x. 4 user1 user1 56 Jun 18 06:58 PERMISSIONS
drwxrwxr-x. 2 user1 user1 6 Jun 18 06:22 REP1
drwxrwxr-x. 2 user1 user1 18 Jun 18 10:35 SCRIPTS


Using Links


Physical and Symbolic Links



ls -l second column of ls -l output : number of physical links

Liens symboliques

[root@lab ~]# ln –s source_file destination_file

Liens physiques

[root@lab ~]# ln source_file destination_file

NB: Use Absolute Paths

When you create a file, you give it a name. Basically, this name is a hard link. On a Linux file system, multiple hard links can be created to a file. This can be useful, because it enables you to access the file from multiple different locations. Some restrictions apply to hard links, though:

• Hard links must exist all on the same device (partition, logical volume, etc).
• You cannot create hard links to directories.
• When the last name (hard link) to a file is removed, access to the file’s data is also removed.


Working with Archives and Compressed Files


Archives and Compressed Files

L'archivage est une technique qui permet de mettre en paquet un ou plusieurs fichiers afin de faciliter la manipulation et surtout le transport. En exemple nous avons les format exe, iso, rpm, deb, rar, zip, ...

La compression est une technique qui permet de réduire la taille d'un fichier afin de réduire la taille qu'il occupe sur le volume. Il existe 2 types de compresion, la compression lossless et la compression lossy.

zip Command

[root@lab ~]# zip archivename file1 file2 create a zip archive
[root@lab ~]# zip –r archivename directory-1 directory-2 create a zip archive of Directories
[root@lab ~]# unzip archive.rar extract archive
[root@lab ~]# unzip archive.rar -d extract-directory extract archive

tar Command


With tar, users can gather large sets of files into a single file (archive). The archive can be compressed using gzip, bzip2, or xz compression.

Creating Archives
[root@lab ~]# tar -cvf archivename file1 file2 create a tar archive of files or directories
[root@lab ~]# tar -zcvf archivename folder1 folder2 create a tar.gz archive
[root@lab ~]# tar -jcvf archivename folder1 folder2 create a tar.gz2 archive
[root@lab ~]# tar -Jcvf archivename folder1 folder2 create a xz compressed archive

Extract Archives
[root@lab ~]# tar -xvf archivename.tar decompress
[root@lab ~]# tar -zxvf archivename decompress a tar.gz archive

Uncompress Archives
[root@lab ~]# gunzip archive.tar.gz uncompress .tar.gz file to .tar file
[root@lab ~]# bunzip2 archive.tar.bz2 uncompress .tar.bz2 file to .tar file
[root@lab ~]# unxz archive.tar.xz uncompress .tar.xz file to .tar file


Files Compressing

Compressing

Syntax [user@redhat ~]$ gzip file-name : GZIP Compression

[user@redhat ~]$ bzip2 file-name : BZIP2 Compression

[user@redhat ~]$ xz file-name : XZ Compression

UnCompressing

Syntax [user@redhat ~]$ gunzip file-name : GZIP Uncompression

[user@redhat ~]$ bunzip2 file-name : BZIP2 Uncompression

[user@redhat ~]$ unxz file-name : XZ Uncompression

Examples








Eninam BAGNABANA     |         |     bagnadavid@outlook.fr     |