540 likes | 632 Views
Chapter Four. Exploring Linux Filesystems. Objectives. Understand and navigate the Linux directory structure using relative and absolute pathnames Describe the various types of Linux files View filenames and file types Use shell wildcards to specify multiple filenames
E N D
Chapter Four Exploring Linux Filesystems
Objectives • Understand and navigate the Linux directory structure using relative and absolute pathnames • Describe the various types of Linux files • View filenames and file types • Use shell wildcards to specify multiple filenames • Display the contents of text files and binary files • Search text files for regular expressions using grep • Identify common text editors used today • Use the vi editor to manipulate text files
The Linux Directory Structure • Directory • Special file on the filesystem used to organize other files into a logical tree structure Figure 4-1: The Windows filesystem structure
The Linux Directory Structure • Absolute pathname • Full pathname to a certain file or directory starting from the root directory Figure 4-2: The Linux filesystem structure
Changing Directories • Home directory • A directory on the file system set aside for users to store personal files and information • pwd command (print working directory) • Linux command used to display the current directory in the directory tree • cd (change directory) command • Linux command used to change the current directory in the directory tree
Changing Directories • ~ metacharacter • Can be used to refer to the current user’s home directory • Can be used to specify another user’s home directory by appending a username at the end of the ~ metacharacter • Relative pathname • Pathname of a target directory relative to your current directory in the tree
Changing Directories • Subdirectory • Directory that resides within another directory in the directory tree • Tab-completion • Feature of the BASH Shell that fills in the remaining characters of a unique filename or directory name when the user presses the Tab key
File Types • Common file types: • Text files • Binary data files • Executable program files • Directory files • Linked files • Special device files • Named pipes and socket files
Filenames • Filename • User-friendly identifier given to a file • Executable programs • File that can be executed by the Linux OS to run in memory as a process and perform a useful function • Filename extensions • Series of identifiers following a dot (.) at the end of a filename used to denote the type of the file
Filenames Table 4-1: Common filename extensions
Filenames Table 4-1 (continued): Common filename extensions
Filenames Table 4-1 (continued): Common filename extensions
Listing Files • ls command • Linux command used to list the files in a given directory • Most common method for displaying files • Displays all the files in the current directory in columnar format • However, you may also pass an argument to the ls command indicating the directory to be listed if different from current directory listing
Listing Files • ll command • Alias for the ls -l command • Gives a long file listing • File command • Linux command that displays the file type of a specified filename
Listing Files • Text file • File that stores information in a readable text format • Some filenames inside each user’s home directory represent important configuration files or program directories • Hidden files • Files that are not normally displayed to the user via common filesystem commands
Listing Files Table 4-2: Common options to the ls command
Listing Files Table 4-2 (continued): Common options to the ls command
Wildcard Metacharacters • Wildcard metacharacter • Can simplify commands that specify more than one filename on the command line • Interpreted by the shell and can be used with most common Linux filesystem commands • Matches certain portions of filenames, or the entire filename itself
Wildcard Metacharacters Table 4-3: Wildcard metacharacters
Displaying Content of Text Files • Concatenation • Joining of text together to make one larger whole • In Linux, words and strings of text are joined together to form a displayed file • cat command • Linux command used to display (or concatenate) the entire contents of a text file to the screen
Displaying Content of Text Files • Log files • File that contains past system events • tac command • Linux command that displays a file to the screen beginning with the last line of the file and ending with the first line of the file
Displaying Content of Text Files • head command • By default, displays the first 10 lines (including blank lines) of a text file to the terminal screen • Can also take a numeric option specifying a different number of lines to display • tail command • By default, displays the last 10 lines (including blank lines) of a text file to the terminal screen • Can also take a numeric option specifying a different number of lines to display
Displaying Content of Text Files • more command • Linux command used to display a text file page-by-page and line-by-line on the terminal screen • Gets its name from the pg command once used on UNIX system • The more command does more than pg did
Displaying Content of Text Files • less command • Linux command used to display to display a text file page-by-page on the terminal screen • Users may then use the cursor keys to navigate the file • The more and less commands can also be used in conjunction with the output of other commands if that output is too large to fit on the terminal screen
Displaying the Contents of Binary Files • It is important to employ text file commands as cat, tac, head, tail, more, and less only on files that contain text • Otherwise you may find yourself with random output on the terminal screen, or even a dysfunctional screen • strings commands • Linux command used to search for and display text characters in a binary file
Displaying the Contents of Binary Files • od command • Linux command that is used to display the contents of a file in octal format (numeric base 8 format) • Safe to use on binary files and text files
Searching for Text within Files • Text tools • File that stores information in a readable text format • Regular expressions (regxp) • Special metacharacters used to match patterns of text within text files • Commonly used by many text tool commands such as grep
Searching for Text within Files • Text tools and programming languages that use regular expressions include: • grep • awk • sed • vi • emacs
Searching for Text within Files • Text tools and programming languages that use regular expressions include (continued): • ex • ed • C++ • PERL • Tcl
Regular Expressions • Differences between regular expressions and wildcard metacharacters include: • Wildcard metacharacters are interpreted by the shell • Regular expressions are interpreted by a text tool program • Wildcard metacharacters match characters in filenames (or directory names) on a Linux filesystem • Regular expressions match characters within text files on a Linux filesystem
Regular Expressions • Differences between regular expressions and wildcard metacharacters include (continued): • Wildcard metacharacters typically have different definitions than regular expressions • There are more regular expressions than wildcard metacharacters • Regular expressions are divided into two different categories: • Common • extended
Regular Expressions Table 4-4: Regular expressions
Regular Expressions Table 4-4 (continued): Regular expressions
The grep Command • grep • Stands for Global Regular Expression Print • Used to display lines in a text file that match a certain common regular expression • Use the egrep command to display lines of text that match extended regular expressions • The fgrep command does interpret any regular expressions and consequently returns results much faster than the egrep command
The vi Editor • One of the oldest and most popular visual text editors available for UNIX operating systems • Its Linux equivalent (known as vim—vi improved) is standard on almost every Linux distribution as a result • Though not the easiest of the editors to use when editing text files, it has the advantage of portability
The vi Editor • The vi editor is called a bi-modal editor as it functions in one of two modes: • Command mode • Allows a user to perform any available text editing task that is not related to inserting text into the document • Insert mode • Allows the user to insert text into the document but does not allow any other functionality
The vi Editor Table 4-5: Common keyboard keys used to change to and from insert mode
The vi Editor Table 4-6: Key combinations commonly used in command mode
The vi Editor Table 4-6 (continued):Key combinations commonly used in command mode
The vi Editor Table 4-6 (continued):Key combinations commonly used in command mode
The vi Editor Table 4-7:Key combinations commonly used at the command mode : prompt
Other Common Text Editors • Pico (PIne COmposer) editor • By far, the easiest alternative to the vi editor • Commonly used to create and edit e-mails • Mcedit editor (Midnight Commander Editor) • Resembles pico yet had more functionality, support for regular expressions, and ability to use the mouse for highlighting text
Other Common Text Editors • Emacs (Editor MAcroS) editor • Alternative to the vi editor that offers an equal set of functionality • Like pico, uses the Ctrl key in combination with certain letters to perform special functions • Yet can be used with the LISP (LSIt Processing) artificial programming language and supports hundreds of key board functions like the vi editor
Other Common Text Editors Table 4-8: Keyboard functions commonly used in the GNU emacs editor
Other Common Text Editors • The emacs editor is not an easy-to-use editor as it must memorize several key combinations to work effectively • Xemacs editor • Version if emacs that runs in the KDE or GNOME GUI environments • Much easier to use than emacs
Other Common Text Editors Figure 4-3: The xemacs test editor
Other Common Text Editors • The xemacs editor may not be available in every Linux distribution that contains a GUI environment • Two commonly used graphical text editors available in most Linux distributions: • Xedit editor • Nedit editor
Other Common Text Editors Figure 4-4: The exedit text editor
Other Common Text Editors Figure 4-5: The nedit text editor
Other Common Text Editors • Gedit editor • Text editor for the GNOME desktop • Kedit editor • Text editor for the KDE desktop • The gedit and kedit editors are similar to nedit yet offer more functionality