![]() | Red Hat Certified System Administration 8 |
Chapter 03: Essential File Management ToolsGoalIn 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 HierarchyFile System OrganizationDirectory StructureOn 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 FilesManaging Files and Directories
Copying Files and Directories[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 PathsAbsolute 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 LinksPhysical and Symbolic Links![]() ls -l second column of ls -l output : number of physical links Liens symboliques[root@lab ~]# ln –s source_file destination_fileLiens physiques[root@lab ~]# ln source_file destination_fileNB: 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 FilesArchives and Compressed FilesL'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 CommandWith 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 CompressingCompressingUnCompressingExamples![]() ![]() ![]() |