2.1 Files, Text, and Pipes
The Linux command line is excellent at text processing. Logs, configuration, CSV files, environment variables, and command output can all become lines of text passed through small tools.
File operation commands
| Command | Purpose |
|---|---|
cp source target | Copy |
mv old new | Move or rename |
rm file | Remove a file |
rm -r dir | Recursively remove a directory |
mkdir -p path/to/dir | Create nested directories |
touch file | Create an empty file or refresh timestamp |
Note
rm -r recursively removes directories. In real environments, always run pwd and ls first to confirm where you are.Redirection
bash
echo "hello" > note.txt
echo "second line" >> note.txt
cat note.txt> overwrites a file. >> appends to the end. This difference matters a lot.
Standard input, output, and error
Most commands have three common streams:
- stdin: standard input, the data a command reads.
- stdout: standard output, normal command output.
- stderr: standard error, diagnostic and error output.
The pipe | sends the stdout of the left command into the stdin of the right command.
Loading concept check...
grep, sort, uniq, wc
bash
grep ERROR app.log
grep ERROR app.log | wc -l
grep ERROR app.log | awk '{print $3}' | sort | uniq -cThese commands are often chained:
grep: filter matching lines.awk: extract fields or perform small text transformations.sort: sort lines.uniq -c: count adjacent duplicate lines, usually aftersort.wc -l: count lines.
Loading interactive lab...
Loading concept check...
How to think in command combinations
Do not search for a single universal command. Linux encourages small tools connected together:
1. Get text. 2. Filter the lines you need. 3. Extract the fields you need. 4. Sort, count, or summarize.
This is the basic shape of many server diagnostics, data cleanup tasks, and log analyses.
Loading practice...