# Linux Essentials

Working with Linux is mostly about using commands — and knowing what they do, how to tweak them with flags, and where things live on disk. This article collects the fundamentals you’ll use every day: how flags and manuals work, how to create/copy/move/delete files and folders, how to read permissions and switch users, and what the important root directories (/etc, /var, /root, /tmp) contain and mean for security.

---

## **TL;DR**

* Flags change a command’s behaviour (-a vs --all). Use --help and man to learn options.
    
* Create files with touch, directories with mkdir. Copy with cp, move/rename with mv, delete with rm.
    
* Read permissions with ls -l (r, w, x for owner/group / others). Use su or su -l to switch users.
    
* /etc holds config (sensitive), /var holds logs/data, /root is root’s home, /tmp is world-writable (sticky bit).
    

---

## **1) Flags, switches, and manuals — how commands accept options**

Short-form flags start with a single hyphen followed by a letter: -a.

Long form switches start with two hyphens and full words: --all.

Quick ways to learn a command:

```bash
# short help summary
ls --help

# full manual (press q to quit)
man ls
```

Example: `ls` lists directory contents by default. `ls` `-a` or `ls —all` shows hidden files (those that begin with .).

---

## **2) Filesystem basics — create, copy, move, delete, inspect**

### **Create files and directories**

```python
# create an empty file
touch note

# create a directory
mkdir mydirectory
```

touch makes a blank file; use `echo`, `cat >`, or a text editor (nano, vim) to add content.

### **Copy and move**

```python
# copy file
cp source_file dest_file

# copy directory recursively
cp -r source_dir dest_dir

# move or rename file/directory
mv oldname newname
mv file.txt /path/to/folder/
```

### **Remove (be careful)**

```python
# delete a file
rm file.txt

# delete a directory and its contents (destructive)
rm -R directory_name

# safer: prompt before deleting
rm -i file.txt
```

### **Determine a file’s type**

```python
file filename
# example output: "note: ASCII text"
```

---

## **3) Permissions 101 — reading** 

## **ls -l**

Run:

```python
ls -lh
```

Sample line:

```python
-rw-r--r-- 1 alice developers 1024 Feb 19 10:37 note
```

Breakdown:

* First character: file type (- file, d directory, l symlink).
    
* Next nine characters: permissions in three groups of three:
    
    * Owner (user): rw- → read, write, no execute
        
    * Group: r-- → read only
        
    * Others: r-- → read only
        
    

Permission symbols: r (read), w (write), x (execute), - (no permission).

Useful commands:

```python
stat file.txt            # show detailed mode/owner/timestamps
chmod 644 file.txt       # owner rw, group r, others r
chmod 755 script.sh      # owner rwx, group rx, others rx
sudo chown user:group file.txt  # change owner and group
```

---

## **4) Switching users**

su allows you to become another user (requires that user’s password unless you are root).

```python
# switch to user2 (keeps current environment)
su user2

# full login shell (use home directory and environment of the target user)
su -l user2
# or
su --login user2
```

su -l drops you into the target user’s home and loads their login environment (PATH, shell init files, etc.).

---

## **5) Common root directories — purpose, importance, and quick checks**

### **/etc**

###  **— system configuration**

* Stores service and system config: network, auth, service configs.
    
* Files of interest: /etc/passwd (accounts metadata), /etc/shadow (password hashes, root-only), /etc/sudoers and /etc/sudoers.d (sudo rules).
    

```python
ls -l /etc | head
cat /etc/passwd
sudo cat /etc/shadow   # requires root
```

**Security note:** /etc/shadow is sensitive — access implies root-level compromise.

---

### **/var**

###  **— variable data and logs**

* Contains logs (/var/log), caches, databases, spool files.
    
* Logs show system and app activity — useful for debugging and auditing.
    

```python
ls -lh /var/log | head
sudo tail -n 200 /var/log/syslog
# or on systemd systems:
sudo journalctl -n 200
```

**Security note:** misconfigured apps may leak secrets into logs.

---

### **/root**

###  **— root user’s home**

* The admin’s home directory. Not /home/root.
    

```python
ls -la /root    # normally permission denied unless root
```

**Security note:** if you can read /root, you likely have root access.

---

### **/tmp**

###  **— temporary files (world-writable)**

* Meant for short-lived files; usually cleared on reboot.
    
* Typically has sticky bit: drwxrwxrwt — anyone can create files, only owner/root can delete them.
    

```python
ls -ld /tmp
[ -w /tmp ] && echo "/tmp writable" || echo "/tmp not writable"
mktemp /tmp/mytmp.XXXXXX  # create a secure temporary file
```

**Security note:** never trust scripts or files placed in /tmp; prefer mktemp for temporary files.

---

## **6) Compact cheatsheet (copy/paste)**

```python
# Learn command options
<cmd> --help
man <cmd>

# File & directory ops
touch file.txt
mkdir folder
cp source dest
cp -r sourcedir destdir
mv oldname newname
rm file.txt
rm -R directory_name
rm -i file.txt

# File info
file file.txt
ls -lh
ls -la

# Permissions & ownership
stat file.txt
chmod 644 file.txt
chmod 755 script.sh
sudo chown user:group file.txt

# Switch users
su otheruser
su -l otheruser

# Important directories quick checks
ls -l /etc /var /root /tmp
ls -lh /var/log | head
sudo tail -n 200 /var/log/syslog
ls -ld /tmp

# Security-focused discovery (use with sudo)
sudo find / -perm -4000 -type f 2>/dev/null | head
sudo find / -xdev -type d -perm -0002 -ls 2>/dev/null | head
```

---

## **7) Security reminders & best practices**

* Treat /etc/shadow, SSH keys, and config files as high-value targets. Protect them.
    
* rm -R (or rm -rf) is destructive — double-check paths before running.
    
* Use mktemp to create unpredictable temporary filenames (avoid race conditions).
    
* Logs in /var/log may contain secrets; don’t expose them in public repos.
    
* Prefer sudo for temporary elevation; avoid running a long-lived root shell unless necessary.
    

---

## **8) Example workflow (quick practical scenario)**

1. Inspect the current directory and hidden files:
    

```python
ls -la
```

2. Create a workspace, move a file into it, and verify:
    

```python
mkdir workspace
mv note workspace/
ls -lh workspace
```

3. Check if the file is text and read it:
    

```python
file workspace/note
cat workspace/note
```

4. If you need elevated config access, view /etc/sudoers safely:
    

```python
sudo visudo   # edits /etc/sudoers with syntax checking
```

---

## **Final notes**

This guide gives you a concise, practical foundation: flags and manuals, the basic file operations you’ll use daily, how to read and change permissions safely, and the root directories you should know about.
