$30 off During Our Annual Pro Sale. View Details »

Time-saving tricks on the command line

Time-saving tricks on the command line

A few simple but very effective practical tips that should make you lightning fast on the command line. I use these literally every minute I spend in the shell. All the tips should work in Linux, UNIX, BSD and similar.
http://www.janosgyerik.com/practical-tips-tricks-in-the-linux-shell/

Janos Gyerik

May 13, 2013
Tweet

More Decks by Janos Gyerik

Other Decks in Technology

Transcript

  1. time-saving tricks
    on the command line

    View Slide

  2. $ cd /usual/path/to/work/on/projectx
    $ screen -R projectx
    $ tail -f my.log
    ctrl-a c
    $ less config.sh
    gimme a new window
    Hm... what's in config.sh?
    Gimme a new window!

    View Slide

  3. gimme a new window
    gimme a new window
    gimme a new window
    gimme a new window
    gimme a new window
    $ tail -f /some/log
    ctrl-a c
    $ less /some/file
    ctrl-a c
    $ man some_doc
    ctrl-a c
    gimme another window
    ctrl-a is the command key
    c stands for create window

    View Slide

  4. ctrl-a n
    ctrl-a p
    ctrl-a w
    Next window
    Previous window
    Window list
    next prev window
    C-a p C-a n
    C-a p C-a n

    View Slide

  5. detach/reattach
    I want to get out!
    I want to go back!
    Terminal crashed!
    My sessions?
    # detach
    ctrl-a d
    # reattach
    $ screen -R label
    # detach and reattach
    $ screen -R label -D
    $ screen -ls

    View Slide

  6. ctrl-a ?

    View Slide

  7. search history with ctrl-r
    ...I want that for loop I
    used a while ago...
    ctrl-r and type "for"
    ctrl-r again...
    ctrl-r again...
    $ history | grep for
    362 for i in *; do mv "$i" "
    -t us-ascii//translit)" -v; don
    364 for i in *; do mv "$i" "
    us-ascii//translit)" -v; done
    (reverse-i-search)
    `for': history |grep
    for
    (reverse-i-search)`for': for i in *; do
    mv "$i" "$(echo $i | iconv -f utf-8 -t
    us-ascii//translit)" -v; done
    (reverse-i-search)`for': for i in *; do
    mv "$i" "$(echo $i | iconv -f iso8859-1
    -t us-ascii//translit)" -v; done

    View Slide

  8. ctrl-w
    ctrl-k
    ctrl-y
    ctrl-c
    Delete last word
    Delete until line end
    Paste deleted stuff
    Cancel entire line
    editing quickly

    View Slide

  9. ctrl-a
    ctrl-e
    Jump to line start
    Jump to line end
    moving quickly
    ctrl-a a inside screen

    View Slide

  10. man bash
    Search for: "READLINE"
    "Commands for Moving"
    "Commands for Changing Text"

    View Slide

  11. /error
    n
    N
    g
    G
    Search for "error"
    Next match
    Previous match
    Jump to first line
    Jump to last line
    moving quickly in less

    View Slide

  12. m a
    ' a
    -i
    :n
    Mark line in register "a", "b"
    Jump to register "a", "b"
    Toggle case sensitive search
    Go to next file
    moving quickly in less
    m b
    ' b

    View Slide

  13. h

    View Slide

  14. mail it to me
    # mail me the relevant stuff
    $ grep stuff /var/log/messages | mailx -s
    "stuff from logs" [email protected]
    # mail me the whole file
    $ uuencode /var/log/messages messages.
    txt | mailx -s "system logs on $HOST"
    [email protected]

    View Slide

  15. # after the loooong task, mail me
    $ rsync --progress /disk1/iso/*
    /disk2/iso/; ls -lha /disk2/iso/ |
    mailx -s "copy isos done"
    [email protected]
    mail me when finished

    View Slide

  16. # infinite loop until file appears and then mail me
    $ while :; do date; test -f file.txt &&
    break; sleep 300; done; ls -lh file.
    txt | mailx -s "file appeared"
    [email protected]
    mail me when file appears

    View Slide

  17. # infinite loop until matching line appears and then mail me
    $ while :; do date; grep pattern
    /path/to/log && break; sleep 300;
    done; { date; grep -B50 -A50
    pattern /path/to/log; } | mailx -s
    "log snippet" [email protected]
    mail me when log matches

    View Slide

  18. $ openssl des3 < secret.bin > secret.bin.des3
    enter des-ede3-cbc encryption password:
    Verifying - enter des-ede3-cbc encryption password:
    $ openssl des3 -d < secret.bin.des3 > decrypted.bin
    enter des-ede3-cbc decryption password:
    $ cmp secret.bin decrypted.bin
    $
    # empty output means the two files are identical
    encrypt and decrypt simply

    View Slide