"LINUX commands"

"LINUX commands"

Surely gonna help you a lot if you are a beginner here, even more, essential for CCP. I have provided almost every basic Linux important command from the base level with real-life Linux coding examples with various use cases and individual details.

Hope you all engage with it!

History of Linux

According to history "https://en.wikipedia.org/wiki/Linux#History"

--It was first released on September 17, 1991, by Linus Torvalds.[13][14][15] Linux is typically packaged as a Linux distribution, which includes the kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name "GNU/Linux" to emphasize the importance of GNU software, causing some controversy

Basic LINUX commands.

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

    The command to view what's written in a file is

       cat filename
    
  2. To change the access permissions of files.

    The command to change the access permission of files is

       chmod 777 filename
    
  3. To check which commands you have run till now.

    The command to check which commands you have run till now is

       history
    
  4. To remove a directory/ Folder.

    The command to remove a folder is

       rmdir foldername
    
    1. The command to create a file and view the content is
      nano fruits.txt
    or
    vi fruits.txt
  1. Add content in Fruits.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

       cat> Fruits.txt
       Apple
       Mango
       Banana
       Cherry
       Kiwi
       Orange
       Guava
    
  2. Show only the top three fruits from the file.

    The command to view the top three fruits from the file i

       head -n 3 fruits.txt
     (output:
      Apple
       Mango
       Banana)
    

    Show only the bottom three fruits from the file.

    The command to view only the bottom three fruits from the file is

       tail -n 3 fruits.txt
     (output:
      Kiwi
       Orange
       Guava)
    
  3. To create another file Colors.txt and to view the content.

       vi Indreni.txt
    
  4. Add content in Indreni.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, and Grey.

     cat> Indreni.txt
     Red
     Pink
     White
     Black
     Blue
     Orange
     Purple
     Grey
    
    1. (Print Working Directory): Display the current directory.

    2.    pwd
      
    3. ls (List): List files and directories in the current directory.

       ls
      
    4. cd (Change Directory): Change to a different directory.

       cd /path/to/directory
      
    5. mkdir (Make Directory): Create a new directory.

       mkdir new_directory
      
    6. cp (Copy): Copy files or directories.

       cp file.txt /path/to/destination
      
    7. mv (Move/Rename): Move or rename files or directories.

       mv file.txt new_location/file.txt
      
    8. rm (Remove): Remove files or directories.

       rm file.txt
      
    9. cat (Concatenate): Display the contents of a file.

       cat file.txt
      
    10. echo (Echo): Print text or variables to the terminal.

      echo "Hello, World!"
      
    11. grep (Global Regular Expression Print): Search for a specific pattern in files.

      grep "pattern" file.txt
      
    12. chmod (Change Mode): Change the permissions of a file or directory.

      chmod 644 file.txt
      
    13. sudo (Superuser Do): Execute a command with administrative privileges.

      sudo apt-get update
      
    14. man (Manual): Display the manual page of a command.

      man ls
      
    15. ping: Check the connectivity to a remote host.

      ping google.com
      
    16. wget (Web Get): Download files from the web.

      wget https://example.com/file.txt
      
    17. (Change Ownership): Change the ownership of files or directories.

      chown user
      chown group
      
    18. Search for files or directories based on various criteria.

      find /path/to/search -name "*.txt"
      

      SERVER Installation Linux Command:

    19. Apache HTTP Server (Ubuntu):

Step 1: Update the package index:

    sudo apt update

Step 2: Install Apache:

    sudo apt install apache2

Step 3: Start the Apache service:

    sudo systemctl start apache2

Step 4: Enable the Apache service to start on boot:

    sudo systemctl enable apache2
  1. Red Hat/CentOS:

Open a terminal and run the following command to install tree:

Copy codesudo yum install tree
  1. shows hidden files:
ls -a

ARCHIVE Linux command:

  1. -x: This option is used to extract files from a tar archive. When combined with other options, it indicates that you want to perform an extraction operation.

  2. -p: The -p option preserves the permissions of the extracted files. It ensures that the original file permissions (including ownership and access rights) are retained during the extraction process.

  3. -v: The -v option enables verbose output, which provides more detailed information during the extraction or creation of an archive. It displays the names of files as they are processed.

  4. -f: The -f option is used to specify the name of the tar archive file to be created or extracted. It must be followed by the filename of the tar archive.

  5. -c: The -c option is used to create a new tar archive. When combined with other options, it indicates that you want to perform a creation operation.

  6. -a: The -a option automatically detects the appropriate compression program based on the file extension of the archive. For example, if the archive has a .gz extension, it will use gzip for compression.

To summarize:

  • tar -xpvf archive.tar: Extracts files from the archive.tar file, while displaying verbose output and preserving permissions.

  • tar -xpf archive.tar: Extracts files from the archive.tar file, preserving permissions.

  • tar -xf archive.tar: Extracts files from the archive.tar file without verbose output or preserving permissions.

  • tar -caf archive.tar.xz directory/: Creates a new compressed tar archive named archive.tar.xz using xz compression for the directory/ directory.

  • tar -cf archive.tar directory/: Creates a new tar archive named archive.tar from the directory/ directory.

  1. Extract files from a tar archive with verbose output and preserved permissions:
tar -xpvf archive.tar
  1. Extract files from a tar archive while preserving permissions:
tar -xpf archive.tar
  1. Extract files from a tar archive without verbose output or preserved permissions:
tar -xf archive.tar
  1. Create a new compressed tar archive using xz compression:
tar -caf archive.tar.xz directory/
  1. Create a new tar archive:
tar -cf archive.tar directory/
  1. Troubleshooting in Linux :

    often involves diagnosing and resolving issues related to system performance, networking, processes, and services. Here are some commonly used troubleshooting commands in Linux:

  2. ping: Checks network connectivity by sending ICMP echo requests to a specified destination. For example:

ping google.com
  1. traceroute: Determines the route taken by packets to reach a destination. It displays the IP addresses and response times of each hop along the path. For example:
traceroute google.com
  1. netstat: Displays network statistics, including active network connections, listening ports, and routing tables. For example:
netstat -tuln
  1. top: Provides real-time information about system processes, resource usage, and CPU activity. It helps identify resource-intensive processes. For example:
top
  1. ps: Lists currently running processes. It shows information such as process ID (PID), CPU and memory usage, and command details. For example:
ps
  1. df: Shows disk space usage information for mounted file systems. It helps identify available and used disk space. For example:
df -h
  1. free: Displays memory usage and available memory on the system. It provides information about total, used, and free memory. For example:
free -h
  1. dmesg: Examines the system's kernel ring buffer for diagnostic messages. It helps identify and troubleshoot hardware-related issues. For example:
dmesg | tail
  1. journalctl: Retrieves log messages from the systemd journal. It is useful for examining system logs and diagnosing issues. For example:
journalctl -xe

Pipeline Commad:

  1. Find files with a specific extension in a directory:
ls /path/to/directory | grep ".txt"

This command uses the ls command to list the files in a directory and pipes it to grep to filter files with the ".txt" extension.

  1. Calculate the sum of numbers in a file:
cat numbers.txt | awk '{sum += $1} END {print sum}'

This command uses the cat command to read the contents of a file and pipes it to awk to calculate the sum of numbers in the file.

Text Editors (Nano , Vim)

Nano:

  1. Open a file in Nano:
nano filename
  1. Save a file in Nano: Press Ctrl + O, then Enter to save the file.

  2. Exit Nano: Press Ctrl + X to exit Nano. If there are unsaved changes, it will prompt you to save before exiting.

Vim:

  1. Open a file in Vim:
vim filename
  1. Switch to insert mode in Vim: Press i to enter insert mode. You can then edit the file.

  2. Save a file in Vim: Press Esc to exit insert mode, then type :w and press Enter to save the file.

  3. Exit Vim: Press Esc to ensure you're in command mode, then type :q and press Enter to exit Vim. If there are unsaved changes, it will not allow you to exit. To force exit without saving changes, use :q!.