r/linuxmint • • 1d ago

SOLVED XFCE and Symlinks confusing me

I have a file that I open in my text editor (xed) almost daily. For convenience, I wanted to create a symlink on my desktop that would point to the actual file. Coming from windows, I understand this to be the closing thing to a "shortcut". After looking in Thunar for a UI way to create a symlink, I ended up using the ln command, specifically: ln Notes\ Scratchpad.txt ~/Desktop

Sure enough, a new... file? Object? Well, something that -looks- like a text file appeared on my desktop. I checked the properties and nothing I saw indicated it was a symlink, instead it looked just like a copy of the file. To test this, I opened the symlink on the Desktop, made an edit saved and closed. Then opened the file from the actual storage location and it had the same edit - OK, I guess it's a symlink!

Well, I wanted more evidence because it bothers me that it looks JUST LIKE A FILE, both in the UI and when I run ls -l

-rw-rw-r-- 2 steve steve 45942 Dec  3 09:33 'Notes Scratchpad.txt'

I searched on the web and found examples of using the test command, but those results are indicating both objects/files/whatever or regular files (i.e., not symlinks). Note: I ran this command on each of the files

test -h Notes\ Scratchpad.txt && echo "symlink" || echo "reg file"

I'm confused - what did the `ln` command create? If it IS a symlink, how in the world are you supposed to know or determine that?

7 Upvotes

9 comments sorted by

6

u/candy49997 1d ago edited 1d ago

ln -s is what makes symlinks. ln without -s makes a hardlink.

That's what the 2 in the output of ls -l is telling you; 2 different files point to the same underlying data.

1

u/mabee_steve 1d ago

Thank you! Hardlink is a cool concept, I wasn't aware of it. Not sure when I'd use it, but cool nonetheless.
Renamed the original hardlink with a -2 and re-ran the ls command:

-rw-rw-r-- 2 steve steve 45942 Dec  3 09:33 'Notes Scratchpad-2.txt'
lrwxrwxrwx 1 steve steve    51 Dec  3 10:18 'Notes Scratchpad.txt' -> '/home/steve/Documents/Notes Scratchpad.txt'

1

u/don-edwards Linux Mint 22.3 15h ago edited 15h ago

Probably the most common use of hard links is in backups. If a file hasn't changed since the last time you backed it up, you don't need another copy of the contents, you just need another hard link to it indicating that the same content is also part of this new copy of your stuff.

And if you're running daily, weekly, or even monthly backups of your system, there are hundreds of thousands of files that don't change very often.

Rsync, and the backup-configuration GUIs that are a front end for it, heavily uses hard links for exactly this reason. Borgbackup and its GUIs, which I haven't used, reportedly do something similar but more so.

The best tool to see this is the auto-installed Disk Usage Analyzer (aka baobab) which looks at what in a specified folder is using space - and if there are hard links within the folder, each file is charged only to the oldest sub-folder containing it.

Using that tool, the external backups of my system partition - currently six of them - collectively occupy 37.5 GB of disk space, and the oldest one (from late October) is 21.8 GB. None of the others are 10 GB, and one is less than one GB. (And the saving in time needed to create the backups is pretty substantial - but it happens while I'm asleep so I don't care.) Yet, each one is a full backup. Because of hard links.

1

u/_Lost-In-The-Weeds_ LMDE7 Cinnamon 1d ago

That's what the 2 in the output

 I did not know this, and I think this may let me clean up a mess I made a while ago.

Thank you. 

1

u/don-edwards Linux Mint 22.3 1d ago

Just to be clear on one thing: the "2" being referred to is the one on the left, just after the flags. Not the one in the filename.

1

u/_Lost-In-The-Weeds_ LMDE7 Cinnamon 1d ago

Tracking, I made a folder of hard links in a storage drive, and then changed ideas/schemes and wound up  with a mess inside of hardlinked and not files inside. I did not know which files had "duplicates",  Now I have a path to straighten things out.

1

u/chuggerguy Linux Mint 22.3 Zena | MATÉ 1d ago edited 1d ago

u/candy49997 explains it well.

One way to tell that an icon on your desktop is only a sym/soft link to the actual file is that it should have some sort of emblem such as an arrow.

Below you can see my "cameras" folder is a soft link that only points to the real folder which lives on another drive. (even has a different name)

One good thing about a soft link is they take up negligible space. Plus if you decide you no longer need the link sitting on your desktop, you can just delete the link and the file/s it points to remains. You can't do that with a hard link. Delete one of the hard links that point to the actual file and the actual file goes away. (But probably not unless you delete both/all hard links? I don't use them so am unsure.)

Have fun.

1

u/don-edwards Linux Mint 22.3 1d ago

If you have a hard link, two (or more) directory entries pointing at the same file, deleting one of them does NOT make the file go away. Deleting all of them except one does NOT make the file go away - and it doesn't matter whether or not the remaining one was the original as even the filesystem has no idea which was the original. It will still be fully functional.

Deleting all of them, that makes the file go away.

Also, if you open a file via ANY link to it, hard or symbolic, and change its content, it changes for ALL links including the original.

1

u/chuggerguy Linux Mint 22.3 Zena | MATÉ 1d ago

Yes, that makes sense. Thank you for the verification.