Bash commands

  • Daniel Marsh |
  • October 30, 2021 |


    d[rwx][rwx][rwx]
    - [owner][group][other]
    - d-directory, [r-read, w-wright x, execute]


    killall (name)
    - removes/closes the program

    ---------------------------------------------------------------

    ls (directory name)
    - list all files/directory's

    ls -l (directory name)
    list's all files directory's and info

     ls -a (directory name)
    - lists all hidden files

    ---------------------------------------------------------------

    pws (print working directory)
    - shows the current directory

    ---------------------------------------------------------------

    touch (new file name/s(.filetype))
    - creates new file

    ---------------------------------------------------------------

    mv (file name new directory - directory/file name new directory)
    - move's file

    mv (file-name new-file-name)
    - rename file name

    ---------------------------------------------------------------

    cp (file name new directory)
    - copy file

    cp -R (folder name new directory)
    - copy folder

    ---------------------------------------------------------------

    rm (file name directory - directory file name)
    - delete file

    rm -R (folder name - directory folder name)
    - delete folder

    ---------------------------------------------------------------

    echo ("text")
    - print text

    ---------------------------------------------------------------

    cat (filename)
    - show file text

    ---------------------------------------------------------------

    nano (filename)
    - used to edit files

    ---------------------------------------------------------------
    ---------------------------------------------------------------
    ---------------------------------------------------------------

    > (command > file)
    redirects command to a file

    >> (command >> file)
    redirects command to a file(.append)

    ---------------------------------------------------------------

    pipe | (command | command)
    - | uses the answer of a command as an input of another command

    ---------------------------------------------------------------

    find (find folder_name)
    - finds folders

    find (find folder_name/ -name file_name)
    - finds files inside folders

    ---------------------------------------------------------------

    grep (grep "word" file_name)
    grep finds words searched for inside  a files case sensitive

    grep -I (grep -i "word" file_name)
    grep not case sensitive

    grep -v (grep -v "word" file_name)
    finds everything except searched word

    ---------------------------------------------------------------

    awk (awk '{print}' file_name)
    - prints all file content

    awk $1,2,3 (awk '{print $1}'
    - prints only Colom 1 of file content

    ---------------------------------------------------------------

    sudo (sudo command)
    - sudo gives root user/full permission

    sudo bash (sudo bash)
    - opens root bash terminal

    ---------------------------------------------------------------

    sudo chown (new owner name of file)
    - changes ownership of a file
    - use option -R for folders

    sudo chgrp (new group name of file)
    - changes group of file
    - use option -R for folders

    ---------------------------------------------------------------

    chmod (u=wrx file name)
    - changes users permission to wrx
    - user = u / guest = g

    ---------------------------------------------------------------

    variable= "text"
    - call the variable using $veriable
    - variables can be used as commands
    -use ${variable} for scripts

    $veriable (${variable}extra)
    - {}extra adds text to the variable

    ---------------------------------------------------------------

    variable (v=$(pwd))
    - saves output of a command into a variable like pwd in v

    ---------------------------------------------------------------

    .profile/ .bashrc/ .bash_profile/ .login
    - the above are login scrips for different os
    -use login scrips to permanently add variables

    ---------------------------------------------------------------

    read (variable-name)
    -creates a variable
    - use -p for a text prompt
    - use -s for password

    read (-sp enter password)
    creates a password

    ---------------------------------------------------------------

    execution (file file-name.txt)
    - gove the directory (c/documents/file-name.txt)
    - this will execute the file

    ---------------------------------------------------------------

    which (command or program)
    - give's the location of the command's program

    ---------------------------------------------------------------

    PATH="/Users/Garry/documents:${PATH}"
    export PATH
    - this only works in the login script
    - this adds the file to the path

    ---------------------------------------------------------------

    #! /bi/bash
    - this must start at the top of every bash script

    ---------------------------------------------------------------

    bash syntax
    - -eq this means equal to
    - -ne this means is not equal to
    - -gt this means grater then
    - -lt this means less then
    - -ge this means grater or equal to
    - -le this means less or equal to
    - -o this is the same as or
    - -a this is the same as and
    - -z used to check if a string is empty
    - == used as -eq for stings
    --------------------------------------------------------------

    if [ condition ]; then
    -this is a if statement
    elif
    - used to add multiple alternative conditions
    else
    - used to add a final alternative condition
    -------------------------------------------------------------

    for i in {1..10}
    do
    done
    - this is a for loop
    -------------------------------------------------------------

    while [ condition ];
    do
    done
    - this is a while loop
    -------------------------------------------------------------
    break
    - used to stop loops

    -------------------------------------------------------------

    sleep 0.00
    - this tell the program to wait in seconds

    -------------------------------------------------------------

    case $variable in
    esac
    - used for multiple choice input
    - us *) for none of the choices

    -------------------------------------------------------------

    start files/directory/webpage
    - use to open files/directory/webpage

    -------------------------------------------------------------

    function name(){

    function code
    }

    - call the function with its name

    ------------------------------------------------------------