Lesson 5 / 8
Module 3Lesson 1⏱ 55 min study

Lesson 5: Command-Line Foundations: Streams, Navigation, and File Manipulation

Deconstruct standard terminal command syntax, navigate directory trees, manipulate files and directories, leverage stream redirection, and construct powerful pipelines using grep and text filters.

✦ Key Architectural Takeaways
  • •Linux commands follow a standard grammar: command [options/flags] [arguments], with options altering behavior and arguments specifying targets.
  • •The shell prompt displays vital context: username@hostname:current_working_directory$ ($ indicates standard user; # indicates superuser).
  • •Absolute paths specify the full trail from root (e.g., /etc/network/interfaces); relative paths specify location relative to the current working directory (e.g., ../configs).
  • •Every Linux process possesses three default I/O streams: Standard Input (stdin, FD 0), Standard Output (stdout, FD 1), and Standard Error (stderr, FD 2).
  • •The pipe operator (|) connects the standard output of one command directly into the standard input of another, enabling powerful modular data processing pipelines.

1. Anatomy of the Linux Terminal Command

When you open a terminal in Linux Mint, you are interacting with GNU Bash (Bourne Again Shell). The command prompt presents critical system context:

TEXTSYSTEM SCHEMATIC
anupam@mint-workstation:~/projects$ ls -la /var/log
[---1--] [------2------] [---3----] [4] [5] [---6---]
  1. 1Current User: anupam
  2. 2Hostname: mint-workstation
  3. 3Current Directory: ~/projects (~ is shorthand for the user's home directory /home/anupam)
  4. 4Command: ls (list directory contents)
  5. 5Options / Flags: -la (-l for long detailed format, -a for all files including hidden dotfiles)
  6. 6Argument: /var/log (the target path)
§

2. Directory Navigation & Inspection Mastery

BASHSYSTEM SCHEMATIC
# Print Working Directory
pwd

# List contents with human-readable file sizes and timestamps
ls -lh

# List all files including hidden configuration dotfiles (.bashrc, .profile)
ls -la

# Change directory using absolute and relative paths
cd /var/log              # Absolute path to /var/log
cd ..                    # Move up one level into /var
cd ../etc                # Move into sibling directory /etc
cd ~                     # Return to current user's home directory
cd -                     # Toggle back to previous working directory
§

3. File & Directory Manipulation

Linux treats almost everything as a stream of bytes or a file. The core utilities for creating, inspecting, copying, and removing files include:

BASHSYSTEM SCHEMATIC
# Create a new empty file or update timestamps
touch notes.txt

# Create nested directory trees in a single command
mkdir -p projects/python/scripts

# View entire file contents
cat /etc/os-release

# Paginate large files with search capabilities (press 'q' to exit, '/' to search)
less /var/log/syslog

# View top 10 lines or continuously stream new log entries in real time
head -n 10 /etc/passwd
tail -f /var/log/syslog

# Copy files and directories recursively
cp notes.txt backup_notes.txt
cp -r projects/ projects_backup/

# Move or rename files
mv notes.txt ideas.txt

# Remove files and directories safely
rm ideas.txt
rm -r projects_backup/     # Recursive directory deletion
rm -rf temp_folder/        # Force recursive deletion (use with extreme caution!)
§

4. Standard Streams & I/O Redirection

Every process launched by the shell is automatically provided with three standard file descriptors:

TEXTSYSTEM SCHEMATIC
                 +-----------------------+
  [ stdin (0) ]  |                       |  [ stdout (1) ] -> Terminal Screen
  -------------> |     Linux Process     | =================> File ( > or >> )
  Keyboard       |       (e.g., cat)     |
                 |                       |  [ stderr (2) ] -> Error Log
                 +-----------------------+ =================> ( 2> or 2>&1 )

Redirection Operator Syntax

OperatorStreamBehaviorExample
>stdout (1)Overwrite target file with command outputls -l > inventory.txt
>>stdout (1)Append command output to end of filedate >> audit.log
<stdin (0)Feed file contents as input into commandwc -l < inventory.txt
2>stderr (2)Redirect only error messages to filecat invalid.txt 2> error.log
&> or 2>&1stdout & stderrMerge both standard output and errors into filemake build &> build.log
§

5. Pipelines & Text Processing Tools

The true power of the Unix philosophy is chaining simple, single-purpose tools together using the pipe operator (|):

BASHSYSTEM SCHEMATIC
# Find all active users with /bin/bash shell
cat /etc/passwd | grep "/bin/bash"

# Count the number of system packages installed
dpkg -l | grep "^ii" | wc -l

# Search for the word 'error' in syslog, ignore case, and sort unique occurrences
grep -i "error" /var/log/syslog | sort | uniq -c | sort -rn | head -n 5

# Locate all PDF files modified in the user's Downloads folder
find ~/Downloads -type f -name "*.pdf"