Complete Guide to Linux Directory Structure and Redirection Operations

As an open-source Unix-like operating system, Linux has become the preferred system for servers and development environments due to its stability, security, and flexibility. This article will delve into the Linux directory structure and redirection mechanisms, helping readers master these two core concepts.

Overview of Linux System Architecture

Hierarchy of the Operating System

Modern computer systems follow a layered design principle:

User → Application → Operating System → Hardware

The specific layers of the Linux system are:

Application Layer (e.g., X Window graphical interface) Shell Layer (command line interface) Kernel Layer (Kernel) Hardware Layer (Hardware)

Popular Linux Distributions

Commercial Distributions:

§Red Hat Enterprise Linux (RHEL) – Enterprise-level commercial distribution

§SUSE Linux – User-friendly graphical interface, suitable for desktop use

Community-Maintained Distributions:

§CentOS/Fedora – Free version based on Red Hat

§Ubuntu – User-friendly, based on Debian

§Debian – Known for its stability, “as solid as a rock, rarely crashes”

§Gentoo – Highly customizable distribution aimed at geek users

Detailed Explanation of Linux Directory Structure

Tree Structure of Directories

Linux adopts a single-root directory tree structure, where all files and directories are organized starting from the root directory /:

/ # Root Directory ├── root/ # Home directory for root user ├── home/ # Home directories for regular users │ └── username/ # Specific user directory ├── etc/ # System configuration files ├── usr/ # User programs and data ├── var/ # Variable data files └── … # Other system directories

Interpreting the Command Line Prompt

[root@kali:~]#

§[] – Decorative symbol, no special meaning

§root – Current logged-in username

§@ – Separator, meaning “at”

§kali – Hostname

§~ – Current directory (where ~ represents the home directory)

§# – Root user prompt (for regular users, it is $)

Basic Directory Operation Commands

# View current directory location          pwd          # Change directory          cd /path/to/directory          cd ~        # Change to home directory          cd ..       # Change to parent directory          # View directory contents          ls          # Basic list          ls -la      # Detailed information (including hidden files)          # Note: Entries starting with 'd' are directories, those starting with '-' are files          # User-related commands          whoami      # Display current user          passwd      # Change password          # File operations          touch filename    # Create an empty file          # Command history          history     # View command history

Command Alias System

Temporary Alias Setting

# Create alias          alias ha='cat /etc/passwd'          # View all aliases          alias          # Delete alias          unalias ha

Permanent Alias Configuration

Edit system configuration file:

vi /etc/bashrc

Add alias configuration:

1.Press i to enter insert mode

2.Add: alias ha=’cat /etc/passwd’

3.Press Esc to exit insert mode

4.Type :wq to save and exit

Make configuration effective:

source /etc/bashrc

Getting Help Information

Built-in Help

# View command help          ls --help

Chinese Manual Page Configuration

Install Chinese manual:

yum -y install man-pages-zh-CN.noarch

Set Chinese manual alias:

echo "alias cman='man -M /usr/share/man/zh_CN'" >> .bashrc          source .bashrc          # Use Chinese manual          cman ls

In-depth Analysis of Redirection Mechanisms

Standard Input and Output Streams

Three standard streams in Linux system:

§0 – Standard Input (stdin)

§1 – Standard Output (stdout)

§2 – Standard Error Output (stderr)

Program Exit Status

echo $?    # View the exit status code of the last program                     # 0 indicates success, non-zero indicates error

Detailed Explanation of Redirection Operators

Basic Redirection

# Overwrite redirection (standard output)          echo "Hello World" > output.txt          # Append redirection (standard output)          echo "New Line" >> output.txt          # Error output redirection          command_not_exist 2> error.log          # Error output append redirection          another_error 2>> error.log

Merging Output Redirection

# Redirect standard output and standard error simultaneously (overwrite)          command &> all_output.log          # Redirect standard output and standard error simultaneously (append)          command &>> all_output.log

Advanced Redirection Techniques

# Redirect error stream to standard output stream          command 2>&1          # Redirect standard output stream to error stream          command 1>&2          # Handle correct and error output separately          echo "success" 1> /root/path/success.out 2> /root/path/error.out          # Correct output to screen, error output to file          echo "test message" 1>&2 2> /root/path/error.out

Here Document

Here Document is a special redirection method that allows multi-line input:

cat > config.file << EOF          This is the first line of content          This is the second line of content          You can input any content here          EOF

Working Principle:

§<< indicates the start of Here Document

§EOF is the end marker (can be any string)

§Input will continue until the end marker is encountered

§All input content will be written to the specified file

Practical Recommendations and Summary

It is recommended to use cloud servers for environment setup: Rent overseas VPS for practice to avoid network restrictions

Learning Path

Basic Commands->Redirection Techniques->Shell Programming->System Management

The directory structure and redirection mechanisms of Linux are fundamental to system operations. The directory structure provides a framework for file organization, while the redirection mechanism offers powerful tools for data flow control. Mastering these core concepts will lay a solid foundation for further learning in Linux system management, shell programming, and server operations.

Through practical operations and repeated practice, and flexibly applying them in daily work. Learning Linux is a gradual process, learning every day, steadily improving skill levels.

Leave a Comment