Good Luck Guys With This Shit
Good Luck Guys With This Shit
1. Memory Management
free
Displays the amount of free and used memory in the system.
Example: free -h (shows memory in human-readable format).
top
Provides a real-time view of processes and their memory/CPU usage.
Press 'M' within top to sort by memory usage.
htop
An interactive process viewer, like top, but more user-friendly.
Install with: sudo apt install htop.
vmstat
Displays virtual memory statistics.
Example: vmstat 2 (updates stats every 2 seconds).
sar
Collects, reports, and saves system activity (including memory usage).
Example: sar -r (shows memory usage statistics).
cat /proc/meminfo
Displays detailed information about memory usage.
dmesg
Prints kernel ring buffer messages, useful for debugging memory issues.
Example: dmesg | grep memory.
swapoff / swapon
Enables (swapon) or disables (swapoff) swap space.
Example: sudo swapoff -a (disables all swap).
2. Process Management
ps
Displays information about active processes.
Example: ps aux (shows all processes with detailed info).
top
Monitors real-time processes and their resource usage.
htop
Provides an enhanced interactive view of processes.
kill
Terminates a process by its PID.
Example: kill 12345 (kills process with PID 12345).
killall
Kills all processes with a specific name.
Example: killall firefox (kills all firefox processes).
pkill
Sends signals to processes based on name or other attributes.
Example: pkill -9 firefox.
jobs
Lists all jobs started in the current shell.
fg
Brings a background job to the foreground.
Example: fg %1 (resumes job 1).
bg
Resumes a suspended job in the background.
Example: bg %2.
nice / renice
Adjusts the priority of a process.
Example: nice -n 10 ./script.sh (starts a script with lower priority).
Example: renice 5 12345 (changes priority of process 12345).
pidof
Finds the PID of a process by its name.
Example: pidof apache2.
strace
Debugs a process by tracing system calls and signals.
Example: strace -p 12345 (traces process with PID 12345).
lsof
Lists open files associated with processes.
Example: lsof -p 12345 (lists files opened by process 12345).
systemctl
Manages system services (e.g., start, stop, enable services).
Example: systemctl restart apache2.
service
Older command to manage services.
Example: service nginx status.
3. File Management
ls
Lists files and directories.
Example: ls -l (detailed list), ls -a (includes hidden files).
cd
Changes the current working directory.
Example: cd /var/www.
pwd
Prints the current working directory.
mkdir
Creates a new directory.
Example: mkdir myfolder.
rmdir
Removes empty directories.
Example: rmdir myfolder.
rm
Deletes files and directories.
Example: rm file.txt, rm -r folder (removes a directory recursively).
cp
Copies files or directories.
Example: cp file1.txt file2.txt (copies file1 to file2).
mv
Moves or renames files/directories.
Example: mv oldname.txt newname.txt.
touch
Creates an empty file or updates the timestamp of an existing file.
Example: touch newfile.txt.
stat
Displays detailed information about a file.
Example: stat file.txt.
find
Searches for files and directories.
Example: find / -name 'file.txt' (searches for file.txt in root).
locate
Quickly finds files by name using a pre-built database.
Example: locate file.txt.
df
Displays disk space usage of file systems.
Example: df -h (shows human-readable sizes).
du
Shows disk usage for files and directories.
Example: du -sh folder/ (summarized size of a folder).
cat
Displays the contents of a file.
Example: cat file.txt.
more / less
Paginates the contents of a file for easier reading.
Example: less file.txt.
head / tail
Displays the first/last lines of a file.
Example: head -n 10 file.txt (first 10 lines).
Example: tail -f log.txt (real-time log monitoring).
chmod
Changes file permissions.
Example: chmod 755 script.sh.
chown
Changes file ownership.
Example: chown user:group file.txt.
ln
Creates symbolic or hard links.
Example: ln -s /path/to/file linkname.
tar
Archives files.
Example: tar -cvf archive.tar folder/.
zip / unzip
Compresses and extracts .zip files.
Example: zip archive.zip file.txt, unzip archive.zip.
scp
Securely copies files to/from remote machines.
Example: scp file.txt user@host:/path.
rsync
Synchronizes files/directories locally or remotely.
Example: rsync -av source/ destination/.
4. Vi/Vim Commands
vi filename or vim filename: Opens a file in the vi/vim editor. If the file does not exist, it
creates a new file.
Exit Commands:
- :q → Quit.
- :q! → Quit without saving.
- :wq or ZZ → Save and quit.
- :w → Save changes without exiting.
2. Modes in vi/vim
3. Navigation Commands
- h → Move left.
- l → Move right.
- j → Move down.
- k → Move up.
**Word Movement**:
- w → Move to the beginning of the next word.
- e → Move to the end of the current/next word.
- b → Move to the beginning of the previous word.
**Line Movement**:
- 0 → Move to the beginning of the line.
- ^ → Move to the first non-blank character of the line.
- $ → Move to the end of the line.
**Paragraph Movement**:
- { → Move to the beginning of the current paragraph.
- } → Move to the end of the current paragraph.
**Screen Movement**:
- Ctrl-d → Scroll down half a screen.
- Ctrl-u → Scroll up half a screen.
- Ctrl-f → Scroll forward a full screen.
- Ctrl-b → Scroll backward a full screen.
**Specific Line**:
- G → Move to the last line.
- gg → Move to the first line.
- :n → Move to line n.
4. Editing Commands
**Insert Text**:
- i → Insert before the cursor.
- I → Insert at the beginning of the line.
- a → Append after the cursor.
- A → Append at the end of the line.
- o → Open a new line below the current line.
- O → Open a new line above the current line.
**Delete Text**:
- x → Delete the character under the cursor.
- X → Delete the character before the cursor.
- dd → Delete the current line.
- d$ → Delete from the cursor to the end of the line.
- d0 → Delete from the cursor to the beginning of the line.
**Searching**:
- /pattern → Search forward for pattern.
- ?pattern → Search backward for pattern.
- n → Repeat the search in the same direction.
- N → Repeat the search in the opposite direction.
**Replacing**:
- :%s/old/new/g → Replace all occurrences of old with new in the file.
- :n,m s/old/new/g → Replace all occurrences of old with new between lines n and m.
6. Visual Mode
**Split Window**:
- :split filename → Open another file in a split window.
- Ctrl-w + arrow key → Navigate between split windows.
5. Echo Commands
1. Basic Syntax
bash
echo [options] [string]
2. **Display Variables**:
```bash
name="John"
echo "My name is $name"
```
Outputs: My name is John
4. **Combine Commands**:
```bash
echo "Today is: $(date)"
```
Outputs: Today is: <current date and time>
3. Common Options
3. **Backslash Escapes**:
| Escape Sequence | Description |
|------------------|--------------------|
| `\n` | Newline |
| `\t` | Tab |
| `\r` | Carriage return |
| `\\` | Backslash |
| `\"` | Double quote |
| `\b` | Backspace |
Example:
```bash
echo -e "Hello\tWorld!"
```
Outputs: Hello World!
4. Writing to a File
2. **Append to a File**:
```bash
echo "Appending this line" >> file.txt
```
Adds the text `Appending this line` to the end of file.txt.
```bash
#!/bin/bash
echo "Starting the script..."
name="Alice"
echo "Hello, $name!"
```
Format Output:
```bash
echo -e "Name\tAge\nAlice\t25\nBob\t30"
```
Outputs:
```
Name Age
Alice 25
Bob 30
```
/etc/hosts
In editors like `vi` or `vim`, the characters `~` at the beginning of the lines indicate empty or
unused lines outside the actual file content.
Explanation:
These lines do not belong to the file itself. They are only a visual indicator to show that there
is nothing after the end of the data.
/etc/passwd
The `/etc/passwd` file is a text file used to manage user information on the system.
It contains essential information about users, but passwords are protected and stored in a
separate file (`/etc/shadow`).