Bash Commands

From Adam Colton
Jump to navigation Jump to search

Resources

Quizzes

  • Folder hierarchy concepts (ls, cd, .., ~, /)
  • Basic text manipulation
  • File manipulation
  • Output direction (>, < |)
  • Utils
  • More Text manipulation (sort, uniq, nl)
  • Processes
  • Remote
  • Sed
  • Grep
  • Awk
  • rsync
  • Scripting

Filename aliasing

  • * match 0 or more characters
  • ? match any single character
  • [abc...] match any enclosed character, hyphen to specify range
  • [!abc...] not any character
  • ~ home directory of current user
  • (a|b) pattern a or b
  • f{oo,u}bar generates foobar or fubar
    • echo {1...10} will list numbers 1 through 10
  • quoting
    • ": $, ' ", \
    • ': only '

Command Forms

  • cmd & - execute in background
  • cmd1 ; cmd2 - execute multiple commands
  • {cmd1; cmd2;} execute commands as a group in the current shell
  • (cmd2; cmd2) execute commands as a group in a subshell (I don't know what this means)
  • cmd1 | cmd2 pipe output from cmd2 as intput to cmd 2
  • cmd1 `cmd2` use cmd2 output as arguments to cmd1
  • cmd > file send output to file and overwrite
  • cmd &> file send output and errors to file and overwrite
  • cmd >> file send output to file and append
  • cmd < file take input for cmd from file
  • (date; ls) > file - All output is sent to the file
  • cmd | tee files send to standard out and files

Command History

  • !! previous command
  • !n command n
  • !-n command n ago
  • !string most recent command that starts with stirng
  • !?string[?] most recent command that contains string

Terminal Commands

  • cd
  • pwd

Special Operations

  • <
  • >
  • |
  • >>
  • tee
  • .
  • !!
  • -
  • ~
  • ..
  • !

Control Characters

Ctrl

  • A: beginning of line
  • C: terminate foreground job
  • E: end of line
  • R: Backward search in history for text in command
  • W: Erase backwards to whitespace
  • Z: Pauses foreground job
  • L: clear the screen
  • U: clears everything from the cursor to the beginning of the line
  • K: clears everything from the cursor to the end of the line
  • T: swap the last two characters before the cursor
  • B: move the cursor backward one character
  • F: forward one character
  • J or M: enter
  • N: Next in history (down arrow)
  • P: Previous in history (up arrow)
  • O: Enter the current command and put in back on the command line for further editing
  • Y: Paste from ^W, ^K or ^U like commands

Alt

  • B: back one word
  • F: forward one word
  • D: delete to end of word
  • C: capitalize letter at cursor, move to end of word
  • U: capitalize from cursor to end of word
  • L: make lowercase to end of word
  • T: swap current word with previous

Bang

  • !! : run last command
  • !blah : run last command starting with blah

Text Processing

  • tr [options] set1 [set2]
    • alnul, alpha, blank, digit, space, upper
    • lower to upper "tr a-z A-Z"
    • -d: delete, -s:squeeze(repeats the operation until no changes occur), -cs
  • uniq
    • unique a file by line
    • -c: print the number of times each line occurred
    • -i: ignore case
    • -d: print only duplicated lines
    • -u: print only unique lines
    • -w N: check only N characters
  • sort
    • -b: ignore leading blanks
    • -f: ignore case
    • -r: Reverse
    • -u: unique
  • wc
    • Shows the line, word and character count
    • -c: print only the byte count
    • -w: print only the word count
    • -l: print only the newline count
    • Example: grep FIXME *.c | wc -l count all the instance of FIXME in *.c files in the current directory
  • fold
    • Wraps lines at a given width, by default, 80
    • -s: break at word boundaries, last blank before the maximum.
    • -w WIDTH: sets width
  • nl
    • -b STYLE
      • a: number all lines
      • t: number only nonempty lines
      • n:do not number lines
      • pREGEXP: number only lines that contain a match for REGEXP
  • diff
    • Display the difference between two files or corresponding files in two directories
      • 'file1 file2'
      • 'dir1 dir2'
      • 'dir file...'
      • 'file... dir'
      • if file is '-', read standard input
    • -b: ignore changes in white spac
    • -B: ignore changes in blank lines
    • -d: Try hard to find a smaller set of changes
    • -E: ignore changes due to tab expansion
    • -i: ignore case differences
    • -l: paginate with 'pr'
    • -N: treat absent files as empty
    • -r: recursively compare sub-directories
    • -s: report when two files are the same

Other

  • ls
    • -a: do no hide entries starting with .
    • -A: same as -a,but hide . and ..
    • -c: sort by change time
    • -d: list directory entries instead of contents
    • -h: print file size in human readable format (1K, 234M)
    • -H: same as -h but use powers of 1000 not 1024
    • -l: use long listing format
    • -m: fill width with a comma separated list of entries
    • -o: use long list without group info
    • -r: reverse order while sorting
    • -R: recursive
    • -S: sort by file size
    • -t: sort by modification time
    • -u: sort by last access time
    • -v: sort by version
  • less
    • Display output one screen at a time command | less
    • Most commands can be prefixed with N to perform that operation N times
    • Moving and Jumping
      • e or j: forward one line
      • y or k: back one line
      • f or space: forward one window
      • z: back one window
      • d: forward half window
      • u: back half window
      • q or ZZ: exit
      • g or <: goto first line of the file
      • G or >: goto last line of the file
      • m: mark the current position
        • ': goto a previously marked position
        • : goto the previous position
    • Searching
      • /pattern : search forward for matching line
      • ?pattern : search backwards for matching line
      • n: repeat previous search
      • N: repeat search in opposite direction
      • &pattern : filter showing only lines that include pattern
    • Options
      • -n: use line numbers
      • -s: squeeze multiple blank lines
  • cat
    • -s: squeeze multiple blank lines
    • -n: number output lines
  • man
  • head -Count file
    • Returns Count lines from a file, if Count is not specified it defaults to 10
  • tac
    • cat in reverse
  • tail -Count file
    • prints Count lines of file, if count is not specified, it displays 10 lines
  • rev
  • echo
    • -n: Do not print the trailing newline

Power Tools

  • sed
  • grep
  • rsync
  • find

Files and Folders

  • mv
    • Move or rename a file
      • mv Source Dest
      • mv Source Directory
    • -b: make a backup of each file that would otherwise be overwritten or removed
    • -f: remove existing destination files and never prompt the user
    • -S Suffix: append suffix to each backup file made with -b
    • -u: Do not replace a file with one having an older modification time (update)
  • rm
    • Remove files
    • -r: remove contents of directories recursively
  • mkdir
    • -m: set permission mode
    • -p: make parent directories as needed
  • cp
    • Copy a file
      • cp source dest
      • cp source directory
    • -b: backup files before they are overriden
    • -f: remove existing destinations without prompt
    • -r: copy recursively, non-directories as files
    • -R: copy directories recursivly
  • rmdir
    • Removes an empty directory
  • chmod
    • Change access permissions
    • -R: change files and directories recursivly
  • du
    • Shows the size of a folder
    • -a: all files not just directories
    • -h: human readable

Utils

  • cal
    • Displays a calendar
      • cal [ [month] year ]
      • If no arguments are given, the current month is displayed
    • -m: Display monday as the first day of the week
    • -y: display a calendar for the current year
  • date
    • Display or change the date
  • time
    • Measure the running time of a program
    • time command [args]
    • -o File: writes the output to file
    • -f format: format the output

Remote

  • ssh
  • screen
  • ftp
  • ping
  • rcp
  • scp
  • wget

Processes

  • bg [PID]
    • Sends a job to the background, if no PID is specified, it's the last job run
  • fg [PID]
    • Brings a job to the foreground
  • jobs
    • lists jobs
  • kill
    • stops a process from running
  • ps
  • coproc
  • nohup
  • disown

Scripting

  • if
  • wait
  • case

Calculators

  • bc
    • Arbitrary precision calculator
  • dc
    • Desktop calculator, uses postfix notation

Other

Check a port (in the example, 22)

 netstat -ano|grep :22|grep ESTABLISHED

Zip

 zip =r dir.zip .

This will recursively zip the directory, including hidden files.