Skip to main content

Lab 3.3 - File Search and Links

Objectives​

By the end of this lab, you will be able to:

  • Search for files with find and locate
  • Create symbolic links
  • Create hard links
  • Understand the differences between link types
  • Use links in practical scenarios

Prerequisites​

  • Ubuntu 24.04 installed
  • Terminal open
  • Normal user access

Exercise 1: Search with find​

# Create test files
mkdir -p ~/test-search/{docs,scripts,data}
touch ~/test-search/docs/{file1.txt,file2.txt}
touch ~/test-search/scripts/{script1.sh,script2.sh}
touch ~/test-search/data/{data1.csv,data2.csv}

# Search by name
find ~/test-search -name "file1.txt"

# Search with wildcards
find ~/test-search -name "*.txt"

# Search by type
find ~/test-search -type f # Files only
find ~/test-search -type d # Directories only
# By size
find ~/test-search -size +1M # More than 1MB
find ~/test-search -size -1k # Less than 1KB

# By modification date
find ~/test-search -mtime -1 # Modified in the last 24h
find ~/test-search -mtime +7 # Modified more than 7 days ago

# By permissions
find ~/test-search -perm 755

# Combinations
find ~/test-search -name "*.txt" -mtime -1

Step 3: Execute Commands​

# Find and delete (caution!)
find ~/test-search -name "*.tmp" -delete

# Find and display
find ~/test-search -name "*.txt" -exec ls -lh {} \;

# Find and count
find ~/test-search -type f | wc -l

Exercise 2: locate​

Step 1: Installation and Configuration​

# Install locate
sudo apt update
sudo apt install mlocate

# Update the database
sudo updatedb

# Search
locate file1.txt

# Case-insensitive search
locate -i file

Step 2: Limit Results​

# Limit the number of results
locate file | head -10

# Count
locate file | wc -l

# Create a file
echo "Original content" > ~/original.txt

# Create a symbolic link
ln -s ~/original.txt ~/symbolic-link.txt

# View the link
ls -l ~/symbolic-link.txt
# Note the arrow: symbolic-link.txt -> /home/user/original.txt

# Read via the link
cat ~/symbolic-link.txt
# Create a link to a directory
ln -s ~/test-search ~/directory-link

# Use the link
ls ~/directory-link
cd ~/directory-link
cd ~/test-search

# Absolute link
ln -s /home/$USER/original.txt absolute-link.txt

# Relative link
ln -s ../original.txt relative-link.txt

# See the difference
ls -l link-*.txt

# Create a file
echo "Content" > ~/original-file.txt

# Create a hard link
ln ~/original-file.txt ~/hard-link.txt

# View both files
ls -li ~/original-file.txt ~/hard-link.txt
# Note: same inode (first number)
# Modify via the hard link
echo "New content" >> ~/hard-link.txt

# View the original file
cat ~/original-file.txt # Content modified!

# Count the links
ls -l ~/original-file.txt
# The number after permissions = number of hard links
# Delete the "original" file
rm ~/original-file.txt

# The hard link still exists!
cat ~/hard-link.txt # Content still there

# Check the inodes
ls -li ~/hard-link.txt

Step 1: Deletion Test​

# Create file, symbolic link, and hard link
echo "Test" > test.txt
ln -s test.txt sym-link.txt
ln test.txt hard-link.txt

# Delete the original file
rm test.txt

# Test the symbolic link
cat sym-link.txt # ❌ Error: file not found

# Test the hard link
cat hard-link.txt # ✅ Still works!
# Hard links do not work for:
# 1. Directories
# ln dir1 dir2 # ❌ Error

# 2. Different file systems
# ln /mnt/external/file.txt ~/link.txt # ❌ Error if FS different

# 3. Symbolic links can cross FS
ln -s /mnt/external/file.txt ~/sym-link.txt # ✅ Works

Exercise 6: Practical Use Cases​

Step 1: Scripts in PATH​

# Create a script
cat > ~/my-script.sh << 'EOF'
#!/bin/bash
echo "Script executed!"
EOF

chmod +x ~/my-script.sh

# Create a link in a PATH directory
sudo ln -s ~/my-script.sh /usr/local/bin/my-script

# Use from anywhere
my-script

Step 2: Software Versions​

# Simulate versions
mkdir -p ~/app/{v1.0,v2.0}
echo "Version 1.0" > ~/app/v1.0/app.txt
echo "Version 2.0" > ~/app/v2.0/app.txt

# Link to current version
ln -s ~/app/v2.0 ~/app/current

# Use
cat ~/app/current/app.txt

# Easily switch versions
rm ~/app/current
ln -s ~/app/v1.0 ~/app/current

Step 3: Configuration​

# Config file in home
echo "config=value" > ~/.myapp-config

# Link from /etc
sudo ln -s ~/.myapp-config /etc/myapp-config

# Use
cat /etc/myapp-config

Verification​

Checklist​

Check that you can:

  • Search for files with find
  • Use locate for quick search
  • Create symbolic links (ln -s)
  • Create hard links (ln)
  • Understand the differences between the two types
  • Use links in practical scenarios

Reflection Questions​

  1. When to use a symbolic link vs a hard link?

    • Symbolic: directories, files on different FS, versions
    • Hard: same FS, safety backup
  2. Why do hard links share the same inode?

    • They point to the same data on disk
    • The file is only deleted when all links are removed
  3. What is the difference between find and locate?

    • find: real-time search, more flexible
    • locate: pre-indexed database, faster but can be outdated

Next Steps​

  • Lab 3.4: Complete Practice
  • Chapter 3.6: File Search
  • Chapter 3.7: Symbolic and Hard Links

Lab created on: December 2024 - Tested on Ubuntu 24.04