Version User Scope of changes
May 19 2008, 4:05 PM EDT (current) Dr.Disk 3 photos deleted
May 19 2008, 4:03 PM EDT Dr.Disk 511 words added, 6 photos added

Changes

Key:  Additions   Deletions

Section 3: The Linux Filesystem - Certification CentralTarget Goals

  • Locate files
  • Create linked files
  • Create and edit files
  • Back up and restore files

Section 3: The Linux Filesystem - Certification CentralLocating Files
The Find Command
find [search_locations] [search criteria] [actions] - used to find locate in specified locations based on conditions provided.
Once files are found additional actions may be performed

Section 3: The Linux Filesystem - Certification CentralMultiple search paths may be used can include directory notations.

Section 3: The Linux Filesystem - Certification CentralBecause find will search the directory and subdirectories the file or directory must meet all specified criteria before being reported.

Find Options
Condition Search by Result
-name File or Directory name Pattern can be full name or "" and metacharacters can be used to define sections of name
-type File, Directory, other d = directory
f = file
often combined with name
-user Ownership argument = user name
-group Group Membership argument = group name

Section 3: The Linux Filesystem - Certification Central\! = notates "not" and is used to create exception when placed before search criteria

Options once a file is located
Switch Result
-print Print to screen
-exec Execute command
-ok Execute interactively

Locate Command
locate - used to find data files, programs, directories and objects matching keywords against a database named slocate.db. locate should be updated prior to being run by logging in as root and running updatedb or slocate -u. locate only searches by name use find to search by attribute and whereis
to search for commands

Grep

grep allows you to search contents of files.

Real World Examplegrep foo test will show all lines that match foo in the file test.
Using ls -l | grep ball will return all files within the current directory containing the word ball.

Section 3: The Linux Filesystem - Certification CentralLinking Files
Linking files in Linux is like making shortcuts in windows.

ln - allows a file name in one directory to point to a file name in another directory

Symbolic vs. Hard Links

Remember a link does not contain data but acts as a pointer.

Symbolic Link - allows a single file or directory to be accessible via multiple file names.

Hard Link - Looks like a real file to all applications.(it is more like a copy). Even if original deleted contents still available via linked file. Links may only be created on the same partition and cannot be created for directories.

Section 3: The Linux Filesystem - Certification CentralCreate and Edit Files

Text Editors

Vi
Included in most versions of UNIX and Linux used for program development but can become a letter-quality word processor. Use Vi to create shell scripts.

Open the Vi editor using the vi command. This will open an unnamed file.

Command Mode - used to save, edit, search, replace and other "administrative" tasks

Input mode- type text into vi file. No commands are available

Changing Modes (Input and Append)

ModeFunction
iInput mode; inserting text to left of cursor
aAppend mode; input text to right of cursor
lInsert mode; input text at beginning of line
AAppend mode; input text at end of line
ESCReturn to command mode

Replacing Text

ModeFunction
rReplaces one character
RReplaces characters until you press ESC
cw or ceDeletes from cursor to beginning of word and puts you in insert mode, ESC returns to command mode
cbDeletes from cursor to beginning of word and puts you in insert mode, ESC returns to command mode
c$ or UDeletes rest of line and puts you in insert mode, ESC returns to command mode

Copying and Deleting
ModeFunction
xDeletes One Character at a time
yyPlaces a copy of current line in buffer
ddDeletes curent line and places in buffer
pInserts copied or deleted line into file below line of cursor
PInserts copied or deleted line into file above line of cursor

Undo
Return to command mode using Esc and press U to undo changes. This removes changes one by one each time you press U.

Save and Exit Vi
Saving from command mode :w [filename]

:q - used to quit Vi once file is saved
:q! - forces Vi closed without saving

Vi Edit commands
TypeResult
gmove left one character
jmove down one line
kmove up one line
lmove right one character
^Move to beginning of current line
$Move to last line in file
:[number]Move to line specified by number
wnext word
bprevious word
emove to end of current word or next word if already at the end of a word
Ctrl+UMove up 1/2 screen
Ctrl+DMove down 1/2 screen
Ctrl+FMove down one full screen
Ctrl+BMove up one full screen

Stream Editor
sed - modifies lines from a specified file by command or script and writes results to display

Linux isn't awkward but awk is!
/[pattern]/ searches for specific pattern or entry in files. Once the first occurance is found N will start the search for the next.
! (NOT), && (AND), and || (OR) will help narrow and widen searches

Output Redirection
Real World Examplels >list tells shell to output command results to the file list. It will create the file if it doesn't exist

Input Redirection
< informs shell to redifine input as a non-keyboard device, usually a file.

Section 3: The Linux Filesystem - Certification CentralBack Up and Restore Files

Archiving
tar - allows you to archive files. (-c = creating an archive -f = create a file with the name that follows)
The destination is the location and name of the file; the source is the location and name of the file or directory to use to create the archive.

Tape archiving to and from remote media is most common but you can create local backups as well.

Section 3: The Linux Filesystem - Certification Central-r is used to append or update a tar file. If source is new it gets appended. New versions will overwrite.
Section 3: The Linux Filesystem - Certification Central-v places you in verbose mode (will also list names of files being archieved or copied)
Section 3: The Linux Filesystem - Certification Centraltar -tvf [tarfilename] - -t lists contents of tar file. -v long listing of contents -f with [filename] specifies the file.

Real World ExampleBacking up directories uses the same command as backing up files

cpio (no it's not R2D2's friend) is a copy in-out program similar to tar. Can archieve in amnay formats including tar. Can operate in the following three modes.

Copy-out : used to create an archieve. List of files read from source and written to either specified or standard output.
Sources may include a text file, the outpu of the find command, output from the ls command or other sources listing files one line at a time.
Format: source | cpio -ocBv > [archive_filename]

Copy-in: used to extract files from archive. By default all files are copied from archive. You can specify using metacharacter patterns.
Format: cpio -icdv < archieve_filename

Copy-Pass: Copy files from one directory tree to another combining the copy-out and copy-in modes without using an archive
SwitchEffect
oCreate an archive using copy-out
BChange block size to 5120 from 512 default
cspecify ASCII archive format
vList files processed
tPrint list of input

Interactive Archiving
tar -wcvf [destination] [source] - before adding a file to the archive the user is prompted

Using other utilities to archive
compress - will reduce the size of a file by 40-50% and add a .Z to the file as an indication it has been compressed.
-v will estimate the amount saved by compression.

gzip will append a .gz extension and will shrink single or multiple files into a single flle whose size is lower than using compress.
gzip is used to unzip gzipped files
zcat is a utility used to view files in their natural state.

Bring it Back!
tar -xvf restores the entire contents of the source file or directory structure. You can restore portions of the tar file by indicating the path and file name you wish to extract.

copy-in recovery cpio -icdv < [archieve_filename]
-i
extracts files using copy-out mode
d
creates directories as necessary
tar -wxvf [destination] [source]
restores interactively

Section 3: The Linux Filesystem - Certification CentralYou should always be root or equivalent to perform backups

If you are not root or equivalent you may see "tar: tarfilename: Permission Denied"
'Tar: Error is not recoverable: exiting now" is a catch-all that can mean anything from the file is corrupt to the tape drive is damaged"