Tuesday 5 April 2011

Bash Cheat sheets

!!Last command
!foo Run most recent command starting with 'foo...' (ex. !ps, !mysqladmin)
!foo:p Print command that !foo would run, and add it as the latest to
command history
!$ Last 'word' of last command ('/path/to/file' in the command 'ls -lAFh
/path/to/file', '-uroot' in 'mysql -uroot')
!$:p Print word that !$ would substitute
!*o All but first word of last command ('-lAFh /path/to/file' in the command
'ls -lAFh /path/to/file', '-uroot' in 'mysql -uroot')
!*:p Print words that !* would substitute
^foo^bar Replace 'foo' in last command with 'bar', print the result, then
run. ('mysqladmni -uroot', run '^ni^in', results in 'mysqladmin -uroot')
{a,b,c}passes words to the command, substituting a, b, and c sequentially
(`cp file{,.bk}` runs `cp file file.bk`)
Ctrl + aJump to the start of the line
Ctrl + bMove back a char
Ctrl + cTerminate the command
Ctrl + dDelete from under the cursor
Ctrl + eJump to the end of the line
Ctrl + fMove forward a char
Ctrl + kDelete to EOL
Ctrl + lClear the screen
Ctrl + rSearch the history backwards
Ctrl + RSearch the history backwards with multi occurrence
Ctrl + tTranspose the current char with the previous
Ctrl + uDelete backward from cursor
Ctrl + wDelete backward a word
Ctrl + xxMove between EOL and current cursor position
Ctrl + x @Show possible hostname completions
Ctrl + zSuspend/ Stop the command
Ctrl + x; Ctrl + eEdit line into your favorite editor
Alt + >Move to the first line in the history
Alt + *lt;Move to the last line in the history
Alt + ?Show current completion list
Alt + *Insert all possible completions
Alt + /Attempt to complete filename
Alt + .Yank last argument to previous command
Alt + bMove backward
Alt + cCapitalize the word
Alt + dDelete word
Alt + fMove forward
Alt + lMake word lowercase
Alt + nSearch the history forwards non-incremental
Alt + pSearch the history backwards non-incremental
Alt + rRecall command
Alt + tTranspose the current word with the previous
Alt + uMake word uppercase
Alt + back-spaceDelete backward from cursor
(Here "2T" means Press TAB twice)
  $ 2T - All available commands(common)
  $ (string)2T - All available commands starting with (string)
  $ /2T - Entire directory structure including Hidden one
  $ (dir)2T - Only Sub Dirs inside (dir) including Hidden one
  $ *2T - Only Sub Dirs inside without Hidden one 
  $ ~2T - All Present Users on system from "/etc/passwd"
  $ $2T - All Sys variables
  $ @2T - Entries from "/etc/hosts"
  $ =2T - Output like ls or dir
  .bash_profile = sourced by login shell, 
  .bashrc = sourced by all shells, 
  .bash_aliases = should be sourced by .bashrc

Run something:
  for i in a b c; do $i 'hello'; done

Do something on a bunch of files:
  for i in *.rb; do echo $i; done

If syntax:
  if [ -e .ssh ]; then echo "hi"; fi

file check flags:
  -e:  file exists
  -f:  regular file (non directory)
  -d:  directory
  -s:  non-zero file
  -x:  execute permission

Avoid duplicates in your history:
  export HISTIGNORE="&:ls:ls *:[bf]g:exit"

No comments:

Post a Comment