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
    
  17. To capture the packets of current network interface. For more tcpdump commands, Click Here. Below is the Syntax to capture packets from current network:

    apt install tcpdump #to install tcpdump
    sudo tcpdump #to run tcpdump
    
  18. The traceroute command is a network diagnostic tool used to trace the route taken by packets from a source to a destination over an IP network. It provides valuable insights into the network path, including the number of hops (routers) between the source and destination, and the round-trip time (RTT) for each hop.

    traceroute [options] destination
    
  19. Difference Between tcpdump and traceroute:

    tcpdump and traceroute are both network diagnostic tools but serve different purposes.

    • tcpdump captures and analyzes network packets in real-time, allowing you to inspect the data traveling over a network. It helps in debugging network issues by providing detailed packet-level information (IP addresses, ports, flags, etc.).

    • traceroute traces the path that packets take from one device to another over a network, showing each hop between routers. It’s useful for identifying network routing issues and delays at various points in the path.

  20. Tool that allows you to view information about the processes running on your Linux system.

    ps
    
  21. Difference between top and ps and other similar command.

    top and ps are both used to monitor system processes, but they serve different purposes:

    • top provides a real-time, dynamic view of system performance, showing CPU, memory usage, and a constantly updated list of running processes. It's useful for ongoing monitoring of system resource consumption.

    • ps displays a snapshot of currently running processes at the moment it is run. It shows detailed process information (PID, memory, CPU usage, etc.) but does not update in real-time.

Other similar commands:

  • htop (like top but more interactive).

  • vmstat shows system resource statistics like memory, processes, and I/O.

  1. Command to Download

    wget <url>
    
    (and)
    
    curl <url>
    

    curl and wget are both command-line tools used for downloading content from the web, but they have key differences:

    • curl is a versatile tool that supports many protocols (HTTP, FTP, SMTP, etc.), and is often used for making API requests, sending data, and handling complex network interactions. It downloads content to stdout by default.

    • wget is primarily designed for downloading files from the web and supports recursive downloading, making it ideal for mirroring websites or downloading large sets of files. It automatically saves files to disk.

  2. Command to encode and decode something in base64

    # Encode
    echo <Type someting you want to encode> | base64  
    eg.
    echo myencode | base64            #input
    Output- bXllbmNvZGUK              #Encoded Output
    
    # Decode
    echo <Enter base64 encoded value> | base64 --decode
    eg.
    echo bXllbmNvZGUK | base64 --decode #input
    Output- myencode                    #Decoded Output