43
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Embed Size (px)

DESCRIPTION

Linux+ Guide to Linux Certification, 2e3 Objectives (continued) 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

Citation preview

Page 1: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, Second Edition

Chapter 4Exploring Linux

Filesystems

Page 2: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 2

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

Page 3: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 3

Objectives (continued)

• 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

Page 4: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 4

The 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

Page 5: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 5

The Linux Directory Structure (continued)

Figure 4-1: The Windows filesystem structure

Figure 4-2: The Linux filesystem structure

Page 6: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 6

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• ~ metacharacter: Refers to home directory• Relative pathname: Pathname of file or directory

relative to current directory

Page 7: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 7

Changing Directories (continued)

• Subdirectory: Directory residing within another directory

• Tab-completion: Pressing the Tab key fills in remaining characters of a unique filename or directory name– BASH shell feature

Page 8: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 8

Viewing Files and Directories: File Types

• Text files: Store information in a readable text format

• Binary data files: Store information associated with executable programs

• Executable program files• Directory files

Page 9: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 9

Viewing Files and Directories: File Types (continued)

• Linked files: Associated with another file• 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

Page 10: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 10

Filenames

• Filename: Identifier given to a file• Filename extensions: Identifiers following a dot (.)

at end of filename – Denote file type– Most files on Linux do not have file extensions

Page 11: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 11

Filenames (continued)

Table 4-1: Common filename extensions

Page 12: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 12

Filenames (continued)

Table 4-1 (continued): Common filename extensions

Page 13: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 13

Listing Files

• ls command: List the files in a directory– May pass an argument indicating the directory to be

listed• –F switch: Argument to list file types• –l switch: Argument to list long file listings

• Alias: Shortcut for a command• ll command: Alias for ls -l• File command: Displays file type of any file

Page 14: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 14

Listing Files (continued)

• Hidden files: Files not normally displayed to user– Filenames start with a dot (.)– Configuration files often hidden– ls –a command: Displays hidden files

Page 15: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 15

Listing Files (continued)

Table 4-2: Common options to the ls command

Page 16: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 16

Listing Files (continued)

Table 4-2 (continued): Common options to the ls command

Page 17: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 17

Wildcard Metacharacters

• Wildcard metacharacter: Used to simplify commands specifying multiple filenames– Can be used with most Linux filesystem commands

Page 18: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 18

Wildcard Metacharacters (continued)

Table 4-3: Wildcard metacharacters

Page 19: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 19

Displaying Content of Text Files

• Concatenation: Joining text together• cat command: Displays (concatenates) contents of

a text file to the screen– -n switch: Displays line number and contents

• Log files: Contain records of past system events– New events appended to end

• tac command: Displays files in reverse order• head command: View first few lines of a file

Page 20: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 20

Displaying Content of Text Files (continued)

• tail command: View last few lines of a file– Can also specify what line number to start at

• more command: Displays text files page-by-page– Space key goes to next page– Enter key goes to next line

• less command: Same as more command, but can also use cursor to scroll

Page 21: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 21

Displaying Content of Text Files (continued)

• more and less can be used with output of other commands – If output is too large to fit on terminal screen, use “|”

metacharacter– e.g., ls -l | more

Page 22: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 22

Displaying the Contents of Binary Files

• Typically use program that created the file• strings command: Searches for and displays text

characters in a binary file– Might indicate purpose of binary file

• od command: Displays contents of file in octal format (numeric base 8 format)

Page 23: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 23

Searching for Text Within Files

• Text tools: Search for and manipulate text• Regular expressions (regexp): Text wildcards that

assist search for specific text– Match patterns of text– Used by many text tools and programming

languages• Including grep, emacs, C++, PERL, and many more

Page 24: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 24

Regular Expressions

• Different from wildcard metacharacters– Wildcard metacharacters interpreted by shell;

regexps interpreted by text tools– Wildcard metacharacters match characters in

filenames; regexps match characters within text files– More regexps than wildcard metacharacters

• Common and extended regexps

Page 25: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 25

Regular Expressions (continued)

Table 4-4: Regular expressions

Page 26: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 26

Regular Expressions (continued)

Table 4-4 (continued): Regular expressions

Page 27: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 27

The grep Command

• grep (global regular expression print) command: Displays lines in a text file matching a given regexp

• egrep command: Displays lines of text that match extended regexps

• fgrep command: Does not interpret any regular expressions – Returns results much faster than egrep

• grep requires two arguments– Text to search for and files to search

Page 28: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 28

Editing Text Files: The vi Editor• One of the oldest and most popular text editors for

UNIX OSs• Vim: Linux equivalent of vi

– Standard on most Linux distributions• Advantage is portability, not usability

– Used on Unix and Linux• Bi-modal editor (two possible modes):

– Command mode: Performs text editing tasks not related to inserting text

– Insert mode: Inserts text, but nothing else• User environment is customizable

Page 29: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 29

Editing Text Files: The vi Editor (continued)

Table 4-5: Common keyboard keys used to change to and from insert mode

Page 30: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 30

Editing Text Files: The vi Editor (continued)

Table 4-6: Key combinations commonly used in command mode

Page 31: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 31

Editing Text Files: The vi Editor (continued)

Table 4-6 (continued): Key combinations commonly used in command mode

Page 32: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 32

Editing Text Files: The vi Editor (continued)

Table 4-6 (continued): Key combinations commonly used in command mode

Page 33: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 33

Editing Text Files: The vi Editor (continued)

Table 4-7: Key combinations commonly used at the command mode : prompt

Page 34: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 34

Other Common Text Editors

• Mcedit editor (Midnight Commander Editor): Easy-to-use text editor– Supports regexp – Supports use of mouse for highlighting text

• Emacs (Editor MACroS) editor: Comparable functionality to vi– Ctrl key combinations to perform special functions – Supports LISP (LISt Processing) artificial intelligence

programming language

Page 35: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 35

Other Common Text Editors (continued)

Table 4-8: Keyboard functions commonly used in the GNU emacs editor

Page 36: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 36

Other Common Text Editors (continued)

• Emacs editor 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

Page 37: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 37

Other Common Text Editors (continued)

Figure 4-3: The xemacs text editor

Page 38: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 38

Other Common Text Editors (continued)

• Graphical text editors available with most Linux distributions:– nedit editor– gedit editor: Distributed with GNOME environment– kedit editor: Distributed with KDE environment

• gedit and kedit offer more functionality than nedit

Page 39: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 39

Other Common Text Editors (continued)

Figure 4-4: The nedit text editor

Page 40: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 40

Other Common Text Editors (continued)

Figure 4-5: The gedit text editor

Page 41: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 41

Other Common Text Editors (continued)

Figure 4-6: The kedit text editor

Page 42: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 42

Summary• The Linux filesystem 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• Accommodates many types of files: text files,

binary data, executable programs, directories, linked files, and special device files

• The ls command is used to view filenames– Wide range of options

• Wildcard metacharacters can simplify selection of several files when using common Linux file commands

Page 43: Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 2e 43

Summary (continued)

• Text files are the most common file type whose contents can be viewed by several utilities, such as head, tail, cat, tac, more, and less

• Regular expression metacharacters can be used to specify certain patterns of text when used with certain programming languages and text tool utilities such as grep

• vi (vim) is a powerful, bimodal text editor that is standard on most UNIX and Linux systems