πŸš€
πŸ–₯️ Terminal Basics
++++
DevOps
Mar 2025Γ—10 min read

A shell is a command-line interface which allows to perform certain tasks using command

Linux & Terminal Commands

Driptanil Datta
Driptanil DattaSoftware Developer

Linux & Terminal Commands

A shell is a command-line interface which allows to perform certain tasks using command

Basic Commands

  • cd β†’ change directory 800
    • cd .. β†’ goes back a single directory 800
  • mkdir β†’ make new folder 800
    • mkdir -p β†’ makes directories and sub-directories too. 800
  • touch β†’ creates a new file 800
  • ls/dir β†’ lists all the files in the current directory 800 800
    • ls -a β†’ displays all the hidden files 800
    • ls -l β†’ shows more information like size, date about the files in working directory. 800
    • ls -R β†’ show files, directories and sub-directories as well. 800
  • xdg-open / open β†’ opens the graphical interface of the present working directory.
  • pwd β†’ Print Working Directory 800
  • cp β†’ creates a copy of a file or a folder 800
    • cp -R β†’ copies directories and also the sub-directories 800
  • mv β†’ moves files 800 If names of two files are given, then the file gets renamed.
  • rm β†’ removes only files permanently. 800
    • rm -R β†’ removes directories permanently. 800
    • rm -rf β†’ force remove of files or directories. 800
  • sudo β†’ Super User Do gives system administrator privileges. 800 sudo apt update && sudo apt upgrade is used to download and install updates.

File Commands

  • echo β†’ displays

  • cat stands for concatenate, it is used to display the contents of a file 800

    • cat > creates a file.
  • vi <file_name> allows editing the file in the console.

    800 800

    • press i to enter insert mode and Esc to enter command mode.
      • I - Insert text at the beginning of the current line
      • A – Write at the end of the line (goes into insert mode)
      • b β†’ Go to the beginning of the word
      • e β†’ Go to the ending of the word
      • u – Undo last change
      • U – Undo all changes to the entire line
      • o – Open a new line (goes into insert mode)
      • dd – Delete line
      • 3dd – Delete 3 lines.
      • D – Delete contents of the line after the cursor
      • C – Delete the contents of a line after the cursor and insert new text.
      • dw – Delete word
      • 4dw – Delete 4 words
      • cw – Change word
      • x – Delete the character at the cursor
      • ~ – Change case of individual character
    • :wq β†’ writes and quit the file in command mode
    • :q! β†’ quit without saving the changes in command mode
    • :w β†’ save and continue editing
    • yy β†’ Yank (copy a line of text)

    An introduction to the vi editor (opens in a new tab)

  • head β†’ displays the first 10 lines of a file

    • head -n 1 β†’ displays the first line of a file
  • tail β†’ displays the lasts 10 lines of a file

    • tail -n 1 β†’ displays the last line of a file
  • diff β†’ compares files and displays lines which does not match

  • tr β†’ translate

  • grep β†’ searches Strings in files.

    • grep -w β†’ searches Strings of a complete word in files.
    • grep -i β†’ [ignore cases] searches non-case sensitive Strings in files.
    • grep -n β†’ searches Strings in files and also displays the line no.
    • grep -win β†’ search non-case sensitive Strings in words in files and also displays the line no.
    • grep -B 3 β†’ also displays 3 lines before the searched Strings.
    • grep -r β†’ searches Strings in files recursively.
    • grep -l β†’ displays list of filenames only.
    • grep -c β†’ display the no of times the search string is present in a file
    • grep -p β†’ is used to search regular expressions
  • sort β†’ displays lines of file sorted alphabetically.

    • sort -r β†’ displays lines of files sorted in reverse
    • sort -b β†’ ignores blacks while sorting
    • sort -f β†’ ignores cases while sorting
    • sort -n β†’ sorts numerical value
  • lsof β†’ lists all the opened files

    • lsof -u driptanil β†’ displays all the opened files by β€˜driptanil’ user
  • cut β†’ remove sections from each line of files

    • cut -b β†’ removes only bytes
    • cut -c β†’ removes only characters
  • locate "*.txt" β†’ will display location of all the .txt files.

  • find . β†’ displays all the files and folder in the working directory.

    • find . -type d β†’ displays all the folders in the working directory.
    • find . -type f β†’ displays all the files in the working directory.
    • find -name β†’ displays all files and folders with the name in working directory.
    • find -iname β†’ displays all files and folders with the name [not case sensitive] in the working directory.
    • find -mmin -15 β†’ displays all the files and folders modified less than 15 mins ago.
    • find -mmin +30 β†’ displays all the files and folders modified more than 30 mins ago.
    • find -mtime -10 β†’ displays all the files and folders modified less than 10 days ago.
    • find -maxdepth 1 β†’ displays files and folders in working directory.
    • find -mindepth 2 β†’ displays files and folders in the folders in working directory.
    • find -size +1k β†’ displays files and folders more than 1 Kb size.
    • find -empty β†’ displays files and folders which are empty.
    • find -perm 777 β†’ displays files and folders with -rwxrwxrwx permissions.
    • find . -type f -name "name*" -exec rm -rf {'{}'} + β†’ exec : execute the files and folder found by find command with another command, {} : place holder, + : add all the files.

Other Commands

  • where/whereis β†’ displays the location of the environment variables.
  • .bashrc file consists of the commands which are automatically executed when bash terminal is opened.
    • It also has alias, which is a short user-defined command that the shell translates to another command. The variables in capitals are environmental variables.
    • temporary alias
    • permanent alias [adding it in ~/.bashrc file
  • .profile file consist of all the environmental variable paths.
  • $PATH β†’ consists all directories of the environmental variables
  • export is used to set temporary environmental variable
  • chmod β†’ used to change the access permissions of file.
    • Types of file permissions:
      1. Read(r) [value = 4]
      2. Write(w) [value = 2]
      3. Execute(x) [value = 1]
    • Types of users:
      1. User[u]
      2. Guest[g]
      3. Other[o]
    • chmod 777 β†’ -rwxrwxrwx
    • chmod 124 β†’ --x-w-r--
    • chmod 000 β†’ ---------
  • chown β†’ change owner

Terminal Shortcuts:

  • Ctrl + A β†’ move to the beginning of the line
  • Ctrl + E β†’ move to the end of the line
  • Ctrl + U β†’ remove the whole command
  • Ctrl + K β†’ removes everything to right of the cursor.
  • Ctrl + L β†’ clears the terminal like clear command
  • Tab β†’ used to auto-complete
  • ; β†’ many commands together

Network Commands

  • ping β†’ allows to request website server and displays all the statistics
  • wget β†’ download files from the internet.
  • nslookup β†’ displays the IP address of a particular domain
  • netstat β†’ displays the ports that are listening
  • curl ifconfig.me -s β†’ displays the IP address of the user

User Commands

  • whoami β†’ displays the username
  • hostname β†’ displays the name of the Hostname
    • hostname -i β†’ displays the IP Address
  • useradd β†’ adds new user
    • passwd β†’ adds new password
    • userdel β†’ deletes existing users
  • uname β†’ displays the kernel name
    • uname -o β†’ displays the type
    • uname -m β†’ displays the architecture
    • uname -r β†’ displays the kernel version
  • id β†’ displays the group IDs
  • getent group driptanil β†’ checks if β€˜driptanil’ group exists

System Information Commands

  • df β†’ shows the disk size and usage details
    • df -h β†’ displays the size and usage details in human readable format.
  • du β†’ shows the disk usage statistics in working directory
    • du -h β†’ shows the disk usage statistics in working directory in human readable format.
  • cat /etc/os-release β†’ displays the operation system information
  • lscpu β†’ displays the CPU details
  • free β†’ displays the memory statistics
  • vmstat β†’ displays the virtual memory statistics

Managing Tasks Commands

  • top β†’ shows all the processes running
  • kill β†’ kills the process with the specified process ID.
  • jobs β†’ displays the running commands
  • ps aux β†’ the processes and the process ID
  • htop β†’ shows the processes which are using the resources

Operators

  • Ampersand & β†’ This command sends a process/script/command to the background.

    • to kill this background process use ps and kill command.
  • Pipe | β†’ The output of the first command acts as input to the second command

  • && β†’ executes the second command if and only if the first command is executed successfully.

  • || β†’ executes the second command if and only if the first command fails.

  • ! β†’ not operator

  • >> β†’ append

  • > β†’ over-write

  • Combination (...) β†’ group commands using ;