Skip to content

Bash

Keyboard Shortcuts

Notes: These are the default keyboard assignments for the default command-line editting mode, which is emacs mode.

If you’re familiar with the vi editor, you can change this to use all the cursor movement and deletion keys you’re familiar with by saying set -o vi to your shell.

From maymay.net

Key Info
CTRL + a Move cursor to beginning of the line. (I think of this as anchoring my cursor to the start.)
CTRL + e Move cursor to the end of the line.
CTRL + k Delete everything from under the cursor to the end of the line. (I think of this as killing the rest of my line.)
CTRL + f Move forward one character. Identical to →.
CTRL + b Move backward one character. Identical to ←.
ESC + f Move forward one word.
ESC + b Move backward one word.
CTRL + u Delete everything from under the cursor the beginning of the line. (recall line deleted by CTRL + U)
CTRL + w Delete from under the cursor to the beginning of the word.
CTRL + r Recall previous commands by searching for them.
CTRL + t Transpose (swap) the two characters before the cursor with one another.
ESC + t Transpose (swap) the two words before the cursor with one another.
CTRL + y Paste the most previously-deleted string. Basically a sort of command-line editting “undo.”
CTRL + z Stop the current process and send it to the background.
CTRL + c (Cancel) Send an SIG_HUP to the current process. The net effect of doing this on the command line is that you cancel your current command and are presented with a blank new line.
CTRL + d Send an end-of-file special character to the current process. Doing this at the command line is identical to closing your terminal window.
CTRL + p Recall previous command executed. Identical to ↑.
CTRL + d Forward delete.
CTRL + h Backspace.
CTRL + j Carriage return. Identical to hitting the return key.
CTRL + m Newline. Identical to return.
CTRL + l Repaint screen. This is useful if a program’s output is overwriting some text on your terminal. The effect of doing this on a command line is that you clear the screen. Note than in Apple’s Terminal.app, you can also press +K to clear the screen.
CTRL + x + CTRL + x Mark current location in line and jump to beginning of line or second mark if defined. Repeat to jump to between both marks.
CTRL + v Insert next character verbatim. This is how you escape control sequences. For instance, to literally send a
CTRL + [ Identical to ESC.
ESC + c Capitolize word under cursor and move to next word.
ESC + u Uppercase word under cursor and move to next word.
ESC + l Lowercase word under cursor and move to next word.
ESC + . Insert last word from previous command after cursor.
TAB Auto-completes file, folder, and program names.
ESC + ? List the possible completions
CTRL + x / List the possible filename completions
ESC + / Attempt filename completion
CTRL + x ~ List the possible variable completions
ESC + ~ Attempt username completion
CTRL + x $ List the possible variable completions
ESC + $ Attempt variable completion
CTRL + x @ List the possible hostname completion
ESC + @ Attempt hostname completion
CTRL + x ! List the possible command completions
ESC + ! Attempt command completion
ESC + TAB Attempt completion from previous commands in the history list
ALT + SHIFT = # Turn line into a comment

Some Command-line Examples

ls List items

Command Info
ls -l List items in directory showing symlinks' locations
(with "SYMLINK -> LOCATION")
ls -d */ List directories only. Info here[^1] and here[^2]
ls a* List all files that start with a. Info here[^2]
ls -l a* long listing of each file whose name starts with a, and of the
contents of each directory whose name starts with a
ls -ld a* list of the files and directories, one line for each

[^1] Info from StackOverflow here : https://stackoverflow.com/questions/14352290/listing-only-directories-using-ls-in-bash [^2] More info on the * here : https://unix.stackexchange.com/questions/75046/why-does-ls-d-also-list-files-and-where-is-it-documented/75047#75047

Copying Files

cp

cp -Rfv SOURCEFILE DESTINATIONFILE

Command Info
cp copy
R maintains file hierarchies
f if an existing destination file cannot be opened, remove it and try again
v verbose mode, displays files transferred as it progresses
sourcefile data you want to copy
destinationfile directory/drive you want to copy to

dd

dd: copy from if input file to of output file
dd if=/path/to/image.img of=/dev/rdisk4 bs=1m

rsync

rsync example: clone folder (e.g. clone android SD card)

Back up your existing SD card:
mkdir ~/Documents/sdbackup
rsync -avz "/Volumes/VOL_NAME/" ~/Documents/sdbackup

To bring them back to the new card:
rsync -avz ~/Documents/sdbackup/ "/Volumes/VOL_NAME"

Links - rsync - cp - ditto

cat

  1. the contents of file1 replaces the contents of file2 (file2 now has the same contents as file1)

    cat file1.txt > file2.txt

  2. the contents of file1 are APPENDED to the contents of file2
    (file2 now includes its original contents plus the contents of file1 at the end)

    cat file1.txt >> file2.txt

  3. the contents of file1 are printed to the terminal
    (file1 is taken as standard input for the "cat" command)

    cat < file1.txt

grep

grep stands for "global regular expression print".
It searches files for lines that match a pattern and returns the results.
It is also case sensitive.

Command Info
grep -i enables the command to be case insensitive
grep can also be used to search within a directory
grep -R searches all files in a directory and outputs filenames and
lines containing matched results. -R stands for "recursive".
grep -Rl searches all files in a directory and outputs only filenames
with matched results. -R stands for "recursive" and l stands for "files with matches".

Other

  1. echo to create / append to files

    1. CREATE a file called 'textfile.txt' and put 'textpart1' in as content

      echo textpart1 > textfile.txt

    2. OVERWRITE the file called 'textfile.txt' with 'textpart2' in as content

      echo textpart2 > textfile.txt

    3. APPEND 'textpart2' to the file called 'textfile.txt'

      echo textpart2 >> textfile.txt

  2. | is a "pipe". The | takes the standard output of the command on the left, and pipes it as standard input to the command on the right.
    You can think of this as "command to command" redirection. Multiple |s can be chained together.

  3. the contents of file are piped to sort
    then the output of the sort command are redirected to sorted-file.txt and sorted-file.txt is saved with the contents output from the sort commmand
    cat file.txt | sort > sorted-file.txt

  4. the uniq command output filters out adjacent, duplicate lines in a file (i.e. does not print out duplicates)
    uniq file1.txt
    sort file1.txt | uniq
    sort file1.txt | uniq > uniq-file1.txt

  5. A more effective way to call uniq is to call sort to alphabetize a file, and "pipe" the standard output to uniq.

    sort fil1.txt | uniq > uniq-file1.txt

  6. sed stands for "stream editor". It accepts standard input and modifies it based on an expression, before displaying it as output data.
    It is similar to "find and replace".
    In the following, sed substitutes (the s tells it this) 'string1' with 'string2' from the output of the file file.txt IMP: this will only replace the first instance of 'string1' on a line.
    sed 's/string1/string2/' file.txt
    To change all instances of 'string1' use the global flag:
    sed 's/string1/string2/g' file.txt

  7. export makes the variable to be available to all child sessions initiated from the session you are in. This is a way to make the variable persist across programs

  8. diskutil list

  9. diskutil unmountDisk /dev/disk4

  10. symlinks - create a link:
    ln -s oldfile newfile
    syntax: ln -s <source_folder> <target_link_folder>
    links the previously existing oldfile to the newly created link, newfile
    example:
    ln -s /Users/ska/Sites ~/Dropbox/dev/codebits/mscGAEapp

  11. symlinks - remove a link:
    either: remove/delete the file/folder using rm or rmdir
    or: unlink ~/Dropbox/dev/codebits/mscGAEapp

  12. test if file exists and load (e.g. aliases file in .bashrc)
    sh if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
    or:
    test -f ~/dotfiles/.aliases && source ~/dotfiles/.aliases

  13. Disk Utility source:

    defaults write com.apple.DiskUtility advanced-image-options 1 defaults write com.apple.DiskUtility DUDebugMenuEnabled 1

  14. List all the symlinks in the directory (and subdirs)

    find . -type l

  15. shows location of symlink THIS ONLY WORKS FROM THE DIRECTORY THE SYMLINK IS LOCATED IN

    readlink LINK