Chapter 2 - File and Directory Management - Operating Systems 2 - PDF
Document Details
![ConscientiousArithmetic](https://quizgecko.com/images/avatars/avatar-4.webp)
Uploaded by ConscientiousArithmetic
Amina Magdich
Tags
Summary
This document is a chapter from an Operating Systems 2 course, created by Amina Magdich, focused on file and directory management within a Linux environment. It details the Linux directory structure, commands used for managing files and directories, and concepts such as file types and names, making it suitable for undergraduate students.
Full Transcript
Operating Systems 2 TECM 113 Chapter 2: File and directory management Dr/ Amina Magdich TECM 113 2 Chapter 2: File and directory management Chapter outline I. Introduction II....
Operating Systems 2 TECM 113 Chapter 2: File and directory management Dr/ Amina Magdich TECM 113 2 Chapter 2: File and directory management Chapter outline I. Introduction II. Linux Directory structure III. Directories and Files management IV. Removable Storage V. Conclusion Dr/ Amina Magdich TECM 113 3 Chapter 2: File and directory management I. Introduction: The objectives of this chapter are: Understand and navigate the linux directory structure using relative and absolute pathnames Use standard linux commands to manage files and directories Describe the various types of linux files Display the contents of text files and binary files Search text files for regular expressions using grep Use the vi editor to manipulate text files Dr/ Amina Magdich TECM 113 4 Chapter 2: File and directory management II. Linux Directory structure: Directory: used to organize other files into a logical tree structure Absolute pathname: pathname from the root directory (/) to a certain file or directory Root: the top level directory - Referred to using the / character - Forms root of a hierarchical tree Dr/ Amina Magdich TECM 113 5 Chapter 2: File and directory management II. Linux Directory structure: Figure 2.1: the windows file system Dr/ Amina Magdich structure TECM 113 6 Figure 2.2: The Linux file system Dr/ Amina Magdich structure TECM 113 7 Table 2.1: Linux directories defined Dr/ Amina Magdich TECM 113 8 Chapter 2: File and directory management II. Linux Directory structure: Changing directories: Home directory: unique to each user pwd (print working directory) command: displays current directory in the directory tree cd (change directory) command: change the current directory in the directory tree Argument specifies the destination directory ~metacharacter: refers to home directory (personal directory) Dr/ Amina Magdich TECM 113 9 Chapter 2: File and directory management II. Linux Directory structure: Changing directories: Relative pathname: pathname of file or directory relative to current directory Parent directory: directory one step closer to the root of the tree $cd.. Subdirectory: directory residing within another directory Tab-completion: pressing the tab key fills in remaining characters Dr/ Amina Magdich of a unique file name or directory name TECM 113 10 Chapter 2: File and directory management III. Directories and File management: Viewing files and directories: file types Text files: store information in a readable text format, contain configuration information Binary data files: store information associated with executable programs Executable program files Directory files: serve as place holders to organize other files Dr/ Amina Magdich TECM 113 11 Chapter 2: File and directory management III. Directories and File management: Viewing files and directories: file typesfiles: associated with another file Linked Special device files: represent system devices Named pipes: identify channel that passes information between processes Socket files: Allow a process on another computer to write to a local file Dr/ Amina Magdich TECM 113 12 Chapter 2: File and directory management III. Directories and File management: File names File name: identifier given to a file File name extensions: identifiers following a dot (.) at end of file name Most files on Linux do not have file extensions Dr/ Amina Magdich TECM 113 13 Chapter 2: File and directory management Table 2.2: Common file name Dr/ Amina Magdich extensions TECM 113 14 Chapter 2: File and directory management Table 2.3: Common file name Dr/ Amina Magdich extensions TECM 113 15 Chapter 2: File and directory management III. Directories and File management: Directory management mkdir command: creates new directories Use –p to create more than one directory Example: $mkdir /home/Lina/MyFolder $mkdir -p /home/Lina/Folder1/Folder2/Folder3 Dr/ Amina Magdich TECM 113 16 Chapter 2: File and directory management III. Directories and File management: Directory management mv command: to move files Two arguments minimum: - Source file/directory - Target file/directory $mv source Destination mv is also used to rename files Example: $mv /home/Lina/File1 /home/Lina/File2 to rename file1 to Dr/ Amina Magdich File2 TECM 113 17 Chapter 2: File and directory management III. Directories and File management: Directory management Exercice: Move the directory Dir 1 from /home/Asmaa to /home (/home/Asmaa/Dir1) $mv /home/Asmaa/Dir1 /home Dr/ Amina Magdich TECM 113 18 Chapter 2: File and directory III. Directories and File management: management Directory management cp command: to copy files Two arguments minimum: - Source file/directory - Target file/directory $cp source file target file Use –r to copy the folder and its content (when the folder is not empty) Example: $cp -r /home/Lina/Folder1 /home/Lina/MyFolder to copy Dr/ Amina Magdich TECM 113 19 Chapter 2: File and directory management III. Directories and File management: Directory management rm command: to remove files or directories rmdir command: remove empty directories -r is used to remove a directory and its content Example: $rmdir /home/Lina/MyFolder (MyFolder is empty) to delete an empty directory $rm -r /home/Lina/MyFolder (MyFolder is not empty) to delete a directory and its content $rm Dr/ Amina/home/Lina/File1 Magdich to delete a file TECM 113 20 Chapter 2: File and directory management III. Directories and File management: Directory management Table 2.4: Common Linux file management Dr/ Amina Magdich TECM 113 21 Chapter 2: File and directory management III. Directories and File management: File management Listing files ls command: List the files in a directory - May pass an argument indicating the directory to be listed -F: argument to list file types -l: argument to list long file listings (listing with details) https://www.youtube.com/watch?v=3MfaZFUC1yE Dr/ Amina Magdich TECM 113 22 Chapter 2: File and directory management III. Directories and File management: File management Listing files file: the file command displays the file type of any file Example: $file TECM113.txt Ascii text: means text file Figure 2.3: Examples of the file command use Dr/ Amina Magdich TECM 113 23 Chapter 2: File and directory management III. Directories and File management: File management Listing files Hidden files: Files not normally displayed to users File names start with a dot (.) Configration files are often hidden $ls –a : displays all the files including hidden files Example: $ls -a /home Dr/ Amina Magdich TECM 113 24 Chapter 2: File and directory management III. Directories and File management: File management Listing files Option Description -a Lists all the files (with hidden files) -A Lists all the files excluding the. And.. Special files -C Lists files in column format -f Lists all the files without sorting -F Lists the files classified by type adds an « / » after the name of each directory, an « * » after a file and an « @ » after a link -r Lists files reverse sorted Table2.5: Common options to the ls Dr/ Amina Magdich command TECM 113 25 Chapter 2: File and directory management III. Directories and File management: File management Listing files Option Description -R Lists the files in the specified directory and all subdirectories -s lists the files and their associate size in kilobytes (KB) -S Lists the files sorted by the file size -t Lists the files sorted by modification time Table2.6: Common options to the ls command https://www.youtube.com/watch?v=2QQk4LJvJP4 https://www.youtube.com/watch?v=2zVvIrhF5KA https://www.youtube.com/watch?v=2zVvIrhF5KA Dr/ Amina Magdich TECM 113 26 Chapter 2: File and directory management III. Directories and File management: File management Listing files Alias: shortcut for a command Example: $alias C= " clear " https://www.youtube.com/watch?v=vIt4aYcVpQE https://www.youtube.com/watch?v=uLYxq_B8s44 $alias: allows to list the existent alias Dr/ Amina Magdich TECM 113 27 Chapter 2: File and directory management III. Directories and File management: File management Creating files touch command: to create an empty file Example: $touch /home/Lina/OS allows to create a file named OS Dr/ Amina Magdich TECM 113 28 Chapter 2: File and directory management III. Directories and File management: File management Displaying content of text files less file more file cat file Example 1: $cat /etc/passwd to display the content of the file passwd Example 2: $less /etc/passwd to display the content of the file passwd Dr/ Amina Magdich TECM 113 29 Chapter 2: File and directory ABC TECM management III. Directories and File management: DEF OS File management 1. ABC Displaying content of text files 2. DEF Concatenation: joining text together 3. TECM 4. OS cat command : displays (concatenates) contents of a text file to the screen -n: displays line number and contents Example 1: $cat /etc/passwd to display the content of the file passwd Example 2: $cat test Dr/ Amina test 1 concatenates the content of the files Magdich TECMtest 113and 30 Chapter 2: File and directory management III. Directories and File management: File management Displaying content of text files head file: to view the first ten (10) lines of a file Example : The command The Result Dr/ Amina Magdich TECM 113 31 Chapter 2: File and directory management III. Directories and File management: File management Displaying content of text files To limit the number of lines using head use the –n option followed by the number of lines to show Example : The command The Result Dr/ Amina Magdich TECM 113 32 Chapter 2: File and directory management III. Directories and File management: File management Displaying content of text files $tail file: to view the last 10 lines of a file Example : Dr/ Amina Magdich TECM 113 33 Chapter 2: File and directory management III. Directories and File management: File management Displaying content of text files It is possible to specify the number of lines to display using the option -n Example : Dr/ Amina Magdich TECM 113 34 Chapter 2: File and directory management A III. Directories and File management: B C File management D Displaying content of text files tac : displays files in reverse order $cat file1 $tac file1 Example: A D $ tac File B C C B D A Dr/ Amina Magdich TECM 113 35 Chapter 2: File and directory management III. Directories and File management: File management grep command grep is the acronym of Global Regular Expression Print grep is used to search for a string of characters in a specified file. It displays lines in a text file matching a given string grep requires two arguments : text to search for and files to search Syntax: $grep stringToMatch File Dr/ Amina Magdich TECM 113 36 Chapter 2: File and directory management III. Directories and File management: File management grep command Example: Dr/ Amina Magdich TECM 113 37 Chapter 2: File and directory management III. Directories and File management: File management Editing text files vi One of the oldest and most popular text editor for UNIX OS: vi : Linux editor which works the same across different platforms and distributions Syntax: vi filename_new or vi filename_Existing Example: $vi text1 https://www.youtube.com/watch?v=o08un9UEuWo Dr/ Amina Magdich TECM 113 38 Chapter 2: File and directory management III. Directories and File management: File management Editing text files: To write in the file press i shift+zz to save and close the file :w to save the file but keep it open :q! to quit without saving :wq to save the file and quit https://www.youtube.com/watch?v=o08un9UEuWo Dr/ Amina Magdich TECM 113 39 Chapter 2: File and directory management III. Directories and File management: File management Dr/ Amina Magdich TECM 113 40 Chapter 2: File and directory management III. Directories and File management: File management Dr/ Amina Magdich TECM 113 41 Chapter 2: File and directory management III. Directories and File management: File management Dr/ Amina Magdich TECM 113 42 Chapter 2: File and directory management III. Directories and File management: File management Dr/ Amina Magdich TECM 113 43 Chapter 2: File and directory management III. Directories and File management: File management Editing text files (vi command): Table2.7: Common keyboards keys used to change to and Dr/ Amina Magdich TECM 113 from insert mode 44 Chapter 2: File and directory management Table2.8: Key combinations commonly used at the Dr/ Amina Magdich command mode: prompt TECM 113 45 Chapter 2: File and directory management III. Directories and File management: File management Editing text files vim vim: Linux equivalent of vi (it is vi improved) Syntax: vim Example: $vim text1 https://www.youtube.com/watch?v=CM7UP-un1vc Dr/ Amina Magdich TECM 113 46 Chapter 2: File and directory management III. Directories and File management: File management Editing text files mcedit editor (Midnight Commander Editor) - Easy ti use - Supports use of mouse for highlighting text Syntax: $mcedit Example: $mcedit Text1 https://www.youtube.com/watch?v=QuyakInZjvg Dr/ Amina Magdich TECM 113 47 Chapter 2: File and directory management III. Directories and File management: File management Editing text files emacs editor (Editor MACroS) - Comparable functionality to vi - Supports LISP (LISt Processing) artificial intelligence programming language Syntax: $emacs Example: $emacs Text1 https://www.youtube.com/watch?v=4jQmS0gS-lU Dr/ Amina Magdich TECM 113 48 Chapter 2: File and directory management III. Directories and File management: File management emacs editor (Editor MACroS) Table2.9: keyboard functions commonly used in the GNU Dr/ Amina Magdich emacs editor TECM 113 49 Chapter 2: File and directory management III. Directories and File management: File management Editing text files emacs editor (Editor MACroS) - emacs is not easy to use: must memorize key combinations - xemacs editor: version of emacs for KDE or GNOME GUI environments Easier to use than emacs Not available with every distribution Dr/ Amina Magdich TECM 113 50 Chapter 2: File and directory management III. Directories and File management: Editing text files xemacs editor The xemacs text Dr/ Amina Magdich editor TECM 113 51 Chapter 2: File and directory management III. Directories and File management: File management Editing text files nedit editor gedit editor: distibuted with GNOME environment kedit editor: distributed with KDE environment gedit and kedit offer more functionality than nedit Dr/ Amina Magdich TECM 113 52 Chapter 2: File and directory management III. Directories and File management: The nedit text Dr/ Amina Magdich editor TECM 113 53 Chapter 2: File and directory management III. Directories and File management: The gedit text Dr/ Amina Magdich editor TECM 113 54 Chapter 2: File and directory management III. Directories and File management: The kedit text Dr/ Amina Magdich editor TECM 113 55 Chapter 2: File and directory management III. Directories and File management: File management Log files Log files: contain records of past system events Dr/ Amina Magdich TECM 113 56 Chapter 2: File and directory management IV. Removable storage: File systems A file system is a way of organizing files on your partition. The linux kernel will inform you about currently loaded file system drivers in /proc/filesystems /proc/filesystems file contains a list of autodetected filesystems Dr/ Amina Magdich TECM 113 57 Chapter 2: File and directory management IV. Removable storage: Common file systems Ext2 and ext3 The most common linux file systems is the ext2. A disadvantage is that file system checks on ext2 can take a long time Ext2 was replaced by ext3 Ext2 and Ext3 are the same, except for the journaling wich is only present in ext3. Dr/ Amina Magdich TECM 113 58 Chapter 2: File and directory management IV. Removable storage: Common file systems Ext4 It is a new file system which is available in the linux kernel since 2008. It supports larger files than ext3 Xfs Is a file system supported in Redhat. It is a highly scalable high-performance file system Dr/ Amina Magdich TECM 113 59 Chapter 2: File and directory management IV. Removable storage: Common file systems Vfat The vfat file system exists in a couple form: fat12 for floppy disks, fat 16 on msdos, and fat32 for larger disks. Fat disks can be read by every OS. Iso 9660 Is the standard format for cdrooms Udf, swap, gfs, reiserfs, and so on … Dr/ Amina Magdich TECM 113 60 Chapter 2: File and directory management IV. Removable storage: Mounting volumes The mount command mounts a storage device or filesystem, making it accessible and attaching it to an existing directory structure Once you put a file system on a partition (or insert a cd), you can mount it. Mounting a file system makes it available for use, usually as a directory. Syntax: mount Dr/ Amina -t type-device directory Magdich TECM 113 61 Chapter 2: File and directory management IV. Removable storage: Mounting volumes Example: To access files on a cd-rom, you must mount the cd-rom on a location in the directory tree mount -t /dev/cdrom /media/cdrom $mount –t /dev/cdrom /MyCD /dev/cdrom is the type of the device /media/cdrom is the chosen mount point (a directory that exists or that Dr/ Amina must be created before applying the command) Magdich TECM 113 62 Chapter 2: File and directory management IV. Removable storage: Unmounting volumes The unmount command unmounts a mounted filesystem, informing the system to complete any pending read or write operations and safely detaching it Example: unmount /media/cdrom Dr/ Amina Magdich TECM 113 63 Chapter 2: File and directory management IV. Conclusion ▸ The Linux file system is arranged hierarchically using a series of directories to store files ▸ Location of these directories and files can be described using absolute or relative pathnames ▸ Text files are the most common file type whose contents can be viewed by several utilities such as head, tail, cat, more and less Dr/ Amina Magdich TECM 113 64