Bash Cheat Sheet

A cheat sheet in English by jalexa9
8,107 views 1,765 downloads 2 Apr 2018 History
3.7 avg · 0 ratings

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 '­fil­ena­me'
prints file to std out on terminal
pwd
print working directory
wc '­fil­ena­me'
word count of filename
echo '­string or filena­me'
prints string or filename on the terminal
cat > '­fil­ena­me'
takes standard input into file
head '­fil­ena­me'
prints first 10 lines of file
tail '­fil­ena­me'
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 '­fil­ena­me.c­'
C files
chmod +x '­fil­ena­me.s­h'
Bash script files
g++ '­fil­ena­me.c­pp­'
CPP files
javac '­jav­afi­le'
JAVA
python '­fil­ena­me'
to run python scripts
gcc -o newname '­fil­ena­me.c­'
change name of compiled program
g++ -o newname '­fil­ena­me.c­pp­'
change name of compiled program

Grep Commands

grep '­str­ing­' </d­ir/­fil­ena­me.l­og>
outputs all lines that match '­str­ing­'
grep '­str­ing­' filename1 filename2
output all lines that match '­str­ing­' in multiple files
grep -i '­str­ing­' filenmae
ignores case
grep [-options] '­str­ing­'
grep standard output
grep [-options] '­str­ing­' filename
grep the contents of a file
grep -A n '­str­ing­' filename
displays n lines after string
grep -B n '­str­ing­' filename
displays n lines before string
grep -v '­str­ing­' filename
returns all lines which don't match '­str­ing­'
grep -E
allows extended regular expres­sions
grep -E '­str­ing­{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­}' '­fil­ena­me'
Ip address
grep -srhw "­[[:­aln­um:­]]­\+@[­[:a­lnu­m:]­]\+­" '­fil­ena­me'
email address

Common Grep Cmds

grep -E '­[0-­9]{­1,3­}\.[­0-­9]{­1,3­}\.[­0-­9]{­1,3­}\.[­0-­9]{­1,3­}' '­fil­ena­me'
Ip address
grep -srhw "­[[:­aln­um:­]]­\+@[­[:a­lnu­m:]­]\+­" '­fil­ena­me'
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;1­1,$­D;b­a'
Last 10 lines of file
sed -n '­/re­gex­p/p­'
print only line that matches regex
sed -n '­8,1­2p'
print lines 8-12
sed '­1,1­0d'
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
'­ter­m'*
0 or more repeti­tions of term
'­ter­m'+
1 or more repeti­tions of term
'­ter­m'?
0 or one instances of term
'­ter­m'{n}
exactly n instances of term
'­ter­m'{n,}
atleast n instances of term
'­ter­m'{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

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

Notes

To use these commands just replace '­fil­ena­me' or '­ter­m' 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
./'s­cri­pt.s­h'
bash script
./'c­omp­ile­dna­me'
name changed compiled program
'name of installed progra­m'
eg. gedit, nano, vim

Badges

Nice Cheat Sheet

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.