Linux Directories and Files 2023-2024 PDF
Document Details
Uploaded by CrispMusicalSaw
Université Ferhat Abbas Sétif 1
2024
Dr S. Balbal
Tags
Summary
This document is a chapter on Directories and Files in Linux. It covers topics such as introduction, reminders, paths, directories, files, operations, and special characters. Specifically looking at the Linux file system.
Full Transcript
Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Chapter 3 Directories and Files in Linux...
Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Chapter 3 Directories and Files in Linux Dr S.Balbal 2023-2024 Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 1 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Contenu 1 Introduction 2 Reminder 3 Path 4 Directorie print working directory change directory list the content of directories make directory remove directory Rename or move a directory Find the location of a directory Copy the contents of a directory 5 Files Create one or more files Delete one or more files Read the content of a file or files Rename or move a file Search for the location of a file Search for the location of a file binaire Copy a file Determine the type of file Search for a word in a file Search and replace a word in a file 6 Raccourcis OS 1 Dr S.Balbal Chapter 3: Directories and Files in Linux 2023-2024 2 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Introduction Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 3 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Introduction Files: are used to store data. There are several types of files: text; image; audio; script; database; etc. Directories: are used for organization. Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 4 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression How are files and directories managed and organized in Linux? The File System (FS)? FS defines the organization of data on a storage medium. The Linux FS depicts a tree structure of directories and sub-directories, starting from a global directory known as the root "/". Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 5 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Linux Directory Hierarchy Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 6 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Linux Directory Hierarchy Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 7 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Linux Directory Hierarchy Important: Directory Racine ( / ) home; Home Directory: /home/username/ CMD : pwd Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 8 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Reminder Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 9 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Reminder username @ hostname : directory To know the username, execute the command: logname To know the hostname, execute the command: hostname To know the current directory (working directory), execute the command: pwd Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 10 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Reminder Filesystem structure Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 11 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Path Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 12 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Path Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 13 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression File and directory names Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 14 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Absolute vs relative paths Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 15 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression The "dot"(.) directories Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 16 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression The home directory ( login directory) Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 17 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Directorie Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 18 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Directory operations 1 print working directory ; 2 change directory ; 3 list the content of directories 4 make directory ; 5 remove directory ; 6 Rename or move a directory ; 7 Find the location of a directory ; 8 Copy the contents of a directory ; Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 19 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression print working directory print working directory pwd [options] pwd shows the path to the working directory Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 20 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression change directory change directory cd [path] cd changes the working directory Allows you to “move” into another directory. With a relative path, With an absolute path Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 21 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression list the content of directories list the content of directories lists contents of a directory ls [options] [rep 1] [rep 2]... [rep n] Many options available, e. g. : -l, uses a long listing format (permissions, owner, size, mod. times) -a, ’show all’, forces ls to show all files and directories -h, displays the file sizes in human readable format -F, adds a trailing ’/’ to the names of directories -r, reverses the order while sorting Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 22 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression make directory make directory mkdir (make directory) creates new (empty) directories mkdir [options] rep 1 [rep 2]...[rep n] If the directory already exists, it reports an error mkdir -p – no error if existing, make parent directory if needed Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 23 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression remove directory remove directory rmdir [options] rep 1 [rep 2]...[rep n] rm -r Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 24 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Rename or move a directory Rename or move a directory mv [options] [rep source] [rep distination] Rename / move Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 25 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Find the location of a directory Find the location of a directory find [options] [rep] locate [options] [rep] Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 26 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Copy the contents of a directory Copy the contents of a directory cp -r [options] [rep source] [rep distination] Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 27 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Files Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 28 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Files operations 1 Create one or more files; 2 Delete one or more files; 3 Read the content of a file or files; 4 Rename a file; 5 Find the location of a file; 6 Copy a file; 7 Determine the type of file; 8 Search for a word in a file; 9 Search and replace a word in a file. Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 29 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Create one or more files Create one or more files touch [options] file1 [file2]...[filen] Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 30 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Delete one or more files Delete one or more files rm [options] [options] file1 [file2]...[filen] Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 31 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Read the content of a file or files Read the content of a file or files cat [options] [options] [options] file1 [file2]...[filen] more [options] file less [options] file In summary, while cat is great for quick, simple tasks involving short files, more and less are better suited for navigating through longer documents, with less providing the most functionality and efficiency, especially for larger files. Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 32 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Rename or move a file Rename or move a file mv [options] [file source] [file or rep destination] Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 33 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Search for the location of a file Search for the location of a file find [options] [file] locate [options] [file] Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 34 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Search for the location of a file binaire Search for the location of a file binaire which [options] namecmd whereis [options] namecmd Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 35 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Copy a file Copy a file cp [options] [file source] [file distination] Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 36 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Determine the type of file Determine the type of file file [options] nameFile Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 37 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Search for a word in a file Search for a word in a file grep [options] word file Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 38 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Search and replace a word in a file Search and replace a word in a file sed [options] ’command’ filename Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 39 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Raccourcis Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 40 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Raccourcis shell Raccourcis shell ctrl+r "up arrow" et "down arrow" ctrl+l ctrl+c tab tab tab Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 41 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Special Characters Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 42 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Special Characters Special Characters ˜(tilde) * ? Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 43 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression hidden Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 44 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression hidden files and folders hidden files and folders ls -a Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 45 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression exercise Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 46 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression exercise exercise Create the directory hierarchy that matches the diagram given below. Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 47 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Archiving & Compression Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 48 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Data Archiving Data Archiving Gather multiple files into one, to facilitate their handling. Useful for backup and transfer. tar -cvf archivename.tar /path/to/directoryorfile -c creates a new archive. -v verbosely lists the files processed (optional). -f specifies the filename of the archive. Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 49 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Data Archiving Examples for data archiving Create a tar archive tar cvf archive.tar file1 file2 file3 cvf archive.tar /home/* View the contents of a tar archive tar tvf archive.tar Extract a tar archive tar xvf archive.tar Extract specific files from a tar archive tar xvf archive.tar file2 Adding files to an archive : adds the file file4 to the archive.tar archive. tar -rvf archive.tar file4 Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 50 / 51 Introduction Reminder Path Directorie Files Raccourcis Special Characters hidden exercise Archiving & Compression Data Archiving Backup using relative or absolute path In Linux, when performing backups, you can specify either a relative or an absolute path to refer to the files or directories you want to back up. tar cvf /backup/archive.tar /etc/config This command creates a tar archive named archive.tar in the /backup directory, con- taining the contents of /etc/config. Here /backup/archive.tar and /etc/config are absolute paths. In the same way, one can do: View the contents Extract Dr S.Balbal OS 1 Chapter 3: Directories and Files in Linux 2023-2024 51 / 51