Comprehensive Guide to All Essential Linux Commands

  1. date

    • Used to print the current date and time in the terminal.
  2. echo "hello world"

    • Used to display lines of text or strings that are passed as arguments on the command line.
  3. ls

    • Lists the contents of a directory.
  4. pwd

    • Prints the present working directory.
  5. whoami

    • Displays the username of the user currently logged in.
  6. mkdir

    • Creates directories.
  7. touch

    • Used to update the access and modification times of files, and to create new, empty files.
  8. cd <directory_name>

    • Changes the current directory to the specified directory.
  9. cd

    • Changes to the user's home directory.
  10. cd /

  • Changes to the root directory (/), which contains all important system files in Linux.
  1. df -h
  • Shows the amount of free disk space on each mounted disk.
  1. man
  • Provides detailed information about a command or function, including its syntax, options, and examples.
  1. cd ..
  • Moves up one directory level.
  1. ping
  • Used to check the network connectivity between the host and another server or host.
  1. touch file{2..10}.txt
  • Creates files named file2.txt through file10.txt.
  1. rm -rf file*
  • The rm command is used to remove files or directories. The -rf option removes files recursively and forces the operation without prompting.
  1. sudo
  • The sudo command stands for "Super User Do". It allows a permitted user to execute a command as the superuser or another user.
  1. sudo apt-get update
  • Updates the package list on Ubuntu-based systems.
  1. sudo apt-get install docker.io
  • Installs Docker on a Linux system (Ubuntu).
  1. sudo apt purge docker
  • Uninstalls Docker from the system.
  1. vi <filename>
  • Opens the vi command-line text editor to create or edit a file.
  1. uptime
  • Shows how long the system has been running.
  1. cd home
  • Changes to the /home directory.
  1. mkdir -p /Rajan/file/myfile
  • Creates multiple directories along the specified path.
  1. cd ~
  • Changes to the home directory of the user.
  1. ls -a
  • Lists all files, including hidden files (those starting with a dot).
  1. echo "hello-world" > hello.txt
  • Redirects the string "hello-world" into the file hello.txt.
  1. vi
  • A command-line text editor used to create, edit, and modify files.
  1. :wq!
  • Saves the file and exits the vi or vim editor.

Common vi/vim Commands:

  • i — Switch to insert mode.

  • Esc — Switch to command mode.

  • :w — Save the file and continue editing.

  • :wq or ZZ — Save and quit.

  • :q! — Quit without saving changes.

  • yy — Yank (copy) a line of text.

  • p — Paste the copied text.

  • o — Open a new line below the current line.

  • O — Open a new line above the current line.

  • A — Append text at the end of the line.

  • a — Append text after the cursor's position.

  • I — Insert text at the beginning of the line.

  • b — Move to the beginning of the current word.

  • e — Move to the end of the current word.

  • x — Delete a single character.

  • dd — Delete the entire current line.

  • Xdd — Delete a specified number of lines.

  • Xyy — Yank a specified number of lines.

  • G — Go to the last line of the file.

  • XG — Go to line X in the file.

  • gg — Go to the first line of the file.

  • :num — Display the current line's line number.

  • h — Move left by one character.

  • j — Move down by one line.

  • k — Move up by one line.

  • l — Move right by one character.


  1. head hello.txt -n 2
  • Displays the first two lines of hello.txt.
  1. cat <filename>
  • Displays the contents of the specified file.
  1. head -n 4 example1.txt
  • Displays the first 4 lines of example1.txt.
  1. tail
  • Displays the last ten lines of a file (default behavior).
  1. tail hello.txt -n 3
  • Displays the last 3 lines of hello.txt.
  1. sudo apt install nginx
  • Installs the NGINX web server.

Additional Steps:

  • To access the public IP of an EC2 instance, copy the public IP and paste it into another browser window.

  • Change to the NGINX directory: cd /var/www/html

  • Edit the default index file: sudo vi index.nginx-debian.html (Modify the file as required).

  • Again, copy the public IP of the EC2 instance and paste it in another browser. It will open the page of NGINX .

  • ========================================================= User Management in Linux

    1. sudo su

      • Use this command to log in as the superuser (root). The root user has all privileges.
    2. sudo useradd -m Devra

      • Adds a user named Devra and the -m option creates the user's home directory.
    3. sudo passwd Devra

      • This command sets a password for the user Devra.
    4. su Devra

      • Switches to the user Devra.
    5. cat /etc/passwd

      • Displays the contents of the /etc/passwd file, where you can see the list of users.

Create Multiple Users and Groups

  1. Create users:

     sudo useradd -m user-1
     sudo useradd -m user-2
     sudo useradd -m user-3
    
    • Creates multiple users user-1, user-2, and user-3, each with their own home directory.
  2. Create groups:

     sudo groupadd devops
     sudo groupadd Tester
    
    • Creates two groups: devops and Tester.
  3. Add user to a group:

     sudo gpasswd -a user-1 devops
    
    • Adds user-1 to the devops group.
  4. cat /etc/group

    • Displays the list of groups and users associated with each group.

File Permissions in Linux

  • File Permissions:

    • r - Read: Anyone can read this file.

    • w - Write: Anyone can modify this file.

    • x - Execute: Anyone can execute the file programmatically.

  • Example: -rw-rw-r--

    • First three (rw-): User has read and write permissions.

    • Second three (rw-): Group has read and write permissions.

    • Last three (r--): Others have read-only permission.

  1. sudo chown Devra hello.txt
  • Changes the ownership of the file hello.txt to user Devra.

SSH (Secure Shell)

  1. Launch an Ubuntu EC2 instance.
  • Go to the path /home/ubuntu/.ssh on your local machine.
  1. Generate a .pem file and copy the private key's content.

  2. SSH into another EC2 instance:

  • Go to the other instance and run the SSH command to connect using the copied private key.

Package Management in Linux

  1. sudo apt-get update
  • Updates the package list for the system.
  1. sudo apt upgrade
  • Upgrades installed packages to their latest available versions.
  1. sudo apt install nginx
  • Installs the NGINX package.
  1. sudo apt purge nginx
  • Uninstalls the NGINX package.
  1. systemctl status nginx
  • Checks the current status of the NGINX service.
  1. sudo systemctl stop nginx
  • Stops the NGINX service.
  1. sudo systemctl start nginx
  • Starts the NGINX service.

GREP (Global Regular Expression Print)

  1. grep junoon -r /home/ubuntu/
  • Searches for the string "junoon" recursively under the directory /home/ubuntu/.
  1. grep junoon -ir /home/ubuntu/
  • Performs a case-insensitive search for "junoon" under /home/ubuntu/.

AWK

  1. awk '/WAR/' Zookeeper_2k.log
  • Searches for the string "WAR" in the Zookeeper_2k.log file.
  1. awk '/WAR/ {print $1}' Zookeeper_2k.log
  • Prints the first column for each line containing "WAR".
  1. awk '/WAR/ {print NR $1$5}' Zookeeper_2k.log | grep ZooKeeperServer@793]
  • Prints specific fields from Zookeeper_2k.log and filters with grep for the key "ZooKeeperServer@793]".
  1. awk '$2>="19:16:14" && $2<="19:18:14" && /WAR/ {print $1$2$5}' Zookeeper_2k.log
  • Filters and prints logs between specific timestamps containing the string "WAR".

SED (Stream Editor)

  1. sed 's/Rajan@1234/****/g' password.txt
  • Replaces all occurrences of "Rajan@1234" with "****" in password.txt.
  1. sed -i 's/Rajan@1234/RK/g' password.txt
  • Directly modifies password.txt, replacing "Rajan@1234" with "RK".

Find

  1. find . -name *.log
  • Finds all .log files in the current directory and its subdirectories.
  1. find /home/ubuntu -name *.log
  • Searches for .log files under the /home/ubuntu directory.