| 1 | + | # Essential Linux Commands |
| 2 | + | |
| 3 | + | Here is a list of essential Linux commands for various operations: |
| 4 | + | |
| 5 | + | ### <mark style="color:green;">File Operations</mark>: |
| 6 | + | |
| 7 | + | * `ls: Lists all files and directories in the present working directory.` |
| 8 | + | * `ls -R: Lists files in sub-directories as well.` |
| 9 | + | * `ls -a: Shows hidden files.` |
| 10 | + | * `ls -al: Lists files and directories with detailed information.` |
| 11 | + | * `cd directoryname: Changes the directory.` |
| 12 | + | * `cd ..: Moves one level up.` |
| 13 | + | * `pwd: Displays the present working directory.` |
| 14 | + | * `cat > filename: Creates a new file.` |
| 15 | + | * `cat filename: Displays the file content.` |
| 16 | + | * `cat file1 file2 > file3: Joins two files and stores the output in a new file.` |
| 17 | + | * `touch filename: Creates or modifies a file.` |
| 18 | + | * `rm filename: Deletes a file.` |
| 19 | + | * `cp source destination: Copies files from the source path to the destination path.` |
| 20 | + | * `mv source destination: Moves files from the source path to the destination path.` |
| 21 | + | * `find / -name filename: Finds a file or a directory by its name starting from root.` |
| 22 | + | * `file filename: Determines the file type.` |
| 23 | + | * `less filename: Views the file content page by page.` |
| 24 | + | * `head filename: Views the first ten lines of a file.` |
| 25 | + | * `tail filename: Views the last ten lines of a file.` |
| 26 | + | * `lsof: Shows which files are opened by which process.` |
| 27 | + | * `du -h --max-depth=1: Shows the size of each directory.` |
| 28 | + | |
| 29 | + | ### <mark style="color:green;">Directory Operations</mark>: |
| 30 | + | |
| 31 | + | * `mkdir directoryname: Creates a new directory in the present working directory.` |
| 32 | + | * `rmdir directoryname: Deletes a directory.` |
| 33 | + | * `cp -r source destination: Copies directories recursively.` |
| 34 | + | * `mv olddir newdir: Renames directories.` |
| 35 | + | * `find / -type d -name directoryname: Finds a directory starting from root.` |
| 36 | + | |
| 37 | + | ### <mark style="color:green;">Process Operations</mark>: |
| 38 | + | |
| 39 | + | * `ps: Displays currently active processes.` |
| 40 | + | * `top: Displays all running processes.` |
| 41 | + | * `kill pid: Kills the process with the given PID.` |
| 42 | + | * `pkill name: Kills the process with the given name.` |
| 43 | + | * `bg: Resumes suspended jobs without bringing them to the foreground.` |
| 44 | + | * `fg: Brings the most recent job to the foreground.` |
| 45 | + | * `fg n: Brings job n to the foreground.` |
| 46 | + | * `renice +n [pid]: Changes the priority of a running process.` |
| 47 | + | |
| 48 | + | ### <mark style="color:green;">File Permissions</mark>: |
| 49 | + | |
| 50 | + | * `chmod octal filename: Change the permissions of a file.` |
| 51 | + | * `chown ownername filename: Change file owner.` |
| 52 | + | * `chgrp groupname filename: Change group owner.` |
| 53 | + | |
| 54 | + | ### <mark style="color:green;">Networking</mark>: |
| 55 | + | |
| 56 | + | * `ping host: Ping a host and output results.` |
| 57 | + | * `whois domain: Get whois information for a domain.` |
| 58 | + | * `dig domain: Get DNS information for a domain.` |
| 59 | + | * `netstat -pnltu: Display various network related information.` |
| 60 | + | * `ifconfig: Displays IP addresses of all network interfaces.` |
| 61 | + | * `ssh user@host: Remote login into the host as a user.` |
| 62 | + | * `scp: Transfers files between hosts over SSH.` |
| 63 | + | * `wget url: Download files from the web.` |
| 64 | + | * `curl url: Sends a request to a URL and returns the response.` |
| 65 | + | * `traceroute domain: Prints the route that a packet takes to reach the domain.` |
| 66 | + | * `mtr domain: Combines the functionality of traceroute and ping.` |
| 67 | + | * `ss: Investigates sockets.` |
| 68 | + | * `nmap: Network exploration tool and security scanner.` |
| 69 | + | |
| 70 | + | ### <mark style="color:green;">Archives and Compression</mark>: |
| 71 | + | |
| 72 | + | * `tar cf file.tar files: Create a tar archive containing files.` |
| 73 | + | * `tar xf file.tar: Extract files from a tar archive.` |
| 74 | + | * `gzip file: Compresses a file.` |
| 75 | + | * `gzip -d file.gz: Decompresses a file.` |
| 76 | + | * `zip -r file.zip files: Create a zip archive.` |
| 77 | + | * `unzip file.zip: Extract the contents of a zip file.` |
| 78 | + | |
| 79 | + | ### <mark style="color:green;">Text Processing</mark>: |
| 80 | + | |
| 81 | + | * `grep pattern files: Search for a pattern in files.` |
| 82 | + | * `grep -r pattern dir: Search recursively for a pattern in a directory.` |
| 83 | + | * `echo 'text': Prints text.` |
| 84 | + | * `sed 's/string1/string2/g' filename: Replaces string1 with string2 in a file.` |
| 85 | + | * `diff file1 file2: Compares two files and shows the differences.` |
| 86 | + | * `wc filename: Count lines, words, and characters in a file.` |
| 87 | + | * `awk: A versatile programming language for working on files.` |
| 88 | + | * `sed -i 's/string1/string2/g' filename: Replace string1 with string2 in a file.` |
| 89 | + | * `cut -d':' -f1 /etc/passwd: Cut out the first field of each line in /etc/passwd.` |
| 90 | + | |
| 91 | + | ### <mark style="color:green;">Disk Usage</mark>: |
| 92 | + | |
| 93 | + | * `df: Shows disk usage.` |
| 94 | + | * `du: Shows directory space usage.` |
| 95 | + | * `free: Show memory and swap usage.` |
| 96 | + | * `whereis app: Show possible locations of an app.` |
| 97 | + | |
| 98 | + | ### <mark style="color:green;">System Info</mark>: |
| 99 | + | |
| 100 | + | * `date: Show the current date and time.` |
| 101 | + | * `cal: Show this month's calendar.` |
| 102 | + | * `uptime: Show current uptime.` |
| 103 | + | * `w: Display who is online.` |
| 104 | + | * `whoami: Who you are logged in as.` |
| 105 | + | * `uname -a: Show kernel information.` |
| 106 | + | * `df -h: Disk usage in a human-readable format.` |
| 107 | + | * `du -sh: Disk usage of the current directory in a human-readable format.` |
| 108 | + | * `free -m: Show free and used memory in MB.` |
| 109 | + | |
| 110 | + | ### <mark style="color:green;">Package Installations</mark>: |
| 111 | + | |
| 112 | + | * sudo `apt-get update: Updates package lists for upgrades.` |
| 113 | + | * `sudo apt-get upgrade: Upgrades all upgradable packages.` |
| 114 | + | * `sudo apt-get install pkgname: Install a package.` |
| 115 | + | * `sudo apt-get remove pkgname: Removes a package`. |
| 116 | + | |
| 117 | + | ### <mark style="color:green;">Others (mostly used in scripts)</mark>: |
| 118 | + | |
| 119 | + | * `command1 ; command2: Run command1 and then command2.` |
| 120 | + | * `command1 && command2: Run command2 if command1 is successful.` |
| 121 | + | * `command1 || command2: Run command2 if command1 is not successful.` |
| 122 | + | * `command &: Run command in the background.` |
| 123 | + | |
| 124 | + | ### <mark style="color:green;">Version Control (Git commands)</mark>: |
| 125 | + | |
| 126 | + | * `git init: Initialize a local Git repository.` |
| 127 | + | * `git clone url: Create a local copy of a remote repository.` |
| 128 | + | * `git add filename: Add a file to the staging area.` |
| 129 | + | * `git commit -m "Commit message": Commit changes with a message.` |
| 130 | + | * `git status: Check the status of the working directory.` |
| 131 | + | * `git pull: Pull latest changes from the remote repository.` |
| 132 | + | * `git push: Push changes to the remote repository.` |
| 133 | + | * `git branch: List all local branches.` |
| 134 | + | * `git branch branchname: Create a new branch.` |
| 135 | + | * `git checkout branchname: Switch to a branch.` |
| 136 | + | * `git merge branchname: Merge a branch into the active branch.` |
| 137 | + | * `git stash: Stash changes in a dirty working directory.` |
| 138 | + | * `git stash apply: Apply changes from a stash.` |
| 139 | + | * `git log: View commit history.` |
| 140 | + | * `git reset: Reset your HEAD pointer to a previous commit.` |
| 141 | + | * `git rm filename: Remove a file from version control.` |
| 142 | + | * `git rebase: Reapply commits on top of another base tip.` |
| 143 | + | * `git revert: Create a new commit that undoes all the changes made in a particular commit.` |
| 144 | + | * `git cherry-pick commitID: Apply the changes introduced by some existing commits.` |
| 145 | + | |
| 146 | + | ### <mark style="color:green;">Environment Variables</mark>: |
| 147 | + | |
| 148 | + | * `env: Display all environment variables.` |
| 149 | + | * `` `echo $V `` |
| 150 | + | |
| 151 | + | <mark style="color:green;">ARIABLE\`: Display the value of an environment variable</mark>. |
| 152 | + | |
| 153 | + | * `export VARIABLE=value: Set the value of an environment variable.` |
| 154 | + | * `alias new_command='old_command options': Create a new command that executes the old command with the specified options.` |
| 155 | + | * `echo $PATH: Print the PATH environment variable.` |
| 156 | + | * `export PATH=$PATH:/new/path: Add /new/path to the PATH.` |
| 157 | + | |
| 158 | + | ### <mark style="color:green;">Job Scheduling (Cron Jobs)</mark>: |
| 159 | + | |
| 160 | + | * `crontab -l: List all your cron jobs.` |
| 161 | + | * `crontab -e: Edit your cron jobs.` |
| 162 | + | * `crontab -r: Remove all your cron jobs.` |
| 163 | + | * `crontab -v: Display the last time you edited your cron jobs.` |
| 164 | + | * `crontab file: Install a cron job from a file.` |
| 165 | + | * `@reboot command: Schedule a job to run at startup.` |
| 166 | + | |
| 167 | + | ### <mark style="color:green;">Package Installations (using pip, a Python package installer)</mark>: |
| 168 | + | |
| 169 | + | * `pip install packagename: Install a Python package.` |
| 170 | + | * `pip uninstall packagename: Uninstall a Python package.` |
| 171 | + | * `pip freeze > requirements.txt: Freeze the installed packages into a requirements file.` |
| 172 | + | * `pip install -r requirements.txt: Install packages from a requirements file.` |
| 173 | + | |
| 174 | + | ### <mark style="color:green;">Shell Scripting</mark>: |
| 175 | + | |
| 176 | + | * `#!/bin/bash: Shebang line to specify the script interpreter.` |
| 177 | + | * `$0, $1, ..., $9, ${10}, ${11}: Script arguments.` |
| 178 | + | * `if [condition]; then ... fi: if statement in Bash scripts.` |
| 179 | + | * `for i in {1..10}; do ... done: for loop in Bash scripts.` |
| 180 | + | * `while [condition]; do ... done: while loop in Bash scripts.` |
| 181 | + | * `function name() {...}: Define a function.` |
| 182 | + | |
| 183 | + | ### <mark style="color:green;">System Monitoring and Performance</mark>: |
| 184 | + | |
| 185 | + | * `iostat: Reports CPU statistics and input/output statistics for devices, partitions, and network filesystems.` |
| 186 | + | * `vmstat: Reports information about processes, memory, paging, block IO, traps, disks, and CPU activity.` |
| 187 | + | * `htop: An interactive process viewer for Unix systems.` |
| 188 | + | |
| 189 | + | ### <mark style="color:green;">Search and Find</mark>: |
| 190 | + | |
| 191 | + | * `locate filename: Find a file by its name.` |
| 192 | + | * `whereis programname: Locate the binary, source, and manual page files for a command.` |
| 193 | + | * `which commandname: Shows the full path of shell commands.` |
| 194 | + | |
| 195 | + | ### <mark style="color:green;">Compression / Archives</mark>: |
| 196 | + | |
| 197 | + | * `tar -cvf archive.tar dirname/: Create a tar archive.` |
| 198 | + | * `tar -xvf archive.tar: Extract a tar archive.` |
| 199 | + | * `tar -jcvf archive.tar.bz2 dirname/: Create a compressed bz2 archive.` |
| 200 | + | * `tar -jxvf archive.tar.bz2: Extract a bz2 archive.` |
| 201 | + | |
| 202 | + | ### <mark style="color:green;">Disk Usage</mark>: |
| 203 | + | |
| 204 | + | * `dd if=/dev/zero of=/tmp/output.img bs=8k count=256k: Create a file of a certain size for testing disk speed.` |
| 205 | + | * `hdparm -Tt /dev/sda: Measure the read speed of your hard drive.` |
| 206 | + | |
| 207 | + | ### <mark style="color:green;">Others</mark>: |
| 208 | + | |
| 209 | + | * `yes > /dev/null &: Use this command to push a system to its limit.` |
| 210 | + | * `:(){ :|:& };::: A fork bomb – handle with care. Do not run this command on a production system.` |
| 211 | + | |
| 212 | + | Remember, you can always use the `man` command (e.g., `man ls`) to get more information about each command. |
| 213 | + | |