43
Linux+ Guide to Linux Certification, Third Edition Chapter 3 Exploring Linux Filesystems

Linux+ Guide to Linux Certification, Third Edition Chapter 3 Exploring Linux Filesystems

Embed Size (px)

Citation preview

Linux+ Guide to Linux Certification, Third Edition

Chapter 3Exploring Linux Filesystems

Linux+ Guide to Linux Certification, 3e 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

Linux+ Guide to Linux Certification, 3e 3

Objectives (continued)

• 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

• Identify common alternatives to the vi text editor used today

Linux+ Guide to Linux Certification, 3e 4

The Linux Directory Structure

• Directory: Used to organize other files into a logical tree structure– Stored in a filesystem of a specific partition in the

hard disk

• 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

Linux+ Guide to Linux Certification, 3e 5

The Linux Directory Structure (continued)

Figure 3-1: The Windows filesystem structure

Figure 3-2: The Linux filesystem structure

Linux+ Guide to Linux Certification, 3e 6

Changing Directories

• Home directory: unique to each user– ~ metacharacter used to refer to home directory

• 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

• Relative pathname: pathname of file or directory relative to current directory

Changing Directories (continued)

• Parent directory: directory one step closer to the root of the tree– Referred to by .. (two dots)

• 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– Alerts user if there is more than one possible match

Linux+ Guide to Linux Certification, 3e 7

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 placeholders to organize other files

Linux+ Guide to Linux Certification, 3e 8

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

Linux+ Guide to Linux Certification, 3e 9

Linux+ Guide to Linux Certification, 3e 10

Filenames

• Filename: identifier given to a file– Up to 255 characters– Can use alphanumeric characters, dash (-),

underscore (_), and dot (.)

• Filename extensions: identifiers following a dot (.) at end of filename – Denote file type

– Most files on Linux do not have filename extensions

Linux+ Guide to Linux Certification, 3e 11

Table 3-1: Common filename extensions

Linux+ Guide to Linux Certification, 3e 12

Listing Files

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

listed• –F option: Argument to indicate file types

• –l option: Argument to list long file listings

Listing Files (continued)

• Long listing for each file includes eight components– File type character– List of permissions (mode of the file)– Hard link count– Owner– Group owner– File size– Most recent modification time– Filename

Linux+ Guide to Linux Certification, 3e 13

Listing Files (continued)

• Alias: shortcut for a command– ll command: Alias for ls -l

• File command: displays file type of any file– Argument indicates what file or files to analyze– Identifies between different types of executable files– Identifies empty files

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

Linux+ Guide to Linux Certification, 3e 14

Linux+ Guide to Linux Certification, 3e 15

Listing Files (continued)

Table 3-2: Common options to the ls command

Linux+ Guide to Linux Certification, 3e 16

Listing Files (continued)

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

Linux+ Guide to Linux Certification, 3e 17

Wildcard Metacharacters

• Wildcard metacharacter: used to simplify commands specifying multiple filenames– Can match the entire filename or portions of

filenames– Can be used with most Linux filesystem commands

Linux+ Guide to Linux Certification, 3e 18

Wildcard Metacharacters (continued)

Table 3-3: Wildcard metacharacters

Linux+ Guide to Linux Certification, 3e 19

Displaying Content of Text Files

• Concatenation: joining text together

• cat command: displays (concatenates) contents of a text file to the screen– -n option: displays line number and contents

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

• tac command: displays contents of a text file in reverse order

Displaying Content of Text Files (Continued)

• head command: view first ten lines of a file

• tail command: view last ten lines of a file

• For head and tail commands– Line count includes blank lines– Can provide numeric option to specify the number of

lines to be displayed (e.g., head -2 filename)

Linux+ Guide to Linux Certification, 3e 20

Linux+ Guide to Linux Certification, 3e 21

Displaying Content of Text Files (continued)

• Large text files can not be viewed using the cat command, because the screen will only fit a portion of the file

• more command: displays text files page-by-page– Pressing Spacebar displays the next page– Pressing Enter displays the next line

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

Linux+ Guide to Linux Certification, 3e 22

Displaying Content of Text Files (continued)

• Users can use keyboard shortcuts to interact with shell while in more and less commands. – e.g., pressing h key gets Help screen– e.g., pressing q key quits more and less commands

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

metacharacter and more or less command– e.g., ls -l | more

Linux+ Guide to Linux Certification, 3e 23

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)– -x option displays contents of the file in hexadecimal

format (numeric base 16 format)

Linux+ Guide to Linux Certification, 3e 24

Searching for Text Within Files

• Text tools: commands that search for and manipulate text

• Regular expressions (regexp): text wildcards that ease the search for specific text– Match patterns of text within a text document– Used by many text tools and programming

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

Linux+ Guide to Linux Certification, 3e 25

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– Wildcard metacharacters have different definitions

that regexps– More regexps than wildcard metacharacters

• Regular expressions are divided into common regexps and extended regexps

Linux+ Guide to Linux Certification, 3e 26

Table 3-4: Regular expressions

Linux+ Guide to Linux Certification, 3e 27

The grep Command

• grep (global regular expression print) command: displays lines in a text file that match common regexps

• egrep command: displays lines in a text file that match extended regexps– Can be written as grep -E

• fgrep command: does not interpret any regular expressions – Returns results much faster than egrep– Can be written as grep -F

The grep Command (continued)

• grep requires two arguments– Text to search for

• Can use regular expressions

– Files in which to search

• grep is case sensitive– For case-insensitive search, use –i option

• grep matches patterns of text, ignoring division into words– To search only for occurrences of a word, surround it

by space charactersLinux+ Guide to Linux Certification, 3e 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

Linux+ Guide to Linux Certification, 3e 29

Linux+ Guide to Linux Certification, 3e 30

Editing Text Files: The vi Editor (continued)

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

Linux+ Guide to Linux Certification, 3e 31

Editing Text Files: The vi Editor (continued)

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

Linux+ Guide to Linux Certification, 3e 32

Editing Text Files: The vi Editor (continued)

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

Linux+ Guide to Linux Certification, 3e 33

Editing Text Files: The vi Editor (continued)

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

Linux+ Guide to Linux Certification, 3e 34

Editing Text Files: The vi Editor (continued)

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

Linux+ Guide to Linux Certification, 3e 35

Other Common Text Editors

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

programming language

• Emacs editor is not easy to use – Must memorize key combination

• Emacs can be run in a GUI environment to get a graphical version of the editor– Much easier to use; icons replace key combinations

Linux+ Guide to Linux Certification, 3e 36

Other Common Text Editors (continued)

Table 3-8: Keyboard functions commonly used in the GNU Emacs editor

Linux+ Guide to Linux Certification, 3e 37

Other Common Text Editors (continued)

Figure 3-3: A graphical Emacs session

Other Common Text Editors (continued)

• Nano editor: text editor that uses Ctrl key combinations for performing functions– Based on the pine UNIX editor– Ctrl key combinations listed at the bottom of the

screen

• Very basic and easy-to-use– Used by Linux administrators to modify files when

advanced functionality is not needed

Linux+ Guide to Linux Certification, 3e 38

Linux+ Guide to Linux Certification, 3e 39

Other Common Text Editors (continued)

• gedit editor: a graphical text editor functional in a GUI environment– Does not have advanced functionality like vi and

Emacs– Easiest editor to use– Functionality is analogous to the Windows Wordpad

and Notepad editors

Linux+ Guide to Linux Certification, 3e 40

Other Common Text Editors (continued)

Figure 3-4: The gedit text editor

Summary

• The Linux filesystem is arranged hierarchically using a series of directories to store files

• Location of directories and files can be described using absolute or relative pathnames

• Linux filesystem can contain 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 to modify views

Linux+ Guide to Linux Certification, 3e 41

Summary (continued)

• Wildcard metacharacters are special keyboard characters– can simplify selection of several files when using

common Linux file commands• 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

Linux+ Guide to Linux Certification, 3e 42

Linux+ Guide to Linux Certification, 3e 43

Summary (continued)

• Regular expression metacharacters can be used to specify certain patterns of text – 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