[Unix]

$ ls -l
total 12875072
-rw-r--r-- 2 euler euler 224444099 May 4 2025 DESTINOS_01.mp4

here what does the '2' between permission and user mean?

 

In the output of the ls -l command, that number represents the number of hard links pointing to that specific file or directory.

What is a Hard Link?

Think of a file as a block of data sitting on your hard drive. A filename is just a "label" or a pointer that tells the system where that data is.

  • 1: Most files have a count of 1. This means there is exactly one filename pointing to that data.

  • 2 or more: This means multiple filenames (in the same or different directories) are pointing to the exact same data on the disk. If you edit one, the changes appear in the other because they are the same physical file.

Why is yours showing '2'?

For a regular file like DESTINOS_01.mp4, a count of 2 typically means:

  1. You (or a program) created a manual link using the ln command (e.g., ln DESTINOS_01.mp4 backup_link.mp4).

  2. The file exists in two different places on your filesystem, but both names share the same inode (the unique identifier for the data).


A Note on Directories

If you run ls -l on a directory, you will notice the link count is usually at least 2. This is because:

  • The directory name itself is one link.

  • The . (dot) entry inside the directory (representing "self") is the second link.

  • Each subdirectory inside it adds another link to the count (because of the .. entry inside the child pointing back to the parent).

Would you like to know how to find the other file that is linked to this one?

 

To find the other file that is sharing the same data as your DESTINOS_01.mp4, you can use the inode number.

The inode is the unique ID for the data on your disk. Since your link count is 2, there is another filename somewhere on the same partition that has the exact same inode number.

1. Find the Inode Number

First, get the inode number of your file using the -i flag:

Bash
 
ls -i DESTINOS_01.mp4

The output will look something like this:

1234567 DESTINOS_01.mp4 (where 1234567 is the inode).

2. Search for the "Twin" File

Once you have that number, you can use the find command to search your system (or a specific directory) for any other file with that same ID:

Bash
 
find /path/to/search -inum 1234567
  • Tip: If you don't want to copy-paste the number, you can do it all in one step with the -samefile flag:

    Bash
     
    find . -samefile DESTINOS_01.mp4
    

Why would you have two?

Common reasons for a link count of 2 on a video file include:

  • Manual Backups: You created a link to save space (since a hard link doesn't take up extra disk space).

  • Media Servers: Some software creates hard links to organize files into different categories (e.g., "Movies" and "Recently Added") without duplicating the large video file.

  • Version Control: Some backup tools (like rsync with --link-dest) use hard links to point to unchanged files from previous backups.


Would you like me to explain how to safely remove one of these links without deleting the actual video data?

Linux Inodes and Hard Links Explained

This video provides a deep dive into how inodes work and how multiple filenames can point to the same physical data.

 

 

Removing a hard link is actually very straightforward, but it’s helpful to understand what happens "under the hood" to ensure your data stays safe.

The Short Answer: Just use rm

You can safely remove one of the filenames using the standard rm command:

Bash
 
rm DESTINOS_01.mp4

In Linux, the rm command actually stands for unlink. When you run it, you aren't necessarily deleting the data; you are simply removing one "label" (link) from that data.

How Linux Decides When to Actually Delete

The system uses a Link Counter (the number 2 you saw earlier) to protect your files. Here is the logic the filesystem follows:

  1. Check the count: When you run rm, the system decrements the link count by 1.

  2. Evaluate:

    • If the count is greater than 0, the data stays on the disk because at least one other filename still points to it.

    • If the count drops to 0, the system marks the disk space as "free" and the data is officially deleted.

  3. Result: Since your count is currently 2, removing one filename leaves the count at 1. Your video data remains perfectly intact and accessible via the second filename.


Safety Checklist

Before you hit enter, keep these two rules in mind:

  • Rule 1: Don't delete both. If you delete both files, the link count hits zero and the video is gone.

  • Rule 2: Avoid rm -rf on directories. While rm is safe for files, be extra careful if you are ever manually messing with directory links (though Linux usually prevents you from hard-linking directories to avoid infinite loops).

How to Verify

If you want to be 100% sure before you delete, you can check the link count of the other file (the "twin" we found earlier). If that file also shows a 2, you are safe to delete DESTINOS_01.mp4. After the deletion, the remaining file's link count will automatically drop to 1.

Would you like me to show you how to create a new hard link so you can practice this without risking your actual video file?

 

 

 

 

 

Neither is strictly "better"—they are different tools for different jobs. To understand why we need both, it helps to think of them in terms of stability vs. flexibility.

The Core Difference

A Hard Link is like having two different front doors to the exact same house. A Soft Link (Symlink) is like a sticky note on your front door that says, "I moved, go to this other address."


Comparison at a Glance

Feature Hard Link Soft Link (Symlink)
What it is A direct pointer to the data (Inode). A file containing a path to another file.
Stability High. If you move/delete the "original," the data stays safe. Low. If you move/delete the "original," the link breaks ("Dangling Link").
Across Disks? No. Only works on the same partition. Yes. Can point to files on other drives or even network shares.
For Folders? No. (Safety restriction to avoid loops). Yes. You can link entire directories.
Performance Slightly faster (direct access). Slightly slower (must resolve the path).

Why do we need both?

1. Use Hard Links when you want "Invisible" Redundancy

Since a hard link is the file, it’s perfect for space-saving backups.

 

 

  • The "Time Machine" Trick: Modern backup tools use hard links so that if a 1GB file doesn't change between Monday and Tuesday, the Tuesday backup just creates a hard link to the Monday file. It looks like you have two full backups, but they only take up 1GB of space. Even if you delete Monday's folder, Tuesday's file remains perfectly fine.

     
     

     

2. Use Soft Links when you want a "Shortcut"

Soft links are far more common for daily use because they are flexible.

  • The "System Version" Trick: Imagine you have python3.10 and python3.12 installed. You can create a soft link named python that points to python3.12. When you want to upgrade, you just point the soft link to the new version. Programs just look for python and don't care which version it's actually pointing to.

  • Organizing Drives: If your Downloads folder is full, you can move the actual folder to a secondary 10TB drive and put a soft link in your Home folder. Your computer will act as if the folder never moved.


Which one should you use?

  • Use a Soft Link if you need to link a directory, link across different drives, or if you want to see exactly where the link goes when you run ls -l.

  • Use a Hard Link if you want to ensure the file never breaks even if the original is moved, or if you are trying to save space on backups on the same disk.

      

Would you like to see the exact commands to create both types so you can see the difference in your terminal?

 

Here is how you can create both and see exactly how they behave differently in your terminal.

1. Creating a Hard Link

We use the ln command (without flags).

Bash
 
# Create a dummy file
echo "Hello World" > original.txt

# Create a hard link
ln original.txt hardlink.txt

What to look for:

Run ls -li. You will notice that both files have the exact same inode number (the first column) and both now show a link count of 2. If you delete original.txt, hardlink.txt still works perfectly and still contains "Hello World."


2. Creating a Soft Link (Symlink)

We use the ln -s command (the -s stands for symbolic).

Bash
 
# Create a soft link
ln -s original.txt softlink.txt

What to look for:

Run ls -l. You will see something very different:

  • The permissions start with an l (for link): lrwxrwxrwx.

  • The output shows an arrow: softlink.txt -> original.txt.

  • The link count for original.txt does not increase; it stays at 1.


3. The "Breaking" Test

This is the best way to understand the difference. If we remove the original file, see what happens to its "children":

Bash
 
rm original.txt
  • Check the Hard Link: cat hardlink.txt → Still works! The data is safe.

  • Check the Soft Link: cat softlink.txtError! "No such file or directory." The link is now "broken" or "dangling" because the path it was pointing to is gone.

Summary of Commands

Action Command
Create Hard Link ln [target] [link_name]
Create Soft Link ln -s [target] [link_name]
Check Inodes ls -i

Would you like to know how to fix a broken soft link if you accidentally move the original file?

 

Fixing a broken soft link is easy because you don't actually "repair" the file—you just point it to a new location.

If you move your original file (e.g., DESTINOS_01.mp4) to a different folder, the soft link will turn red in most terminals, indicating it’s "dangling" (pointing to a path that no longer exists).

1. The "Force" Method (The Quickest Way)

The most common way to fix a link is to overwrite it using the -f (force) flag. This tells Linux: "I know this link exists; overwrite its destination with this new path."

Bash
 
ln -sf /new/path/to/DESTINOS_01.mp4 softlink.txt

2. Using readlink to see where it thinks it is

If you aren't sure why a link is broken, you can ask the link where it's trying to point:

Bash
 
readlink softlink.txt

This will output the literal text string stored inside the link. If that path is wrong, you know you need to update it.

3. Relative vs. Absolute Paths (The Pro Tip)

This is where most people break their links.

  • Absolute Path: ln -s /home/euler/videos/movie.mp4 link

    • Pro: You can move the link anywhere on the system and it will still work.

    • Con: If you move the movie, the link breaks.

  • Relative Path: ln -s ../movie.mp4 link

    • Pro: Great for projects or folders you might move to another drive as a whole unit.

    • Con: If you move just the link to a different subfolder, it breaks because ../ now points somewhere else.


How to find all broken links

If you’ve been moving files around and want to clean up your "dangling" links, you can use the find command:

Bash
 
find . -xtype l

This looks for files of type "link" (l) that are "broken" (x).

Since you're using ZFS, are you managing these files for a media server like Plex or Jellyfin? I can show you how they handle these links differently.

 

 

posted @ 2026-01-29 02:04  profesor  阅读(3)  评论(0)    收藏  举报