Working With Files

In the terminal, you often need to quickly check what is inside a file without opening a full text editor like Nano. Linux provides several commands for this.

1. cat (Concatenate)

The cat command displays the entire contents of a file on the screen.

Bash

cat file.txt

2. head (View Top)

The head command shows you the first few lines of a file (default is 10). This is great for large files where you only want to see the header.

Bash

# View first 10 lines
head file.txt

# View first 5 lines
head -n 5 file.txt

3. tail (View Bottom)

The tail command is the opposite of head—it shows the last few lines of a file.

Bash

# View last 10 lines
tail file.txt

# View last 20 lines
tail -n 20 file.txt

4. less (Scrollable View)

For massive files that don't fit on your screen, use less. It opens the file in a scrollable view.

5. wc (Word Count)

The wc command counts the number of lines, words, and characters in a file.

Bash

# Get line count only
wc -l file.txt