# Viewing File Contents — Linux

Source: https://www.geekswithgeeks.com/en/linux/linux-viewing-files

> cat, less, head and tail.

## cat — dump the whole file

`cat` prints a file's entire contents to the screen — fine for short files.

```bash
cat /etc/hostname
```

Output:

```
ubuntu-server
```

## less — page through big files

`less` opens a file for scrollable viewing without loading it all into memory. Press `q` to quit, `/word` to search.

```bash
less /var/log/syslog
```

## head & tail

`head` shows the first lines, `tail` the last. `tail -f` follows a growing file live — great for watching logs.

```bash
head -n 5 access.log
tail -f access.log
```

**Quiz:** Which command lets you watch a log file update live?

- [ ] cat log.txt
- [x] tail -f log.txt
- [ ] head log.txt

*Answer:* tail -f log.txt. `tail -f` keeps the file open and prints new lines as they're appended.
