Skip to content

Advanced File Operations

Summary

This chapter covers advanced file manipulation techniques including symbolic and hard links, wildcards and file globbing patterns, and file attributes. You'll learn how to work with multiple files efficiently using pattern matching, understand the difference between symbolic and hard links, and explore file metadata like timestamps and attributes.

Concepts Covered

This chapter covers the following 12 concepts from the learning graph:

  1. Symbolic Links
  2. Hard Links
  3. Ln Command
  4. File Globbing
  5. Wildcards
  6. Asterisk Wildcard
  7. Question Mark Wildcard
  8. Bracket Expressions
  9. Brace Expansion
  10. Tilde Expansion
  11. File Attributes
  12. File Timestamps

Prerequisites

This chapter builds on concepts from:


Level Up: Advanced File Wizardry

You've learned to walk around the Linux file system and do basic file operations. Now it's time to learn some advanced tricks that will make you look like a command-line wizard! In this chapter, we're diving into:

  • Links - Creating shortcuts and aliases to files (way cooler than desktop shortcuts!)
  • Wildcards and Globbing - Matching multiple files with patterns (like search superpowers)
  • File Attributes and Timestamps - The hidden metadata that files carry around

These are the techniques that separate casual Linux users from power users. Master these, and you'll be able to do in seconds what would take others minutes of clicking around. Let's get started!

In the graphical world, you're probably familiar with shortcuts—those little arrow-icons that point to actual files. Linux has something similar but WAY more powerful: links.

There are two types of links in Linux:

  • Symbolic links (symlinks) - Like shortcuts, but better
  • Hard links - A mind-bending concept where two names point to the same actual data

Let's explore both!

A symbolic link (also called a "symlink" or "soft link") is a special file that points to another file or directory. It's like a signpost saying "the real file is over there."

Think of it like a redirect URL on the web. When you visit a shortened URL, it redirects you to the actual page. A symlink works the same way—when you access it, Linux automatically follows the link to the real file.

1
2
3
4
5
# Create a symbolic link
ln -s /path/to/original /path/to/link

# Example: Create a shortcut to your project
ln -s ~/Documents/super-long-project-name ~/project

Now instead of typing cd ~/Documents/super-long-project-name, you can just type cd ~/project. Nice!

Common uses for symbolic links:

  • Creating shortcuts to deeply nested directories
  • Linking configuration files to a central location
  • Pointing to the "current" version of software
  • Sharing files between locations without copying
1
2
3
4
5
6
# See that it's a link with ls -l
ls -l ~/project
# lrwxrwxrwx 1 dan dan 35 Dec  6 16:00 project -> /home/dan/Documents/super-long-project-name

# The 'l' at the start means it's a link
# The -> shows where it points

Symlinks Are Everywhere!

Linux uses symlinks extensively in the system. For example, /bin is often a symlink to /usr/bin on modern systems. Run ls -la / and look for the -> arrows!

If you delete the original file, the symlink becomes "broken" or "dangling." It still exists, but it points to nothing:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Create a file and a link to it
touch original.txt
ln -s original.txt link.txt

# Delete the original
rm original.txt

# The link is now broken!
ls -l link.txt
# lrwxrwxrwx 1 dan dan 12 Dec  6 16:05 link.txt -> original.txt

cat link.txt
# cat: link.txt: No such file or directory

The link is shown in red (on most terminals) to indicate it's broken. It's like having a URL that leads to a 404 page.

Now here's where things get interesting. A hard link is NOT a pointer to a file—it IS the file. Or more precisely, it's another name for the same data on disk.

To understand hard links, you need to know a little secret about how Linux stores files:

  1. The actual file data lives on disk in a structure called an inode
  2. What we call a "filename" is just a label pointing to that inode
  3. A hard link creates another label pointing to the SAME inode

It's like having two different contact names in your phone that both call the same person. Neither one is the "real" name—they're both equally valid.

1
2
3
4
5
6
7
8
9
# Create a hard link
ln original.txt hardlink.txt

# Check they have the same inode number
ls -li original.txt hardlink.txt
# 1234567 -rw-r--r-- 2 dan dan 100 Dec  6 16:10 hardlink.txt
# 1234567 -rw-r--r-- 2 dan dan 100 Dec  6 16:10 original.txt
#    ^                ^
#    Same inode!      Link count is 2

Notice two things:

  • Both files have the same inode number (1234567 in this example)
  • The link count shows "2" - meaning two names point to this data

The cool part: If you delete "original.txt", the data doesn't disappear! It still exists because "hardlink.txt" is pointing to it. The data is only truly deleted when ALL hard links are removed.

Feature Symbolic Link Hard Link
Points to Path/filename Inode (actual data)
Can link to directories Yes No (usually)
Can cross filesystems Yes No
Broken if original deleted Yes No
Shows as link in ls -l Yes (l prefix) No
Understanding Links Visualization

Type: diagram

Bloom Taxonomy: Understand Learning Objective: Visualize the fundamental difference between how symbolic links and hard links reference file data, helping students understand why hard links survive deletion of the "original" file.

Layout: Two side-by-side panels comparing the two link types

Left Panel - Symbolic Link: - Box labeled "symlink.txt" with arrow pointing right - Arrow labeled "points to path" going to - Box labeled "original.txt" - Arrow from original.txt going down to - Cylinder labeled "Inode 12345 (actual data on disk)" - Show what happens when original.txt is deleted (broken arrow, red X)

Right Panel - Hard Link: - Box labeled "original.txt" with arrow pointing down - Box labeled "hardlink.txt" with arrow pointing down - Both arrows converge on the same cylinder "Inode 12345 (actual data on disk)" - Show link count = 2 on the inode - Show what happens when original.txt is deleted (hardlink.txt still works, link count = 1)

Color coding: - Filenames: Blue boxes - Inode/data: Orange cylinder - Symlink arrow: Dashed green line - Hard link arrows: Solid blue lines - Broken link: Red dashed line with X

Interactive features: - Click "Delete original.txt" button to see what happens to each type - Hover over components for explanations

Implementation: p5.js

The ln command creates both types of links:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Create a HARD link (default)
ln original.txt hardlink.txt

# Create a SYMBOLIC link (-s flag)
ln -s original.txt symlink.txt

# Create a symbolic link to a directory
ln -s /var/log ~/logs

# Force overwrite existing link (-f flag)
ln -sf /new/target existing_link

# Verbose mode (-v flag)
ln -sv source.txt link.txt
# 'link.txt' -> 'source.txt'

Ln command options:

Option Meaning
-s Create symbolic (soft) link
-f Force - overwrite existing destination
-v Verbose - show what's being done
-i Interactive - ask before overwriting
-n Don't follow existing symlinks

Hard Link Limitations

Hard links have restrictions:

  • Can't link to directories (prevents infinite loops)
  • Can't cross filesystem boundaries (different partitions)
  • Only work within the same filesystem

When in doubt, use symbolic links (ln -s).

Wildcards and File Globbing: Pattern Power!

Now let's learn something that will 10x your productivity: wildcards! Instead of typing out every filename, you can use patterns to match multiple files at once.

This pattern-matching feature is called file globbing (yes, that's the real name—it sounds like something from a fantasy game, but it's actually incredibly useful).

What is File Globbing?

File globbing is when the shell expands wildcard patterns into a list of matching filenames BEFORE running your command. It happens automatically!

1
2
3
4
5
6
7
# You type:
ls *.txt

# The shell expands it to:
ls notes.txt report.txt todo.txt

# Then runs the command with those files

This expansion happens for any command, not just ls. That's the magic—wildcards work everywhere!

The Asterisk Wildcard: Match Everything

The asterisk wildcard (*) matches zero or more characters. It's the most commonly used wildcard.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Match all .txt files
ls *.txt

# Match all files starting with "report"
ls report*

# Match all files containing "2024"
ls *2024*

# Match all files (except hidden)
ls *

# Copy all Python files to backup
cp *.py backup/

# Delete all temporary files
rm *.tmp

The asterisk is like saying "I don't care what's here, match anything."

Pattern examples:

Pattern Matches Doesn't Match
*.txt notes.txt, a.txt, .txt notes.txt.bak
report* report, report.pdf, reports myreport
*log* log, mylog.txt, log_backup (none in this pattern)
*.tar.gz backup.tar.gz backup.tar

The Question Mark Wildcard: Single Character

The question mark wildcard (?) matches exactly ONE character. Use it when you know the length but not the exact characters.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Match any single character
ls file?.txt
# Matches: file1.txt, file2.txt, fileA.txt
# Doesn't match: file10.txt, file.txt

# Match two unknown characters
ls report??.pdf
# Matches: report01.pdf, reportAB.pdf
# Doesn't match: report1.pdf, report001.pdf

# Combine with asterisk
ls ???.*
# Matches any 3-character filename with any extension

Think of ? as "exactly one mystery character."

Bracket Expressions: Character Classes

Bracket expressions let you specify a SET of characters that can match in a position. Put the options inside square brackets [].

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Match specific characters
ls file[123].txt
# Matches: file1.txt, file2.txt, file3.txt
# Doesn't match: file4.txt, file12.txt

# Match a range of characters
ls file[1-9].txt
# Matches: file1.txt through file9.txt

ls file[a-z].txt
# Matches: filea.txt through filez.txt

# Match uppercase or lowercase
ls [Rr]eport.txt
# Matches: Report.txt, report.txt

# Combine ranges
ls file[0-9a-f].txt
# Matches: file0.txt through file9.txt, filea.txt through filef.txt

Special bracket patterns:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Negate with ^ or ! (match anything EXCEPT these)
ls file[^0-9].txt
# Matches: filea.txt, fileX.txt
# Doesn't match: file1.txt, file9.txt

# Character classes
ls [[:digit:]]*.txt    # Files starting with a digit
ls [[:alpha:]]*.txt    # Files starting with a letter
ls [[:upper:]]*.txt    # Files starting with uppercase
ls [[:lower:]]*.txt    # Files starting with lowercase
Class Matches
[[:digit:]] 0-9
[[:alpha:]] a-z, A-Z
[[:alnum:]] a-z, A-Z, 0-9
[[:upper:]] A-Z
[[:lower:]] a-z
[[:space:]] Whitespace characters

Diagram: Wildcard Pattern Matching

Interactive Wildcard Matcher

Type: microsim

Bloom Taxonomy: Apply, Analyze Learning Objective: Allow students to experiment with wildcard patterns and see which files match in real-time, building intuition for pattern construction.

Canvas layout (responsive, ~750px max width): - Top section (100px): Pattern input field - Middle section (300px): File list showing matches/non-matches - Bottom section (100px): Explanation of why each file matches or doesn't

Visual elements: - Text input for entering wildcard pattern - List of sample filenames - Matched files highlighted in green - Non-matched files in gray - Real-time pattern explanation

Sample file list: - report.txt - report1.txt - report2.txt - report10.txt - Report.txt - myreport.txt - data.csv - data_2024.csv - backup.tar.gz - notes.md - .hidden

Interactive controls: - Text input: Enter pattern (e.g., "report.txt") - Quick pattern buttons: ".txt", "report?", "[A-Z]", "[0-9]*" - Reset button

Behavior: - As user types pattern, matching files highlight immediately - Show count: "5 of 11 files match" - Below each file, show why it matched or didn't - Handle special cases (hidden files, no matches)

Example interactions: - Pattern ".txt" → highlights report.txt, report1.txt, report2.txt, report10.txt, Report.txt, myreport.txt - Pattern "report?.txt" → highlights report1.txt, report2.txt only - Pattern "[Rr]eport" → highlights report.txt, report1.txt, report2.txt, report10.txt, Report.txt

Color scheme: - Matched: Green background - Not matched: Gray text - Pattern input: Blue border

Implementation: p5.js

Brace Expansion: Generate Sequences

Brace expansion is slightly different from globbing—it generates a list of strings, whether or not matching files exist. It's done by the shell BEFORE checking for files.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Create multiple files at once
touch file{1,2,3}.txt
# Creates: file1.txt, file2.txt, file3.txt

# Create numbered sequences
touch chapter{01..10}.md
# Creates: chapter01.md through chapter10.md

# Create letter sequences
touch backup_{a..e}.tar
# Creates: backup_a.tar through backup_e.tar

# Combine multiple braces
mkdir -p project/{src,tests,docs}/{main,utils}
# Creates a whole directory tree!

# Use with other text
echo {un,re,dis}happy
# Output: unhappy rehappy dishappy

Brace expansion vs wildcards:

Feature Brace Expansion Wildcards (Globbing)
Expands before File check File check
Creates new names Yes No
Requires existing files No Yes
Syntax {a,b,c} or {1..10} *, ?, []
1
2
3
4
5
6
7
# Brace expansion: ALWAYS expands
echo {1,2,3}.txt
# Output: 1.txt 2.txt 3.txt (even if files don't exist)

# Glob: Only matches existing files
echo *.txt
# Output: (list of actual .txt files, or "*.txt" if none exist)

Brace Expansion Power Move

Use brace expansion to create project structures instantly:

1
mkdir -p myapp/{src/{components,utils,styles},tests,docs,config}
This creates a complete directory tree in one command!

Tilde Expansion: Home Sweet Home

Tilde expansion is so common you might not even think of it as expansion. The tilde (~) expands to your home directory.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Go to your home directory
cd ~

# Go to your Documents
cd ~/Documents

# Copy to home directory
cp file.txt ~

# Reference another user's home (if you have permission)
ls ~alice      # /home/alice
ls ~root       # /root

# In scripts, $HOME is more reliable
echo $HOME     # /home/dan
echo ~         # /home/dan

The tilde is expanded by the shell before the command runs, turning ~ into /home/username.

File Attributes and Timestamps: The Hidden Metadata

Every file carries around invisible information about itself—metadata that tells you (and the system) important details. Let's explore this hidden world!

File Timestamps: When Things Happened

Every file in Linux has THREE file timestamps:

  1. atime (Access time): Last time the file was READ
  2. mtime (Modification time): Last time the file CONTENT was changed
  3. ctime (Change time): Last time the file METADATA changed (permissions, owner, etc.)
1
2
3
4
5
6
7
# See timestamps with stat command
stat myfile.txt

# Output shows:
# Access: 2025-12-06 14:30:00.000000000 -0600
# Modify: 2025-12-05 10:15:00.000000000 -0600
# Change: 2025-12-05 10:15:00.000000000 -0600

When each timestamp updates:

Timestamp Updated when...
atime File is read (cat, less, executed)
mtime File contents are modified
ctime Anything changes (content, permissions, name, links)
1
2
3
4
5
6
7
8
# Reading updates atime
cat myfile.txt

# Editing updates mtime (and ctime)
echo "new content" >> myfile.txt

# Changing permissions updates ctime only
chmod 755 myfile.txt

atime and Performance

Updating atime on every read can slow down the system. Many modern Linux systems mount filesystems with noatime or relatime options to reduce disk writes. Don't be surprised if atime doesn't update as expected!

Viewing and Modifying Timestamps

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# See detailed timestamps
stat filename

# List files sorted by modification time (newest first)
ls -lt

# List files sorted by access time
ls -ltu

# See the full timestamp
ls -l --full-time filename

# Modify timestamps with touch
touch filename                    # Update all timestamps to now
touch -a filename                 # Update only atime
touch -m filename                 # Update only mtime
touch -d "2024-01-15" filename    # Set specific date
touch -r reference.txt target.txt # Copy timestamps from another file

File Attributes: Extended Properties

Beyond the basic permissions, files can have special file attributes that control how they behave. These are managed with lsattr (list attributes) and chattr (change attributes).

1
2
3
4
5
6
7
8
# List file attributes
lsattr filename
# ----i--------e-- filename

# Common attributes:
# i - Immutable (cannot be modified, deleted, or renamed)
# a - Append only (can only add to file, not modify)
# e - Extent format (how file is stored on disk)

Setting immutable attribute (requires root):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Make a file immutable (cannot be changed or deleted)
sudo chattr +i important.txt

# Try to delete it
rm important.txt
# rm: cannot remove 'important.txt': Operation not permitted

# Even root can't delete it without removing the attribute!
sudo rm important.txt
# rm: cannot remove 'important.txt': Operation not permitted

# Remove immutable attribute
sudo chattr -i important.txt

Useful attributes:

Attribute Meaning
i Immutable - can't be modified, deleted, renamed, or linked
a Append only - can only add data, not modify existing
A No atime updates - don't update access time
c Compressed - automatically compress file
s Secure delete - overwrite data when deleted

Attribute Limitations

File attributes don't work on all filesystems. They work on ext4 (the most common Linux filesystem) but may not work on FAT32, NTFS, or network filesystems.

Diagram: File Metadata Overview

File Metadata and Timestamps

Type: diagram

Bloom Taxonomy: Remember, Understand Learning Objective: Show students all the metadata associated with a file, including timestamps, permissions, ownership, and attributes.

Layout: Central file icon with metadata branching out

Visual elements: - Central icon representing a file "document.txt" - Branching sections for each metadata category - Clear labels and example values

Metadata sections: 1. Timestamps (clock icon) - Access time: 2025-12-06 14:30:00 - Modify time: 2025-12-05 10:15:00 - Change time: 2025-12-05 10:15:00

  1. Ownership (person icon)
  2. User: dan (UID 1000)
  3. Group: developers (GID 100)

  4. Permissions (lock icon)

  5. Mode: -rw-r--r-- (644)
  6. Owner: read, write
  7. Group: read
  8. Others: read

  9. Size/Storage (disk icon)

  10. Size: 4,096 bytes
  11. Blocks: 8
  12. Inode: 1234567

  13. Attributes (shield icon)

  14. Immutable: No
  15. Append-only: No

Color coding: - Timestamps: Blue - Ownership: Green - Permissions: Orange - Size/Storage: Purple - Attributes: Red

Interactive features: - Hover over each section for detailed explanation - Click to see the command that retrieves this info

Implementation: p5.js or HTML/CSS

Putting It All Together: Advanced Operations

Let's combine everything we've learned into some real-world scenarios!

Scenario: Organize Music Library

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Create symlinks for favorite albums
mkdir ~/favorites
ln -s ~/Music/Artist1/BestAlbum ~/favorites/artist1
ln -s ~/Music/Artist2/TopHits ~/favorites/artist2

# Find all MP3 files and count them
ls ~/Music/**/*.mp3 | wc -l

# Copy all 2024 files to backup
cp ~/Music/*2024* ~/backup/

# Find files modified in the last 7 days
find ~/Music -mtime -7 -name "*.mp3"

Scenario: Development Project Setup

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Create project structure with brace expansion
mkdir -p project/{src/{components,utils,hooks},tests,docs,config}

# Create multiple config files
touch project/config/{dev,prod,test}.json

# Link global config to project
ln -s ~/.config/prettier ~/.prettierrc

# Find all JavaScript files
ls project/**/*.js

# Find all test files (ending in .test.js)
ls project/**/*.test.js

Scenario: Log File Management

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# View all log files from December
ls /var/log/*dec*

# Find large log files (using stat and sort)
ls -lS /var/log/*.log | head -5

# Create dated backup with brace expansion
cp /var/log/app.log /var/log/app.log.{$(date +%Y%m%d)}

# Make config file immutable
sudo chattr +i /etc/important.conf

Wildcard Cheat Sheet

Pattern Matches Example
* Zero or more characters *.txt → all .txt files
? Exactly one character file?.txt → file1.txt, fileA.txt
[abc] One of the listed characters file[123].txt → file1.txt, file2.txt, file3.txt
[a-z] One character in range file[a-z].txt → filea.txt through filez.txt
[^abc] NOT one of these characters file[^0-9].txt → not file1.txt
{a,b,c} Generate each option {un,re}do → undo, redo
{1..5} Generate sequence file{1..5} → file1 through file5
~ Home directory ~/docs → /home/user/docs

Key Takeaways

You've leveled up your Linux skills with these advanced file operations!

  • Symbolic links (ln -s) create shortcuts that point to files/directories
  • Hard links (ln) create additional names for the same file data
  • Wildcards (*, ?, []) match patterns in filenames
  • Brace expansion ({}) generates lists of strings
  • Tilde expansion (~) represents your home directory
  • File timestamps track access, modification, and change times
  • File attributes provide additional control (like immutability)

You're Becoming a Power User!

These tools separate beginners from experts. You can now manipulate files with patterns, create efficient shortcuts, and understand the hidden metadata that files carry. Practice using wildcards in your daily work—they become second nature surprisingly quickly!

What's Next?

Now that you understand advanced file operations, it's time to learn about permissions and ownership—who can read, write, and execute your files! This is crucial for both security and collaboration.


Quick Quiz: Advanced File Operations
  1. What's the difference between a symbolic link and a hard link?
  2. What command creates a symbolic link?
  3. What does the pattern *.txt match?
  4. What does file[0-9].txt match?
  5. How does brace expansion {a,b,c} differ from bracket expression [abc]?
  6. What are the three file timestamps in Linux?
Quiz Answers
  1. A symbolic link points to a path/filename and breaks if the target is deleted. A hard link points to the inode (data) and survives if the "original" is deleted.
  2. ln -s target linkname creates a symbolic link
  3. All files ending in .txt
  4. Files like file0.txt, file1.txt through file9.txt (exactly one digit)
  5. Brace expansion generates all combinations whether files exist or not. Bracket expressions only match existing files with that character in that position.
  6. atime (access), mtime (modification), ctime (change/metadata)

References

  1. Understanding Linux Symbolic Links - Red Hat's comprehensive guide to symbolic and hard links with visual examples.
  2. Hard Links vs Soft Links Explained - Linuxize tutorial on creating and managing different types of links.
  3. Inodes and Links in Linux - Grymoire's deep dive into how inodes work and why hard links behave differently.
  4. Bash Wildcards and Globbing - The Linux Documentation Project's guide to pattern matching.
  5. File Globbing Patterns Explained - Linux Journal article on advanced globbing techniques.
  6. Wildcards in Linux Commands - TecMint's practical guide to using wildcards effectively.
  7. Brace Expansion in Bash - GNU Bash manual section on brace expansion syntax and usage.
  8. POSIX Character Classes - Regular-Expressions.info guide to character classes in bracket expressions.
  9. Understanding File Timestamps - Linuxize explanation of atime, mtime, and ctime with modification examples.
  10. File Attributes with lsattr and chattr - TheGeekStuff tutorial on extended file attributes.
  11. Immutable Files in Linux - nixCraft guide to using the immutable attribute for protection.
  12. Bracket Expressions Tutorial - GNU grep manual on character classes and bracket syntax.
  13. Tilde Expansion Explained - Bash manual section on tilde expansion for home directories.
  14. Working with Find Command - GeeksforGeeks comprehensive guide to finding files by various attributes.
  15. Stat Command Deep Dive - Linux man page for stat with detailed timestamp explanations.
  16. Symbolic Link Best Practices - Opensource.com article on when and how to use symbolic links effectively.
  17. Glob Patterns Cheat Sheet - Linux Training Academy's quick reference for globbing patterns.
  18. Advanced Ln Command Usage - Computer Hope's reference for the ln command with all options.
  19. Filesystem Metadata Overview - Kernel.org wiki explaining how filesystems store metadata including timestamps.
  20. Pattern Matching in Shell Scripts - BashGuide wiki on glob patterns and their uses in scripting.