Show Menu
Cheatography

Bash Keyboard Shortcuts by

Simple Bash Commands

HOME
Cursur to start of line
END
Cursur to end of line
TAB
Autoco­mpl­etion
!!
Repeat last Bash cmd
cd foldername
change director to foldername
cd ..
go up one folder
ls
list services- shows files in current directory
man <cm­d>
shows manual for <cm­d>
clear
clears terminal
reset
resets terminal but doesn't restart service
CTRL-Z
Stops current running function
CTRL-J
Same as RETURN
DEL
deletes backward from pointer
exit
logs out of current session
cat 'filename'
prints file to std out on terminal
pwd
print working directory
wc 'filename'
word count of filename
echo 'string or filename'
prints string or filename on the terminal
cat > 'filename'
takes standard input into file
head 'filename'
prints first 10 lines of file
tail 'filename'
prints last 10 lines of file

Cut

cut -b
specific bytes
cut -b 1,2,3
bytes 1 2 and 3
cut -b 1-3,5-7
bytes 1-3 and 5-7
cut -b 1-
from first byte to end of line
cut -d
use a delimiter
cut -d " "
outputs from beginning of line to first space

Common Compil­ation commands

gcc 'filen­ame.c'
C files
chmod +x 'filen­ame.sh'
Bash script files
g++ 'filen­ame.cpp'
CPP files
javac 'javafile'
JAVA
python 'filename'
to run python scripts
gcc -o newname 'filen­ame.c'
change name of compiled program
g++ -o newname 'filen­ame.cpp'
change name of compiled program
 

Grep Commands

grep 'string' </d­ir/­fil­ena­me.l­og>
outputs all lines that match 'string'
grep 'string' filename1 filename2
output all lines that match 'string' in multiple files
grep -i 'string' filenmae
ignores case
grep [-options] 'string'
grep standard output
grep [-options] 'string' filename
grep the contents of a file
grep -A n 'string' filename
displays n lines after string
grep -B n 'string' filename
displays n lines before string
grep -v 'string' filename
returns all lines which don't match 'string'
grep -E
allows extended regular expres­sions
grep -E 'strin­g{n}'
get lines with n number of string in it
grep -c
count results
grep -n
show line number

Common Grep Cmds

grep -E '[0-9]­{1,­3}\.[0­-9]­{1,­3}\.[0­-9]­{1,­3}\.[0­-9]­{1,3}' 'filename'
Ip address
grep -srhw "­[[:­aln­um:­]]­\+@[­[:a­lnu­m:]­]\+­" 'filename'
email address

Common Grep Cmds

grep -E '[0-9]­{1,­3}\.[0­-9]­{1,­3}\.[0­-9]­{1,­3}\.[0­-9]­{1,3}' 'filename'
Ip address
grep -srhw "­[[:­aln­um:­]]­\+@[­[:a­lnu­m:]­]\+­" 'filename'
email address

Progra­m/S­cript commands

cmdX||cmdY
run cmdX if it fails then run cmdY
cmdX && cmdY
run cmdX if it doesn't fail run cmdY
cmd &
will push cmd to background
cmd & >/d­ev/null &
will put cmd to background and not display cmd outputs
disown
typed after pushing cmd to background to disown cmd from current terminal
jobs
show disowned jobs

SED

sed G
double space a file
sed 'G;G'
triple space a file
sed -n '$='
count lines
sed = filename | sed 'N; s/^/ /; s/ *\(.\{­6,­\}\)­\n/\1 /'
number each line of a file
sed 's/^[ \t]//;s/[ \t]$//'
delete both leading and trailing whitespace
sed 's/[ \t]*$//'
delete trailing whitespace
sed '/\n/!­G;s­/\(.\)­\(.*­\n­\)/­&­\2\1­/;/­/D;­s/.//'
reverse all characters on the line
sed 10q
print first 10 lines of file
sed q
print first line of file
sed -e :a -e '$q;N;­11,­$D;ba'
Last 10 lines of file
sed -n '/rege­xp/p'
print only line that matches regex
sed -n '8,12p'
print lines 8-12
sed '1,10d'
delete first 10 lines of a file
 

Regular Expres­sions

.
Matches any single character
\d
number in 0-9
\D
non number
\w
"­wor­d" letters, digits and _
\W
non word
\r
return
\n
newline
\s
whitespace
\S
non whitespace
'term'*
0 or more repeti­tions of term
'term'+
1 or more repeti­tions of term
'term'?
0 or one instances of term
'term'{n}
exactly n instances of term
'term'{n,}
atleast n instances of term
'term'­{x,n}
between x and n instances of term
(term1­|term2)
term1 or term2

Common regex Cmds

[0-9]
matches any number
[a-z]
matches any letter
[aeiou]
matches vowels
([A-Za­-z0­-9-]+)
letters numbers and hyphens
(\d{1,­2}­\/\d­{1,­2}­\/\d{4})
European Date (eg. 21/3/2018 )
(\w+@[­a-z­A-Z­_]+­?\.[­a-­zA-­Z]{­2,6})
email addresses

File Redire­ction

> 'file'
create file or overwrite if existing
>> 'file'
append to the file
< 'file'
read from file
X|Y
pipe X as input to Y

Notes

To use these commands just replace 'filename' or 'term' or 'cmd' with the file or regex or program that is to be used.

How to run Programs or scripts

./a.out
for recently compiled program
./'scr­ipt.sh'
bash script
./'com­pil­edname'
name changed compiled program
'name of installed program'
eg. gedit, nano, vim
       
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Linux Command Line Cheat Sheet
          Bash/ZSH Shourtcuts Cheat Sheet
          bash Shortcuts Cheat Sheet