Upload
augustus-owens
View
292
Download
5
Tags:
Embed Size (px)
Citation preview
Linux+ Guide to Linux Certification
Chapter Four
Exploring Linux Filesystems
ObjectivesObjectives
• 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 StructureThe 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 StructureThe 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 DirectoriesChanging 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 DirectoriesChanging 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 DirectoriesChanging 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 TypesFile Types
• Common file types:– Text files– Binary data files– Executable program files– Directory files– Linked files– Special device files– Named pipes and socket files
FilenamesFilenames
• 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
FilenamesFilenames
Table 4-1: Common filename extensions
FilenamesFilenames
Table 4-1 (continued): Common filename extensions
FilenamesFilenames
Table 4-1 (continued): Common filename extensions
Listing FilesListing 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 FilesListing 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 FilesListing 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 FilesListing Files
Table 4-2: Common options to the ls command
Listing FilesListing Files
Table 4-2 (continued): Common options to the ls command
Wildcard MetacharactersWildcard 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 MetacharactersWildcard Metacharacters
Table 4-3: Wildcard metacharacters
Displaying Content of Text FilesDisplaying 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 FilesDisplaying 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 FilesDisplaying 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 FilesDisplaying 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 FilesDisplaying 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 Displaying the Contents of Binary FilesFiles
• 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 Displaying the Contents of Binary FilesFiles
• 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 FilesSearching 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 FilesSearching for Text within Files
• Text tools and programming languages that use regular expressions include:– grep– awk– sed– vi– emacs
Searching for Text within FilesSearching for Text within Files
• Text tools and programming languages that use regular expressions include (continued):– ex– ed– C++– PERL– Tcl
Regular ExpressionsRegular 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 ExpressionsRegular 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 ExpressionsRegular Expressions
Table 4-4: Regular expressions
Regular ExpressionsRegular Expressions
Table 4-4 (continued): Regular expressions
The grep CommandThe 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 EditorThe 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 EditorThe 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 EditorThe vi Editor
Table 4-5: Common keyboard keys used to change to and from insert mode
The vi EditorThe vi Editor
Table 4-6: Key combinations commonly used in command mode
The vi EditorThe vi Editor
Table 4-6 (continued):Key combinations commonly used in command mode
The vi EditorThe vi Editor
Table 4-6 (continued):Key combinations commonly used in command mode
The vi EditorThe vi Editor
Table 4-7:Key combinations commonly used at the command mode : prompt
Other Common Text EditorsOther 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 EditorsOther 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 EditorsOther Common Text Editors
Table 4-8: Keyboard functions commonly used in the GNU emacs editor
Other Common Text EditorsOther 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 EditorsOther Common Text Editors
Figure 4-3: The xemacs test editor
Other Common Text EditorsOther 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 EditorsOther Common Text Editors
Figure 4-4: The exedit text editor
Other Common Text EditorsOther Common Text Editors
Figure 4-5: The nedit text editor
Other Common Text EditorsOther 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
Other Common Text EditorsOther Common Text Editors
Figure 4-6: The gedit text editor
Other Common Text EditorsOther Common Text Editors
Figure 4-7: The kedit text editor
Chapter SummaryChapter Summary
• The Linux filesystem is arranged hierarchically using a series of directories to store files, and the location of these directories and files can be described using absolute or relative pathnames
• There are many types of files that may exist on the Linux filesystem
• The ls command can be used to view filenames and offers a wide range of options to modify this view
Chapter SummaryChapter Summary
• Wildcard metacharacters can be used to simplify the selection of several files when using common Linux file commands
• Text files are the common file type whose contents may be viewed by several utilities such as head, tail, cat, tac, more, and less
• Regular expressions can be used to specify certain patterns of text when used with certain programming languages and text tools such as grep
• Vi (vim) is a powerful, bi-modal text editor that is standard on most UNIX and Linux systems