|
| 1 | +# Linux Commands Cheat Sheet |
| 2 | + |
| 3 | +## File and Directory Management |
| 4 | + |
| 5 | +### Navigation |
| 6 | + |
| 7 | +- Change directory: |
| 8 | + `cd /path/to/directory` |
| 9 | +- Go to the home directory: |
| 10 | + `cd ~` |
| 11 | +- Go to the previous directory: |
| 12 | + `cd -` |
| 13 | +- List files and directories: |
| 14 | + `ls` |
| 15 | +- List detailed file info: |
| 16 | + `ls -l` |
| 17 | +- List hidden files: |
| 18 | + `ls -a` |
| 19 | + |
| 20 | +### File Operations |
| 21 | + |
| 22 | +- Copy files: |
| 23 | + `cp source destination` |
| 24 | +- Copy directories: |
| 25 | + `cp -r source_dir destination_dir` |
| 26 | +- Move or rename files: |
| 27 | + `mv source destination` |
| 28 | +- Delete files: |
| 29 | + `rm file.txt` |
| 30 | +- Delete directories: |
| 31 | + `rm -r directory_name` |
| 32 | +- Create a new file: |
| 33 | + `touch file.txt` |
| 34 | +- Create a new directory: |
| 35 | + `mkdir directory_name` |
| 36 | +- Remove an empty directory: |
| 37 | + `rmdir directory_name` |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## File Viewing and Editing |
| 42 | + |
| 43 | +### Viewing Files |
| 44 | + |
| 45 | +- Display file content: |
| 46 | + `cat file.txt` |
| 47 | +- View file content one page at a time: |
| 48 | + `less file.txt` |
| 49 | +- Display the first 10 lines of a file: |
| 50 | + `head file.txt` |
| 51 | +- Display the last 10 lines of a file: |
| 52 | + `tail file.txt` |
| 53 | +- Follow a file in real-time: |
| 54 | + `tail -f file.txt` |
| 55 | + |
| 56 | +### Editing Files |
| 57 | + |
| 58 | +- Edit files using `nano`: |
| 59 | + `nano file.txt` |
| 60 | +- Edit files using `vim`: |
| 61 | + `vim file.txt` |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +## File Permissions and Ownership |
| 66 | + |
| 67 | +### Changing Permissions |
| 68 | + |
| 69 | +- Change file permissions (e.g., `rwx`): |
| 70 | + `chmod 755 file.txt` |
| 71 | +- Change directory permissions recursively: |
| 72 | + `chmod -R 755 directory_name` |
| 73 | + |
| 74 | +### Changing Ownership |
| 75 | + |
| 76 | +- Change file owner: |
| 77 | + `chown user file.txt` |
| 78 | +- Change file owner and group: |
| 79 | + `chown user:group file.txt` |
| 80 | +- Change ownership recursively: |
| 81 | + `chown -R user:group directory_name` |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | +## Searching and Finding Files |
| 86 | + |
| 87 | +### File Search |
| 88 | + |
| 89 | +- Search for a file by name: |
| 90 | + `find /path -name "filename"` |
| 91 | +- Search for files by extension: |
| 92 | + `find /path -name "*.txt"` |
| 93 | + |
| 94 | +### Content Search |
| 95 | + |
| 96 | +- Search for text in a file: |
| 97 | + `grep 'pattern' file.txt` |
| 98 | +- Search recursively in a directory: |
| 99 | + `grep -r 'pattern' /path` |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## Disk and System Information |
| 104 | + |
| 105 | +### Disk Usage |
| 106 | + |
| 107 | +- Check disk space usage: |
| 108 | + `df -h` |
| 109 | +- Check directory or file size: |
| 110 | + `du -sh /path` |
| 111 | + |
| 112 | +### System Information |
| 113 | + |
| 114 | +- Display system information: |
| 115 | + `uname -a` |
| 116 | +- Show current logged-in users: |
| 117 | + `who` |
| 118 | +- Display memory usage: |
| 119 | + `free -h` |
| 120 | +- Show running processes: |
| 121 | + `top` |
| 122 | +- Display detailed process list: |
| 123 | + `ps aux` |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## Networking |
| 128 | + |
| 129 | +### Network Configuration |
| 130 | + |
| 131 | +- Display IP address: |
| 132 | + `ip addr` |
| 133 | +- Test network connectivity: |
| 134 | + `ping example.com` |
| 135 | +- Display open network ports: |
| 136 | + `netstat -tuln` |
| 137 | + |
| 138 | +### File Transfers |
| 139 | + |
| 140 | +- Copy files between systems: |
| 141 | + `scp file.txt user@remote:/path` |
| 142 | +- Download files using `wget`: |
| 143 | + `wget http://example.com/file.zip` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## Process Management |
| 148 | + |
| 149 | +### Process Operations |
| 150 | + |
| 151 | +- Show current running processes: |
| 152 | + `ps aux` |
| 153 | +- Kill a process by PID: |
| 154 | + `kill PID` |
| 155 | +- Kill a process by name: |
| 156 | + `pkill process_name` |
| 157 | + |
| 158 | +### Background and Foreground |
| 159 | + |
| 160 | +- Run a command in the background: |
| 161 | + `command &` |
| 162 | +- List background jobs: |
| 163 | + `jobs` |
| 164 | +- Bring a background job to the foreground: |
| 165 | + `fg %job_number` |
| 166 | + |
| 167 | +--- |
| 168 | + |
| 169 | +## User Management |
| 170 | + |
| 171 | +### User and Group Operations |
| 172 | + |
| 173 | +- Add a new user: |
| 174 | + `sudo useradd username` |
| 175 | +- Change a user's password: |
| 176 | + `sudo passwd username` |
| 177 | +- Add a user to a group: |
| 178 | + `sudo usermod -aG groupname username` |
| 179 | + |
| 180 | +### Switching Users |
| 181 | + |
| 182 | +- Switch to another user: |
| 183 | + `su - username` |
| 184 | +- Execute a command as another user: |
| 185 | + `sudo command` |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +## Archiving and Compression |
| 190 | + |
| 191 | +### Creating Archives |
| 192 | + |
| 193 | +- Create a `.tar` archive: |
| 194 | + `tar -cvf archive.tar file_or_directory` |
| 195 | +- Create a compressed `.tar.gz` archive: |
| 196 | + `tar -czvf archive.tar.gz file_or_directory` |
| 197 | + |
| 198 | +### Extracting Archives |
| 199 | + |
| 200 | +- Extract a `.tar` archive: |
| 201 | + `tar -xvf archive.tar` |
| 202 | +- Extract a `.tar.gz` archive: |
| 203 | + `tar -xzvf archive.tar.gz` |
| 204 | + |
| 205 | +--- |
| 206 | + |
| 207 | +## Package Management |
| 208 | + |
| 209 | +### Debian/Ubuntu (APT) |
| 210 | + |
| 211 | +- Update package list: |
| 212 | + `sudo apt update` |
| 213 | +- Upgrade all packages: |
| 214 | + `sudo apt upgrade` |
| 215 | +- Install a package: |
| 216 | + `sudo apt install package_name` |
| 217 | +- Remove a package: |
| 218 | + `sudo apt remove package_name` |
| 219 | + |
| 220 | +### Red Hat/CentOS (YUM/DNF) |
| 221 | + |
| 222 | +- Install a package: |
| 223 | + `sudo yum install package_name` |
| 224 | + `sudo dnf install package_name` |
| 225 | +- Remove a package: |
| 226 | + `sudo yum remove package_name` |
| 227 | + `sudo dnf remove package_name` |
| 228 | + |
| 229 | +### Arch Linux (Pacman) |
| 230 | + |
| 231 | +- Update package list: |
| 232 | + `sudo pacman -Sy` |
| 233 | +- Upgrade all packages: |
| 234 | +- Install a package: |
| 235 | + `sudo pacman -S package_name` |
| 236 | +- Remove a package: |
| 237 | +- `sudo pacman -R package_name` |
| 238 | +- Search for a package: |
| 239 | + `pacman -Ss search_term` |
| 240 | + |
| 241 | +### Arch Linux (yay) |
| 242 | + |
| 243 | +- Install a package: |
| 244 | + `yay -S package_name` |
| 245 | +- Remove a package: |
| 246 | + `yay -R package_name` |
| 247 | +- Search for a package: |
| 248 | + `yay -Ss search_term` |
| 249 | + |
| 250 | +--- |
| 251 | + |
| 252 | +## Shell Scripting |
| 253 | + |
| 254 | +### Variables and Scripts |
| 255 | + |
| 256 | +- Assign a variable: |
| 257 | + `variable_name="value"` |
| 258 | +- Print a variable: |
| 259 | + `echo $variable_name` |
| 260 | +- Run a shell script: |
| 261 | + `bash script.sh` |
| 262 | + |
| 263 | +### Loops and Conditions |
| 264 | + |
| 265 | +- Example `for` loop: |
| 266 | + |
| 267 | + ```bash |
| 268 | + for i in {1..5}; do |
| 269 | + echo $i |
| 270 | + done |
| 271 | + ``` |
| 272 | + |
| 273 | +- Example `if` statement: |
| 274 | + |
| 275 | + ```bash |
| 276 | + if [ condition ]; then |
| 277 | + echo "Condition met" |
| 278 | + fi |
| 279 | + ``` |
| 280 | + |
| 281 | +--- |
| 282 | + |
| 283 | +## Miscellaneous |
| 284 | + |
| 285 | +### History and Aliases |
| 286 | + |
| 287 | +- Show command history: |
| 288 | + `history` |
| 289 | +- Create a command alias: |
| 290 | + `alias ll='ls -la'` |
| 291 | + |
| 292 | +### Permissions and Ownership |
| 293 | + |
| 294 | +- Display current working directory: |
| 295 | + `pwd` |
| 296 | +- Change the hostname: |
| 297 | + `sudo hostname new_hostname` |
0 commit comments