0% found this document useful (0 votes)
19 views11 pages

Good Luck Guys With This Shit

This document provides a comprehensive guide to Linux commands for memory, process, and file management, as well as vi/vim and echo commands. It includes examples for each command, covering functionalities such as memory monitoring, process control, file manipulation, and text editing. Additionally, it explains the structure of system files like /etc/hosts and /etc/passwd.

Uploaded by

drawingsfiras
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views11 pages

Good Luck Guys With This Shit

This document provides a comprehensive guide to Linux commands for memory, process, and file management, as well as vi/vim and echo commands. It includes examples for each command, covering functionalities such as memory monitoring, process control, file manipulation, and text editing. Additionally, it explains the structure of system files like /etc/hosts and /etc/passwd.

Uploaded by

drawingsfiras
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Linux Commands for Memory, Process, and File Management

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.

watch -n 1 cat /proc/meminfo


Monitors memory usage live by refreshing every second.

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

1. Opening and Closing Files

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

vi/vim operates in three modes:


- **Normal Mode**: For navigating and manipulating text. Default mode when you open
vim.
- **Insert Mode**: For inserting text. Enter by pressing i (insert) or a (append). Exit by
pressing Esc.
- **Command-Line Mode**: For saving, quitting, and other advanced commands. Enter by
typing : in Normal Mode.

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.

**Copy and Paste**:


- yy → Copy (yank) the current line.
- y$ → Copy from the cursor to the end of the line.
- p → Paste after the cursor.
- P → Paste before the cursor.

**Undo and Redo**:


- u → Undo the last change.
- Ctrl-r → Redo the undone change.

5. Searching and Replacing

**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

**Enter Visual Mode**:


- v → Character-wise selection.
- V → Line-wise selection.
- Ctrl-v → Block (column) selection.

**Actions in Visual Mode**:


- d → Delete selected text.
- y → Copy (yank) selected text.
- > → Indent selected text.
- < → Unindent selected text.

7. Working with Multiple Files

**Open Multiple Files**:


- vim file1 file2 file3 → Open multiple files.
**Switch Files**:
- :n → Go to the next file.
- :prev → Go to the previous file.
- :first → Go to the first file.
- :last → Go to the last file.

**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. Common Use Cases

1. **Print Text to the Terminal**:


```bash
echo "Hello, World!"
```
Outputs: Hello, World!

2. **Display Variables**:
```bash
name="John"
echo "My name is $name"
```
Outputs: My name is John

3. **Print Special Characters**:


To prevent special characters from being interpreted, use quotes:
```bash
echo "This is \$100"
```
Outputs: This is $100

4. **Combine Commands**:
```bash
echo "Today is: $(date)"
```
Outputs: Today is: <current date and time>

3. Common Options

1. `-n`: Suppress the Trailing Newline


Normally, `echo` adds a newline at the end. Use `-n` to avoid this:
```bash
echo -n "Hello"
echo "World"
```
Outputs: HelloWorld

2. `-e`: Enable Interpretation of Backslash Escapes


Useful for special formatting:
```bash
echo -e "Line1\nLine2\nLine3"
```
Outputs:
```
Line1
Line2
Line3
```

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. `--help`: Display Help


```bash
echo --help
```
Shows available options and usage.

4. Writing to a File

1. **Overwrite File Contents**:


```bash
echo "This is a test" > file.txt
```
Creates (or overwrites) file.txt with the text `This is a test`.

2. **Append to a File**:
```bash
echo "Appending this line" >> file.txt
```
Adds the text `Appending this line` to the end of file.txt.

3. **Combine with Commands**:


Example: Save the current date to a file:
```bash
echo "The date is: $(date)" > date.txt
```

5. Example Use Cases

```bash
#!/bin/bash
echo "Starting the script..."
name="Alice"
echo "Hello, $name!"
```

Add an entry to `/etc/hosts` (requires sudo):


```bash
echo "127.0.0.1 mywebsite.local" | sudo tee -a /etc/hosts
```

Combine Commands for Dynamic Output:


```bash
echo "User: $(whoami), Date: $(date), Uptime: $(uptime)"
```

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`).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy