Show Menu
Cheatography

Bash & Unix Commands Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

General

ls -lrt | ls -al | ll | dir
List direct­ories in current locaiton
pwd
Current directory location
free -m | df | meminfo | vmstat | top
Memory commands
ps aux | grep "­tex­t"
Find "­tex­t" in Current process list
cat "­fil­e" | grep "­tex­t"
Find "­tex­t" in "­fil­e"
sed '/text/d' "­fil­e" > "­fil­e2"
Delete the lines in "­fil­e" that matches the "­tex­t" into "­fil­e2"
chmod -R 777 file|d­ire­ctory
Change the permis­sions of objects
chown -R user:group file|f­ire­ctory
Change the owner of objects
rm -rf file|d­ire­ctory
Delete files or direct­ories recurs­ively
mkdir -p
Create direct­ories with parents recurs­ively
zip "­fil­e.z­ip" files...
zip compre­ssion of "­fil­es..." into the "­fil­e.z­ip"
tar -xvf
Uncompress files based in .tar.gz
unzip -l "­fil­e.z­ip"
List files inside "­fil­e.z­ip"
unzip -o "­fil­e.z­ip" -d "­des­t_d­ir"
Unzip the "­fil­e.z­ip" provided into the destin­ation dir "­des­t_d­ir"
scp -pw "­pas­swo­rd" user@h­ost­:/p­ath­/to­/un­ix/file /path/­to/­uni­x/file
Copy files between unix hosts
pscp -sftp -hostkey "­HEX­:VA­LUE­" -pw "­pas­swo­rd" user@h­ost­:/p­ath­/to­/un­ix/file C:\pat­h\t­o\w­ind­ows­\file
Copy files from unix host to windows host
svn diff "­SVN­URL­1" SVNURL­2"
Take the diff changes of 2 online reposi­tories
svn "­com­man­d" --non-­int­era­ctive --trus­t-s­erv­er-cert --username "­use­r" --password "­pwd­" <pa­ram­s>
SVN base command to use from console
dos2unix -k -o "­fil­e"
Transform windows file to unix
init 6 | shutdown -r now | reboot
Restart the machine
init 0
Turn off the machine
wget "­fil­e_u­rl" -P "­par­ent­_di­r"
Download the file in "­fil­e_u­rl" into the "­par­ent­_di­r"
curl -k -u user:p­assword "­https url"
Get the contents of a url form https with username and password
cp -Rft
Copy Forced
tail -f -n 100 "­fil­e"
Follow last 100 lines of a file
 

Progra­mming

./bash.sg <pa­ram­1> <pa­ram­2>
Execute bash with 2 params, the .sh file must have permis­sions 755 or more
if [ expres­sion_1 ] \n then \n "­Sta­tem­ent­s" \n else|elif [ expression ] then "­Sta­tem­ent­s" fi
IF ELSE|ELSIF Condition
for item in item1 item2 item3 itemN \n do \n "­Sta­tements with $item" \n done
FOR statement
while read line \n do \n "­Sta­tements with $line" \n done < file
Read a file line by line
function operat­ion() { \n "­Sta­tem­ent­s" echo $value for return \n }
Simple function for paramters with return >> operation "­par­am1­" param2­"
 

Bash Variables

VAR=value
Variable Assignment
HOST=$­(ho­stname -a)
Command Result in a variable
RESULT­=$(­($value + 5))
Operation result in a variable
VAR_AR­RAY=( v1 v2 v3 v4 )
Array declar­ation
echo ${VAR_­ARR­AY[*]}
Print array values
echo ${VAR_­ARR­AY[2]}
Get array value
unset VAR_AR­RAY[2]
Delete item in array
envs
Show enviro­nment variables
$VAR1$VAR
Concat­enate values
"­STR­"
used for strings but no ''

Condit­ional Bash

[ ]
Always use brakets
-eq
=
-ne
!=
-gt
>
-ge
>=
-lt
<
-le
<=
-z
is null
-n
is not null
-a
true if both true
-o
true if one is true