Table of Contents
Description
Learn how to manage files from the command line, create soft and hard links, and use input/output redirection in Linux. Includes clear examples for stdout, stderr, and stdin.
Managing Files, Links, and Redirection in Linux — A Practical Guide
Whether you're preparing for RHCSA, working in DevOps, or just learning Linux, mastering file links and I/O redirection is essential. This guide walks through soft vs hard links and how to use stdin, stdout, and stderr redirection with clear, real-world examples.
File Links in Linux
Every file on a Linux filesystem has an inode, which is like a pointer to where the file lives on the disk.
There are two types of links you can create to refer to a file:
Soft Links (Symbolic)
- A soft link points to the original file path.
- If the original file is deleted or renamed, the link is broken.
- Works across different filesystems.
Example
ln -s /home/user/original.txt shortcut.txt
Ones delete, soft link is broken
Hard Links
A hard link points directly to the file's inode.
Deleting or renaming the original file doesn’t break the link.
Cannot span across different filesystems.
ln /home/user/original.txt clone.txt
Creating Links with ln
Command | Description |
---|---|
ln file1 file2 |
Creates a hard link |
ln -s file1 linkname |
Creates a symbolic (soft) link |
Input and Output Redirection
Linux uses three default data streams
Stream | Description | File Descriptor |
---|---|---|
stdin | Standard Input | 0 |
stdout | Standard Output | 1 |
stderr | Standard Error | 2 |
Standard Output (stdout)
By default, command output shows in the terminal. You can redirect it to a file using >
ls -l > listings
pwd > findpath
This overwrites the file if it exists.
Appending Output
Use >> to append to an existing file without overwriting:
ls -la >> listings
echo "Hello World" >> findpath
Standard Input (stdin)
Use <
to provide input from a file to a command
cat < listings
mail -s "Office memo" [email protected] < memoletter
Standard Error (stderr)
Redirect errors using 2>
ls -l /root 2> errorfile
telnet localhost 2> errorfile
To redirect both output and errors to the same file
command > output.txt 2>&1
Or, split them
command > out.txt 2> err.txt
Conclusion
Understanding how to manage links and redirect input/output gives you more control over how Linux handles files and processes. Whether you're scripting, debugging, or configuring servers, these tools will save you time and help avoid mistakes.