Lecture Two

File System; Common Commands; The vi Editor

File System

  • Unix file system is hierarchical (tree, directory within directory)
  • root directory is / - ancestor of all files and directories on system
  • directories "contain" files and/or other directories
    • directories actually only contain filenames and pointers to inodes (information about the files)
  • to be safe, for file and directory names use alphabetics, numerics, underscore, period, or comma
  • some UNIX systems allow up to 255 characters in a filename, others only allow up to 14
  • files within one directory must have different names
  • hidden filenames begin with a period, ls won't list them, but ls -a will
  • hidden filenames are used mostly for configuration files, that you don't want to see every time you issue an ls command
  • . represents the current working directory, .. represents its parent
  • home directory is the working directory upon log in, or after the cd command is issued with no arguments

Standard Directories

  • standard directories & files
    • /home typically contains the home directories of all users
    • /bin and /usr/bin contain standard utility programs
    • /sbin and /usr/sbin contain utilities for system administration
    • /etc contains admin & configuration files, such as /etc/passwd
    • /var contains files that vary as the system is running, such as temp, log, spool, and mailbox files
    • /dev contains files representing peripheral devices (device drivers)
    • /tmp is used by some programs for temporary files

File Types

  • file types, as indicated by the first character in ls -l output:
    • - indicates an ordinary file
    • d indicates a directory
    • l indicates a symbolic link
    • b indicates a block device file, such as for a dvd reader
    • c indicates a character device file, such as for a keyboard
    • p indicates a pipe, used to communicate between processes on the same server
    • s indicates a socket, used to communicate between processes on the same or different servers

Common Commands

  • touch - creates a new file, or updates stats on an existing file
  • mkdir - to create directories
    • mkdir -p - to create a directory & any parent directories not already existing
  • rmdir - to delete empty directories, eg. rmdir directory-list
  • mv existing-file new-file - to rename or move files & directories
  • cp source-file destination-file - to copy files (careful, overwrites destination with no warning)
    • cp -r - to copy directories including files and subdirectories
  • rm - to delete files, eg. rm file-list
    • rm -r - to delete directories including files and subdirectories
    • rm -ir - to delete directories including files and subdirectories, with prompt to confirm removal
  • cat filename - display contents of file
  • more or less - displays file contents one page at a time
    • - will go to next page
    • b - will go to previous page
    • /string - will search for string within document being viewed
    • q - will quit
  • file filename - gives info about the contents of the file
  • find - to find files matching specified characteristics
    • find . -name file* - lists pathname of any filenames beginning with "file", from the current directory and any subdirectories
    • find . -size +50k - lists pathname of any files larger than 50 kb, from the current directory and any subdirectories
  • man command - online manual (or help) for command, uses more to display information
    • man -k keyword - eg. man -k calendar - searches through man sections for keyword
  • diff file1 file2 - displays differences between 2 files
  • echo text or $variable (eg. $HOME)
  • which utility - lists pathname that would be used to access this utility

The vi Editor

Modes

  • input mode, command mode, or last-line mode (special case of command mode)
  • to get to input mode:
    • insert - i, I (before cursor, beginning of line)
    • append - a, A (after cursor, end of line)
    • open - o, O (below, above)
  • escape [ESC] to get to command mode
  • basically, if typing doesn't appear on screen, vi is in command mode, if typing appears on screen, vi is in input mode

Cursor Movement

  • cursor moved using h, j, k, l (left, down, up, right)
    • can use repetition factor, eg. 12j will move down 12 lines
    • arrow keys may also be used, but using h, j, k, l is faster
  • ^f, ^b- scroll forward full screen, backward full screen
  • nG - move to line n, or last line if n not specified

Common Commands

  • J - join current and following line
  • x - delete character under cursor
    • can use repetition factor, eg. 3x
  • r - replace character under cursor
  • dw, d$, dd- delete text (word, to end of line, line) and copy to buffer
    • can use repetition factor, eg. 7dw or d7w, 12dd or d12d
  • cw, c$, cc - change - delete text, copy text to buffer, and leave in input mode
    • can use repetition factor, eg. 5cw or c5w, 12cc or c12c
  • yw, y$, yy - yank - copy text to buffer
    • can use repetition factor eg. 5yw or y5w, 12yy or y12y
  • p, P - paste buffer after or before cursor (below or above current line if buffer contains entire lines)
  • . - repeat most recent command that made a change
  • u- undo (only one level on older systems)
  • :wq - write changes to disk and quit
  • :q! (quit without saving changes)

Searching

  • /pattern - searches for the next occurrence of pattern after the current cursor position
  • ?pattern - searches backwards through the file
  • n - repeat last search