A symbolic link is a special file that points to another file or directory. Think of it like a shortcut or alias.
- It doesn’t hold the actual data
- It just contains a reference (a path) to the real file or folder
📌 Example
Let’s say you have:
/home/stanley/documents/report.txt
You create a symbolic link:
/home/stanley/report_link.txt → points to → /home/stanley/documents/report.txt
Now, if you open or read from report_link.txt
, it behaves just like the original file.
✅ Key Characteristics
Feature | Description |
---|---|
Type | A special file that links to another |
Can link to | Files or directories |
Works across | File systems (unlike hard links) |
Can break | If the target file is deleted or moved |
In Java? | Supported via java.nio.file.Path + Files.createSymbolicLink() |
🔁 Symbolic Link vs Hard Link
Feature | Symbolic Link | Hard Link |
---|---|---|
Points to | Path (name) | Inode (actual data) |
Cross-partition? | ✅ Yes | ❌ No |
Detectable? | ✅ Yes (isSymbolicLink ) | ❌ Appears like original |
Breaks if target removed? | ✅ Yes | ❌ No (file stays) |
🧪 Symbolic Links in Java (NIO)
import java.nio.file.*;
public class SymlinkDemo {
public static void main(String[] args) throws Exception {
Path target = Paths.get("real_file.txt");
Path link = Paths.get("shortcut.txt");
Files.createSymbolicLink(link, target);
System.out.println("Symlink created: " + link + " → " + target);
}
}
Check if a file is a symlink:
Files.isSymbolicLink(Paths.get("shortcut.txt"));
🧵 TL;DR
Concept | Symbolic Link |
---|---|
What is it? | A file that points to another file/directory |
Also called | symlink, soft link |
Breaks if target deleted? | ✅ Yes |
Cross-filesystem support? | ✅ Yes |
Java support? | ✅ Since Java 7 (java.nio.file ) |