bash Basics

Improvements? Suggestions? email lif.zone.main+dna@gmail.com

Bash rescue

Tips that helps you getting started.

history: print commands history. When a veteran come to your computer and does a lot of 'black magic', or you remember that you did something somehow, and not sure exacly how, or if you did something wrong and not sure what, this will help you understand, history can come in handy.

contribution welcome!

Bash Keyboard shortcuts

Glossary

  • KEY1+KEY2: Press KEY1, add KEY2 before releasing KEY1, release both
  • KEY1, KEY2: Press KEY1, release KEY1, press KEY2, release KEY2
  • Shortcuts

    Esc, B - move one word backwards
    Esc, F - move one word forward
    Esc, Delete (backspace) - delete one word backwards
    Esc, D - delete one word forward
    Ctrl+Y - insert last deleted word
    Ctrl+A - move to beginning of line
    Ctrl+E - move to end of line
    Ctrl+U - kill back to beginning of line
    Ctrl+K - kill forward to end of line

    Esc, / - complete as filename
    Ctrl+X, / - list possible filename

    Esc, ~ - complete as user
    Ctrl+X, ~ :- list possible user

    Esc, $ - complete as variable
    Ctrl+X, $ :- list possible variable

    Esc, @ - complete as hostname
    Ctrl+X, @ :- list possible hostname

    Esc, ! - complete as command
    Ctrl+X, ! :- list possible command

    Esc, C - capitalize word
    Esc, U - all cap (upper-case) word
    Esc, L - all low-case word
    ESC, . - insert last killed word

    Ctrl+L(ell) - clear screen (not in CRT)
    Ctrl+T - transpose char ("swap")
    Ctrl+V - quoted insert (next char is "quoted")

    Alt+. - retrieve last word from the previous executed command
    Esc, . - retrieve last word from the previous executed command

    Bash useful techniques

    Search for text in all files in the current directory and below

    (this example searches for all .c files for the text my_func)
    find . | grep [.]c | xargs grep my_func
    or better:
    find . -name "*.c" | xargs grep my_func
    or:
    find . -name "*.c" -exec grep -Hn my_func {} ;

    Searching for my_func in files that might include a space in the name

    find . -printf ""%p" " | xargs grep my_func

    Sending all files to a utility one by one

    edit a file run_dos2unix:
    for filename in "$@" ; do dos2unix $filename ; done
    execute in the bash:
    $ chmod +x run_dos2unix
    $ find . -name "*.c" | xargs ./run_dos2unix

    Sending all files to a utility one by one, but in one line

    find . -name '*.c' | xargs -n 1 dos2unix

    Defining a "workset" of files you usually work on

    edit file with names of files you work on, one on a line.
    then run gvim `cat workset` to load them all into gvim.

    Capture all terminal output into file using script

    $ script my_output_file
    :
    : some commands & stuff here
    : (e.g., make)
    :
    $ exit
    now all the output you saw between script and exit is in my_output_file