HomeAboutTree
  • dev
  • Linux cheatsheet & basics

    Know about basic commands, bash scripting, cron jobs etc...

    Basic commands

    PurposeCommand
    print in terminalecho "Hello"
    print current working directorypwd
    create a filetouch abc.md
    create a hidden filetouch .defs
    create a directorymkdir abc
    goto a directorycd /var/tmp
    goto root directorycd ~
    list filesls
    list files - with permissionsls -ltr
    list files - including hidden onesls -lart
    copy a file to a dircp /var/tmp/abc.md /home/dev/
    copy of a file for backupscp /var/tmp/abc.md /var/tmp/abc.md_bkp
    display current date and timedate
    display hostnamehostname
    display userid & detailsid
    terminal calculatorbc [+ - * / * ** % ] quit - to quit

    Medium level commands

    PurposeCommand
    to display bash locationwhich bash
    convert windows file to unixdos2unix abc.md
    allow permission to allchmod 777 abc.md
    allow execute permission to user - uchmod u+x abc.md
    run a script./invoke.sh
    run a script - by passing params./invoke.sh param1 param2
    view a fileless abc.md q - to quit
    view a file - last 100 linestail -100 abc.md
    view a file - in vimvi abc.md
    secure copy between hostscp /var/tmp/abc.md user@.host.com:path/to/destination

    basics of bashing

    refer this link from freecodecamp - [Bash scripting basics]

    [root@host ~]#

    if terminal has # it means we are logged in as root/super user

    [root@host ~]$

    if terminal has $ it means we are logged in as normal user

    gists

    purposecmd
    single line comment#string to be commented
    multi line comment: ' string to be commented '
    echo into a file - replace contentecho "string" > test.txt
    echo into a file - append stringecho "string" >> test.txt
    open a file and let ur enter string - replace contentcat > test.txt ctrl + d - to quit
    open a file and let ur enter string - append stringcat >> test.txt

    bash file starts with

    #! usr/bin/bash
    

    export

    #! usr/bin/bash
    msg = "string"
    export msg
    ./second_script.sh
    

    Heredoc delimeter - this will display content inside the anyText in the terminal

    cat << anyText
    this is a text
    multi line
    anyText
    

    Fetching value from parameter

    terminal: ./run.sh param1 param2 param3
    
    #! usr/bin/bash
    echo $1 $2 $3
    

    Fetching value from parameter into array

    $@ will fetch unlimited params passed

    terminal: ./run.sh param1 param2 param3
    
    #! usr/bin/bash
    args=("$@")
    echo $@ - will print the array
    echo $# - will print the array length
    #echo ${args[0]} ${args[1]} ${args[2]} - alternate way to print
    #echo $1 $2 $3 - alternate way to print
    

    Declaring variable and accessing them

    #! usr/bin/bash
    USER = abc
    AGE = 30
    
    echo "`$USER` is of age `$AGE`"
    

    conditional statements

    if

    #! usr/bin/bash
    if [ $AGE -gt 18 ]
    then
        echo "age greater than 18"
    fi
    

    if, else

    #! usr/bin/bash
    if [ "$AGE" -gt 18 ]
    then
        echo "age greater than 18"
    else
        echo "age less than 18"
    fi
    

    if, else if, else

    #! usr/bin/bash
    if [ "$AGE" > 18 ] && [ "$AGE" -lt 25 ]
    then
        echo "age greater than 18 and less than 25"
    elif (( $AGE > 25 ))
        echo "age greater than 25"
     else
        echo "age less"
    fi
    

    another example

    #! usr/bin/bash
    if [ "$AGE" > 18 -a "$AGE" -lt 25 ]
    then
        echo "age greater than 18 and less than 25"
    elif (( $AGE > 25 ))
        echo "age greater than 25"
     else
        echo "age less"
    fi
    

    conditional operators

    conditioncmd
    and &&-a
    or-o

    logical operators

    conditioncmd
    equal >-eq
    not equal >-ne
    greater than >-gt
    less than >-lt
    greater than or equal >-ge
    less than or equal >-le

    looping statements

    while

    #! usr/bin/bash
    number = 1
    while [ $number -le 10 ]
    do
        echo "$number"
        number = $(( number+1 ))
    done
    

    until

    until runs until the condition becomes true and breaks out if condition is true.

    #! usr/bin/bash
    number = 1
    until [ $number -ge 10 ]
    do
        echo "$number"
        number = $(( number+1 ))
    done
    

    for

    #! usr/bin/bash
    for i in 1 2 3 4 5
    do
        echo $i
    done
    

    Another example

    #! usr/bin/bash
    for i in {0..10}
    do
        echo $i
    done
    

    Another example

    #! usr/bin/bash
    for ((i = 0; i<10; i++))
    do
        echo $i
    done
    

    Read file line by line

    #! usr/bin/bash
    while read line
    do
        echo "$line"
    done < "file_name.txt"
    

    cron job scheduling

    5 0 * 8 *  => At 00:05 in August                                       
    5 4 * * 6 => At 04:05 on Sunday                                       
    0 22 * * 1-5 => At 22:00 on every day-of-week from Monday through Friday