Linux File Management and Search
Guide
a. Hard and Symbolic Links
Creating Symbolic Links (Symlinks)
Example 1: Creating a symlink to a frequently accessed configuration file
ln -s /etc/nginx/nginx.conf ~/nginx_config_symlink
Explanation:
- This creates a symbolic link named `nginx_config_symlink` in your home directory pointing
to the actual NGINX configuration file
- If you delete the symlink (`rm ~/nginx_config_symlink`), the original file remains
- If you delete the original file, the symlink becomes "dangling" (points to nothing)
- Useful for quick access to deeply nested files without remembering long paths
Example 2: Creating a symlink for a shared development tool
ln -s /opt/new_software/bin/executable /usr/local/bin/dev_tool
Explanation:
- Makes a new version of a tool available system-wide without moving the original
- Allows multiple versions to exist while maintaining a consistent interface
Creating Hard Links
Example 1: Creating a hard link for important document backup
ln important_report.txt report_backup_link
Explanation:
- Creates another name (`report_backup_link`) pointing to the same inode as
`important_report.txt`
- Both files are equally valid - deleting one doesn't affect the other
- Changes to one file are reflected in both (they're the same file with different names)
- Only works within the same filesystem
Example 2: Hard link for system log monitoring
ln /var/log/syslog ~/current_system_log
Explanation:
- Creates a convenient access point to monitor system logs from your home directory
- Both files share the same inode number (check with `ls -i`)
- Maintains the file even if one name is deleted
b. File Search and Retrieval
Search Files by Name/Type
Example 1: Finding all Python files modified in last 7 days
find ~/projects -name "*.py" -type f -mtime -7
Explanation:
- Searches your projects directory for files (`-type f`) ending with `.py`
- `-mtime -7` filters for files modified in the last 7 days
- Useful for finding recent work or tracking changes
Example 2: Finding large log files (>100MB) for cleanup
find /var/log -size +100M -exec ls -lh {} \;
Explanation:
- `-size +100M` finds files larger than 100MB
- `-exec` runs `ls -lh` on each found file to show human-readable sizes
- Useful for system maintenance and disk cleanup
Locate Files
Example 1: Finding all system manual pages about networking
locate man | grep network | head -n 10
Explanation:
- `locate` uses a pre-built database for faster searching
- Pipes through `grep` to filter for network-related man pages
- `head` shows just the first 10 results
- Much faster than `find` for system-wide searches
Example 2: Locating configuration files for a specific application
sudo updatedb && locate apache2.conf
Explanation:
- `sudo updatedb` ensures the locate database is current
- Then searches for Apache configuration files
- Essential when you know a file exists but not where
c. File and Directory Management
Create Files and Directories
Example 1: Setting up a project structure
mkdir -p ~/new_project/{src,doc,tests,data/{raw,processed}}
touch ~/new_project/src/{main.py,utils.py}
Explanation:
- `mkdir -p` creates parent directories as needed
- Brace expansion creates multiple directories at once
- `touch` creates empty files for source code
- Creates an organized project structure in one command
Example 2: Creating timestamped log files
mkdir -p ~/logs/$(date +%Y-%m)
touch ~/logs/$(date +%Y-%m)/system_$(date +%Y%m%d).log
Explanation:
- Creates a directory named with current year-month
- Creates a log file with current date in the name
- Useful for automated logging systems
List Directory Contents
Example 1: Detailed listing sorted by modification time
ls -lht --color=auto
Explanation:
- `-l` long listing format
- `-h` human-readable file sizes
- `-t` sort by modification time (newest first)
- `--color=auto` adds color coding for file types
Example 2: Showing inode numbers and hidden files
ls -lia
Explanation:
- `-i` shows inode numbers
- `-a` shows all files including hidden ones (starting with .)
- `-l` provides detailed information
- Useful for understanding hard links and system files
Move, Copy, and Rename Files
Example 1: Safely moving files with confirmation
mv -iv old_project/* new_project/
Explanation:
- `-i` interactive (prompts before overwrite)
- `-v` verbose (shows what's happening)
- Moves all files from old to new directory
- Safer alternative to blind moving
Example 2: Copying with preservation of attributes
cp -a ~/important_data /mnt/backup_drive/
Explanation:
- `-a` archive mode (preserves permissions, timestamps, etc.)
- Essential for proper backups
- Better than simple `cp` for system administration
Delete Files and Directories
Example 1: Safe recursive deletion with confirmation
rm -ir old_directory/
Explanation:
- `-i` interactive confirmation
- `-r` recursive (for directories)
- Safer than `rm -rf` which is dangerous
Example 2: Cleaning up temporary files older than 30 days
find /tmp -type f -mtime +30 -delete
Explanation:
- Finds files (`-type f`) in /tmp older than 30 days (`-mtime +30`)
- `-delete` removes them
- More controlled than blanket `rm` commands
- Useful for automated cleanup scripts