Target Goals
- Locate files
- Create linked files
- Create and edit files
- Back up and restore files
Locating 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

Multiple search paths may be used can include directory notations.
Because 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 |
\! = 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
Grepgrep allows you to search contents of files.
grep 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.
Linking 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.
Create 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)
| Mode | Function |
| i | Input mode; inserting text to left of cursor |
| a | Append mode; input text to right of cursor |
| l | Insert mode; input text at beginning of line |
| A | Append mode; input text at end of line |
| ESC | Return to command mode |
Replacing Text
| Mode | Function |
| r | Replaces one character |
| R | Replaces characters until you press ESC |
| cw or ce | Deletes from cursor to beginning of word and puts you in insert mode, ESC returns to command mode |
| cb | Deletes from cursor to beginning of word and puts you in insert mode, ESC returns to command mode |
| c$ or U | Deletes rest of line and puts you in insert mode, ESC returns to command mode |
Copying and Deleting
| Mode | Function |
| x | Deletes One Character at a time |
| yy | Places a copy of current line in buffer |
| dd | Deletes curent line and places in buffer |
| p | Inserts copied or deleted line into file below line of cursor |
| P | Inserts 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
| Type | Result |
| g | move left one character |
| j | move down one line |
| k | move up one line |
| l | move right one character |
| ^ | Move to beginning of current line |
| $ | Move to last line in file |
| :[number] | Move to line specified by number |
| w | next word |
| b | previous word |
| e | move to end of current word or next word if already at the end of a word |
| Ctrl+U | Move up 1/2 screen |
| Ctrl+D | Move down 1/2 screen |
| Ctrl+F | Move down one full screen |
| Ctrl+B | Move 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
ls >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.
Back 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.
-r is used to append or update a tar file. If source is new it gets appended. New versions will overwrite.
-v places you in verbose mode (will also list names of files being archieved or copied)
tar -tvf [tarfilename] - -t lists contents of tar file. -v long listing of contents -f with [filename] specifies the file.

Backing 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
| Switch | Effect |
| o | Create an archive using copy-out |
| B | Change block size to 5120 from 512 default |
| c | specify ASCII archive format |
| v | List files processed |
| t | Print 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
You 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"