Day 3: Linux Basic Commands

Day 3: Linux Basic Commands

Commands-

  1. To view what's written in a file.

     $ cat altos.txt
    
    • cat -b: This adds line numbers to non-blank lines

    • cat -n: This adds line numbers to all lines

    • cat -s: This squeezes blank lines into one line

    • cat –E: This shows $ at the end of the line

  2. To run any bash script written in editors like nano,vim etc.

     ./altos.txt
    
    • ./ - Allow you to run the bash script
  3. To see all the permissions of files.

     ls -la
    
    • ls - List files and directories

    • -l - List files and directories with permission

    • -a - List all files and directories, even hidden files and directories with permission

  4. To change the access permissions of files.

     chmod 777 altos.txt
    

    or

     chmod o+wx testfile
    
     ls -l testfile
     -rwxrwxrwx  1 amrood   users 1024  Nov 2 00:10  testfile
    
     chmod u-x testfile
    
     ls -l testfile
     -rw-rwxrwx  1 amrood   users 1024  Nov 2 00:10  testfile
    
     chmod g = rx testfile
    
     ls -l testfile
     -rw-r-xrwx  1 amrood   users 1024  Nov 2 00:10  testfile
    
    • Understanding Security Permissions
      First, you need to think of the nine signs as three groups of three signs (see box below). Each of the three "rwx" icons represents a different action you can perform on the file.

        ---     ---     ---
        rwx     rwx     rwx
        user    group   other
      

      r = “read” the file’s contents

      w = “write”, or modify, the file’s contents

      x = “execute” the file

      - = "No permission"

  5. To check which commands you have run till now.

     history
    
  6. To make a file.

     touch file1.txt
    
  7. To remove a file.

     rm file1.txt
    
  8. To make a directory/folder.

     mkdir myfolder
    
  9. To remove a directory/folder.

     mkdir -r myfolder
    
  10. To create a fruits.txt file and to view the content.

    touch fruits.txt 
    echo "here is the watermelon" > fruits.txt | cat fruits.txt
    

    or

    touch fruits.txt 
    vim fruits.txt
    cat fruits.txt
    
  11. Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

    vim fruits.txt
    
    • vim - It is a text editor and after saving the edited file it'll automatically create the file without giving touch command.

      Below are the commands to use vim editor,

      Entering and Exiting Vim:

      • vim: Open a file in Vim.

      • vim filename: Open a specific file in Vim.

      • :q: Quit Vim.

      • :q!: Force quit Vim without saving changes.

      • :wq or :x: Save changes and exit Vim.

      • :w: Save changes to the current file.

      • :w filename: Save the file with a new name.

      • :wq or :x: Save changes and exit Vim.

          Apple 
          Mango
          Banana
          Cherry
          Kiwi
          Orange
          Guava
        
          # press "ESC + :wq + ENTER" to save and exit the file
        
  12. To Show only top three fruits from the file.

    The head command in Linux is used to display the first few lines of a text file. By default, it displays the first 10 lines of a file, but you can specify a different number of lines using the -n option. Here's the basic syntax of the head command:

    head [OPTIONS] [FILE]
    

    a) OPTIONS are optional flags that modify the behavior of the head command.

    b) FILE is the name of the file you want to view. If not specified, head reads from standard input.

    To display the first 3 fruits from fruits.txt file,

    head -n 3 fruits.txt
    
  13. To Show only the bottom three fruits from the file.

    The tail command in Linux is used to display the last few lines of a text file. It is particularly useful for monitoring log files or any other text files where you want to see the most recent entries. Here's the basic syntax of the tail command:

    tail [OPTIONS] [FILE]
    

    a) OPTIONS are optional flags that modify the behavior of the tail command.

    b) FILE is the name of the file you want to display. If not specified, tail reads from standard input.

    To display the last 3 fruits from fruits.txt file,

    tail -n 5 fruits.txt
    
  14. To create another file colors.txt and to view the content.

    touch colors.txt
    
  15. Add content in colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.

    Red 
    Pink
    White 
    Black 
    Blue 
    Orange 
    Purple 
    Grey
    
    # Press "ESC + :wq + ENTER" to save and exit the file
    
  16. To find the difference between fruits.txt and Colors.txt file.

    The diff command compares two files line by line and highlights the differences. Here's the basic syntax:

    diff fruits.txt colors.txt
    

    or

    vimdiff fruits.txt colors.txt #display the difference with visuals