BASH Cheat Sheet

Cheater!
So, I like bash(or BaSH or BaSh or whatever). It’s powerful, and it’s Fun! Throughout the years I have picked up some fun shortcuts for the CLI.
This is a running list of some of my favorites (and some which I don’t really use). Disclaimer: this stuff was found on the Internet!

# Bash Cheat Sheet
Here are my most commonly used shortcuts while on the CLI:

Ctrl + a – Jump to the start of the line
Ctrl + d – Delete from under the cursor
Ctrl + e – Jump to the end of the line
Ctrl + u – Delete backward from cursor
Ctrl + w – Delete backward a word

And look at this little gem. How may times have you gone to make a backup copy of a file and had to type the entire filename twice?(Tab completion and copy/paste also)?
This little bash shortcut will save you lots of time when making backup copies of config files:

{a,b,c} passes words to the command, substituting a, b, and c sequentially
(`cp file{,.bak}` runs `cp file file.bak`)

More detailed example:
To cp or mv a file while adding an extention, use ” {,.old} ”

Example:
[root@MSSDCOVS06 centos65minBase]# ls -la
t-rw-r–r– 1 root root 1062 Jun 6 16:28 MSPODLOG02.xen-cfg

[root@MSSDCOVS06 centos65minBase]# cp MSPODLOG02.xen-cfg{,.old}

[root@MSSDCOVS06 centos65minBase]# ls -la
-rw-r–r– 1 root root 1062 Jun 6 16:28 MSPODLOG02.xen-cfg
-rw-r–r– 1 root root 1062 Jun 6 16:41 MSPODLOG02.xen-cfg.old

[root@MSSDCOVS06 centos65minBase]#

Neato!


Now what if you want to create multiple directories containing multiple sub-directories? A good friend and coder at work turned me on this nested “mkdir -p” command:

#> mkdir -p {rh5/{i386,x_86_64},rh4/{i386,x_86_64},rh3/{i386,x_86_64}}

(more here –> bash-brace)
—-

Fun with Bang !
!! – Run the 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
!* – 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’)

—-
Longer list of shortcuts:

Ctrl + a – Jump to the start of the line
Ctrl + b – Move back a char
Ctrl + c – Terminate the command
Ctrl + d – Delete from under the cursor
Ctrl + e – Jump to the end of the line
Ctrl + f – Move forward a char
Ctrl + k – Delete to EOL
Ctrl + l – Clear the screen
Ctrl + r – Search the history backwards
Ctrl + R – Search the history backwards with multi occurrence
Ctrl + t – Transpose the current char with the previous
Ctrl + u – Delete backward from cursor
Ctrl + w – Delete backward a word
Ctrl + xx – Move between EOL and current cursor position
Ctrl + x @ – Show possible hostname completions
Ctrl + z – Suspend/ Stop the command
Ctrl + x; Ctrl + e – Edit line into your favorite editor

Alt + < – Move to the first line in the history
Alt + > – 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 + b – Move backward
Alt + c – Capitalize the word
Alt + d – Delete word
Alt + f – Move forward
Alt + l – Make word lowercase
Alt + n – Search the history forwards non-incremental
Alt + p – Search the history backwards non-incremental
Alt + r – Recall command
Alt + t – Transpose the current word with the previous
Alt + u – Make word uppercase
Alt + back-space – Delete backward from cursor

Double Tab “commands”
$ <TAB><TAB> – All available commands(common)
$ (string)<TAB><TAB> – All available commands starting with (string)
$ /<TAB><TAB> – Entire directory structure including Hidden one
$ (dir)<TAB><TAB>- Only Sub Dirs inside (dir) including Hidden one
$ <TAB><TAB> Р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

## BELOW HERE IS WORK IN PROGRES ##
Basic bash for:
for i in a b c; do $i ‘hello’; done

Bash for with file globbing (wildcards):
for i in *.rb; do echo “$i”; done

Basic bash if:
if [[ -e .ssh ]]; then echo “hi”; fi

Numerical comparison:
i=0; if (( i <= 1 )); then echo “smaller or equal”; else echo “bigger”; 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”

 

:wq