When working with Linux filesystems, you'll frequently encounter scenarios where you need to reference files across different partitions. The error message you're seeing:
ln: creating hard link /var/www/myweb/linkedfile.txt' => sourcefile.txt': Invalid cross-device link
occurs because hard links have an important limitation - they can't span different filesystems or partitions. This is due to how hard links work at the filesystem level.
What you actually want is a symbolic link (symlink), which creates a special file that points to another file by pathname. Unlike hard links, symlinks can cross partition boundaries. Here's the correct syntax:
ln -s /mnt/storage/sourcefile.txt /var/www/myweb/linkedfile.txt
Key differences from your original command:
- The
-s
flag creates a symbolic link instead of a hard link - The command follows the pattern:
ln -s target_path link_location
After creating the symlink, verify it with:
ls -l /var/www/myweb/linkedfile.txt
You should see output like:
lrwxrwxrwx 1 user user 25 Feb 15 10:00 linkedfile.txt -> /mnt/storage/sourcefile.txt
Here are some practical scenarios with symlinks across partitions:
# Linking entire directories ln -s /mnt/storage/web_assets/ /var/www/myweb/assets # Relative path symlinks (useful when partitions might mount differently) cd /var/www/myweb ln -s ../../mnt/storage/sourcefile.txt linkedfile.txt # Creating multiple symlinks at once ln -s /mnt/storage/{file1.txt,file2.jpg} /var/www/myweb/
Remember that symlinks:
- Inherit permissions from the target file, not the link location
- Require execute permissions on directories for traversal
- May need proper SELinux contexts if enabled
If your symlinks aren't working as expected:
# Check if the target exists ls -l /mnt/storage/sourcefile.txt # Verify filesystem mounting df -h /mnt/storage/ /var/www/myweb/ # Check for broken symlinks find /var/www/myweb/ -xtype l
When working with Linux filesystems, you might encounter this common error when trying to create hard links between different partitions:
ln: creating hard link /var/www/myweb/linkedfile.txt' => sourcefile.txt': Invalid cross-device link
This occurs because hard links (created with ln
without options) must reside on the same filesystem partition. They share the same inode number and physical data blocks.
For cross-partition linking, symbolic links (symlinks) are the solution. They're essentially special files that act as pointers to other files:
ln -s /mnt/storage/sourcefile.txt /var/www/myweb/linkedfile.txt
This creates a symbolic link at the target location that references the source file across partitions.
Common use cases for cross-partition symlinks include:
- Linking web application assets from storage partitions
- Creating accessible paths for frequently-used files
- Maintaining data integrity during migrations
To verify your symlink creation:
ls -l /var/www/myweb/linkedfile.txt # Output should show the link and its target
For directory symlinks:
ln -s /mnt/storage/data/ /var/www/myweb/assets
Remember that:
- The symlink inherits permissions from its target
- You need write permissions in the target directory
- Relative paths can be used if maintaining directory structure
# Using relative path example: cd /var/www/myweb ln -s ../../mnt/storage/sourcefile.txt linkedfile.txt
If your symlinks aren't working:
- Check that the target file exists
- Verify the path is accessible
- Test with absolute paths first
- Ensure no filesystem mounting issues exist