Show Menu
Cheatography

basic bash commands

pwd : print working directory
cd /path/­to/dir : change direcotry
ls /dir/t­o/list : list directory content (default is .)
 ­ ­ ­ -1 : display the content on one column
 ­ ­ ­ -l : display the content with long listing format
 ­ ­ ­ -a : display the content of the directory (including hidden files)
 ­ ­ ­ -R : Display the content of the directory and the content of subdir­ect­ories
mv /path/­to/file /path/­whe­re/­to/move : move or rename a file or a directory
cp /path/­to/file /path/­whe­re/­to/copy : copy a file
 ­ ­ ­ -r : copy recurs­ively (used to copy directory)
rm /path/­to/file : remove a file
 ­ ­ ­ -r : remove recurs­ively (used to remove direct­ories)
 ­ ­ ­ -f : force remove
mkdir /path/­dirName : create an empty directory
rmdir /path/­to/dir : remove a directory (works only if the direcotry is empty)

bash redire­ctions

command > file : redirect stdout to file. (creates the file if it doesn't exist and overwrite it if it does exist)
command >> file : redirect stdout to file. (creates the file if it doesn't exist and append to the end it if it does exist)
command 2> file : redirect stderr to file (creates the file if it doesn't exist and overwrite it if it does exist)
command 2>> file : redirect stdout to file. (creates the file if it doesn't exist and append to the end it if it does exist)
command &> file : redirect stdout and stderr to file (creates the file if it doesn't exist and overwrite it if it does exist)
command &>> file : redirect stdout and stderr to file. (creates the file if it doesn't exist and append to the end it if it does exist)
command < file : redirect stdin to file.
command1 | command 2 : uses the output of command1 as the input of command2

file globbing regex

\ : escape character. It deletes the signif­ication of a special character
? : Any character, once.
* : Any character, 0, 1 or many time.
[...] : Any character that is in the class. ex: [abc], [a-z], [0-9]
[^...]: Any character that is not in the class. ex: [^abc], [^a-z], [^0-9]
{s1, s2, sN} : match s1 or s2 or sN

control structure (if)

if <expression>; then
    [statements]
elif <expression>; then
    [statements]
else
    [statements]
fi

control structure (while)

while <expression>; do
    [statements]
done

control structure (for)

for var in <expression>; do
    echo $var
    [statements]
done

control structure (case)

# patterns are file globing regex
case <expression> in
    pattern1)
        [statements]
        ;;
    pattern2)
        [statements]
        ;;
    *)
        [statements]
        ;;
esac

function definition

function functionName {
    [statements]
    [return X]
}

condit­ional expres­sions

&& : logical and operator
|| : logical or operator
[[ string ]] : return 0 if string is not empty
[[ -z string ]] : return 0 if the string is empty
[[ string1 == string2 ]] : return 0 if the string are equivalent
[[ string1 != string2 ]] : return 0 if the string are not equivalent
[[ string =~ pattern ]] : return 0 if the string matches the pattern (extended regex)
[[ -e file ]] : return 0 if the file exists
[[ -d file ]] : return 0 if file is a directory
[[ -f file ]] : return 0 if file is a file
[[ -x file ]] : return 0 if file is executable
[[ $n1 -eq $n2 ]] : return 0 if $n1 == $n2
[[ $n1 -lt $n2 ]] : return 0 if $n1 < $n2
[[ $n1 -gt $n2 ]] : return 0 if $n1 > $n2
[[ $n1 -ge $n2 ]] : return 0 if $n1 >= $n2
[[ $n1 -le $n2 ]] : return 0 if $n1 <= $n2
[[ $n1 -ne $n2 ]] : return 0 if $n1 != $n2
 

more basic bash commands

passwd : change your password
history : consult the history of your command
jobs : list of your pending proccesses
cat file1 file2 ... : concat­enate files and print to stdout
more / less file1 file2 .. : diplsay a file page by page on stdout
tail / head number : display the "­num­ber­" first or last line of a file on stdout
touch file1 file2 ... : change the modifi­cation date of the files
chmod : change the privileges of a file / directory
echo "­tex­t" : display a line of text to stdout
sort file1 file2 ... : sort the file (combine files if many are specified) and print the result to stdout (files aren't impacted)
 ­ ­ ­ -r : sort in reverse order
 ­ ­ ­ -n : numerical sort
 ­ ­ ­ -u : delete duplicated lines
wc file1 file2 ... : print to stdout the number of charac­ters, words and lines of files
 ­ ­ ­ -l : number of lines only
 ­ ­ ­ -w : number of words only
 ­ ­ ­ -w : number of characters only
diff file1 file1 : compare file1 and file 2 for differ­ences
 ­ ­ ­ -i : ignore the character case
 ­ ­ ­ -B : ignore empty lines
 ­ ­ ­ -w : ignore whites­paces
 ­ ­ ­ -c : add context to the output (good for readab­ility)
which comman­dName : print the path of a command
pushd / popd /path/­to/dir : change directory using the directory stack
dirs : print the directory stack
find /path/­to/dir -name pattern : find every files and directory that have a name that matches "­pat­ter­n" in the directory specified and its subdir­ect­ories
man comman­dName : Display the manual for command comman­dName
sudo command : run the command as superuser
command1 | xargs -i command2 : uses the output of the command1 as the input of the command2. output will be accessible via {} in command2

grep (simple regex)

grep "­pat­ter­n" file1 file2 ... : print the lines that matched the pattern
 ­ ­ ­ -v : print lines that didn't match the pattern
 ­ ­ ­ -i : ignore the character case
 ­ ­ ­ -l : print the name of the files that have at least one match
 ­ ­ ­ -o : print only the piece of line that matched the pattern
 ­ ­ ­ -E : uses the extended regex
 ­ ­ ­ -q : quiet. returns 0 in $? if at least one line has been matched. 1 if no line matched

variables

VAR=VA­RVALUE : create a variable VAR. the variable can be accessed like so: $VAR or ${VAR}
VAR="$V­AR2­" : $VAR will contains the value of $VAR2
VAR='$­VAR2' : $VAR will contains $VAR2
VAR=$(­com­mand) : $VAR will contains the output of the command
(( VAR = $VAR + 1 )): the double parent­heses must be used when doing arithm­etics
${VAR#­pat­tern} : return a substring of VAR where the smallest string (starting from the beginning) matching “pattern” will be cut
${VAR#­#pa­ttern} : return a substring of VAR where the longest string (starting from the beginning) matching “pattern” will be cut
${VAR%­pat­tern} : return a substring of VAR where the smallest string (starting from the end) matching “pattern” will be cut
${VAR%­%pa­ttern} : return a substring of VAR where the longest string (starting from the end) matching “pattern” will be cut
$? : the exit status of the last command / function executed. usually 0 when everything went right.
$# : the number of args passed to the script / function
$0 : the name of the script
$n : the nth argument passed to the script / function
$@ : the list of all the argument passde to the script / function
Arrays
 ­ ­ ­ ­myA­rra­y=(­value1 value2 value3): declare an array
 ­ ­ ­ ­declare -a myArra­y=(­value1 value2 value3): declare an array
 ­ ­ ­ ­${m­yAr­ray­[in­dex]} : access an element (index starts at 0)
 ­ ­ ­ ­myA­rra­y[i­ndex]= : add or modify the element at index
 ­ ­ ­ ­${#­myA­rra­y[*]} : return the lenght of the array
 ­ ­ ­ ­${m­yAr­ray­[*]}: all the elements of the array
 

simple regex

\ : escape character. It deletes the signif­ication of a special character
. : joker. It represents any characters
* : 0, 1 or many repetition of the last character / sequence of character
^ : The beginning of the line
$ : The end of the line
[...] : Any character that is in the class. ex: [abc], [a-z], [0-9]
[^...]: Any character that is not in the class. ex: [^abc], [^a-z], [^0-9]
\(...\) : Capture the pattern. The pattern can then be accessed with \1, \2 ... \n depending on the number of capture in the regex
\{n\} : n repeti­tions of the last character / sequence of character
\{n,\} : At least n repeti­tions of the last character / sequence of character
\{n, m\} : Between n and m repeti­tions of the last character / sequence of character

extended regex

\ : escape character. It deletes the signif­ication of a special character
. : joker. It represents any characters
* : 0, 1 or many repetition of the last character / sequence of character
+ : 1 or more repetition of the last character / sequence of character
? : The last character / sequence of character can appear or not
^ : The beginning of the line
$ : The end of the line
[...] : Any character that is in the class. ex: [abc], [a-z], [0-9]
[^...]: Any character that is not in the class. ex: [^abc], [^a-z], [^0-9]
s1|s2 : Either s1 or s2 but not both
(...) : change the priority
{n} : n repeti­tions of the last character / sequence of character
{n,} : At least n repeti­tions of the last character / sequence of character
{n, m} : Between n and m repeti­tions of the last character / sequence of character

sed (simple regex)

sed 'sed script' file : execute the script on every line of "­fil­e"
 ­ ­ ­ ­s/p­att­ern­/ne­wSt­ring/gI : Substitute the piece of the line that matches "­pat­ter­n" by "­new­Str­ing­". g (optio­nal): global, I (optio­nal): ignore case
 ­ ­ ­ ­/pa­ttern/d : delete the line if "­pat­ter­n" is matched
 ­ ­ ­ ­/pa­ttern/p : print the line if "­pat­ter­n" is matched
 ­ ­ ­ ­/pa­tte­rn1­/,/­pat­tern2/ : print every lines between the first line that matches "­pat­ter­n1" to the first line that matches "­pat­ter­n2"
 ­ ­ ­ ­-i.ext : Modifi­cations done "­in-­pla­ce". A backup file will be created with .ext extension (it is optional)
 ­ ­ ­ -n : print only the lines that matched the pattern

awk (extended regex)

awk -Fc 'awk script' file1 file2 ... (where "­c" is the delimiter)
typical awk script: 'BEGIN {state­ments} /pattern/ {script statem­ents} END {state­ments}'
 ­ ­ ­ ­BEGIN {} : Will be executed once at the start
 ­ ­ ­ END {} : Will be executed once at the end
 ­ ­ ­ ­/pa­ttern/ : only lines that matched the pattern will be processed
 ­ ­ ­ ­/pa­tte­rn1­/,/­pat­tern2/ : every line from the first line that matches pattern1 to the first line that matches pattern2 will be processed
 ­ ­ ­ ­{script statem­ents} : core of the script
 ­ ­ ­ ­ ­ ­printf: C-style formatter (man printf)
 ­ ­ ­ ­ ­ ­ ­ $n : the nth field of the line
 ­ ­ ­ ­ ­ ­ ­ $0 : the entire line
 ­ ­ ­ ­ ­ ­ ­ NR : the record number
 ­ ­ ­ ­ ­ ­ ­ NF : the number of fields in the record
 ­ ­ ­ ­ ­ ­ ­ FS: The field separator (the delimiter)
               
 

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

          bash Shortcuts Cheat Sheet
          Bash Cheat Sheet

          More Cheat Sheets by gregcheater