this post was submitted on 30 Jan 2025
28 points (100.0% liked)

Linux Questions

1281 readers
60 users here now

Linux questions Rules (in addition of the Lemmy.zip rules)

Tips for giving and receiving help

Any rule violations will result in disciplinary actions

founded 2 years ago
MODERATORS
 

Is there anything that could help me memorise the argument order of the ln command?

I'll take anything from little helper phrases over shell extensions to replacements written in Rust. I only use the command occasionally, but I need a fool-proof solution. Do you have any tricks to get it right?

@linuxquestions

#AskFedi #Linux

top 22 comments
sorted by: hot top controversial new old
[–] [email protected] 14 points 1 day ago

Nope, you will, like me, overwrite your files until the day we die

[–] [email protected] 11 points 1 day ago (2 children)

It’s the same as cp or mv. Source dest.

[–] [email protected] 4 points 1 day ago (1 children)
[–] [email protected] 5 points 1 day ago (1 children)

Think of it like making a fake copy? Source file is first, symlink is second.

[–] [email protected] 2 points 1 day ago (1 children)

That's what I mean: what you're going to operate on; where you're going to put it.

[–] [email protected] 3 points 1 day ago

Ahhh sorry I read it as a question

[–] [email protected] 2 points 1 day ago

This is how I remember as well. I write the command in my head if I would copy the file, than replace cp with ln

[–] [email protected] 2 points 22 hours ago (1 children)

I remember it because it has the one-arg form which only takes the target, so that's the first arg. Good luck!

[–] [email protected] 2 points 17 hours ago

This is the one that works for me, too.

A link operation is meaningless without a target, so that must come first.

If you don't specify a name for the link, a reasonable name can be inferred from the target. So, the link name goes last, where it can be potentially omitted.

[–] [email protected] 9 points 1 day ago* (last edited 1 day ago)

Link shoots his target before asking for names.

[–] [email protected] 8 points 1 day ago (1 children)

@fell @linuxquestions IIRC it is just like with cp:

cp some_existing_file new_file
ln -s some_existing_file new_file

[–] [email protected] 8 points 1 day ago* (last edited 1 day ago) (1 children)

I can understand OP's confusion, though.

For move and copy it's pretty certain which is the 'from' and which is the 'to' - the order intuitively makes sense.

For symlinks it's more ambiguous based on your personal mental model.

For example, if you think about symlinks from the perspective of the original file before a link is created, then the original file represents the 'from' and the link is the 'to' (CORRECT)

But if you are thinking from the perspective of using a link after it's created, then you can easily imagine the symlink as the 'from' - because that's where you start when you follow it, and the target file/dir as the 'to' - because that's where you arrive after following it. (INCORRECT)

So I totally get the ambiguity.

[–] [email protected] 3 points 1 day ago

You worded that goodly.

[–] muhyb 3 points 1 day ago

If you're gonna need that only for your machine, you can easily create aliases that can you can remember.

[–] [email protected] 2 points 1 day ago

I'm glad to see I'm not the only person who can never remember this. The docs don't help me either, I have to just try stuff

[–] [email protected] 3 points 1 day ago

The order is source target, just like with cp. "source" is the existing thing and "target" is the new thing.

[–] [email protected] 3 points 1 day ago

I've been using linux for a long time and I still mix it up. I just check tldr before typing the command.

[–] zero_spelled_with_an_ecks 2 points 1 day ago

All this stuff about source and destination and target or whatever hasn't helped me because those terms are too confusing when it comes to what they do with links. I like to think of it as hitching a new car onto a train. ln is the locomotive, the car that's already there comes next, and then you link the new one to it.

[–] eternacht 2 points 1 day ago

I think of it in terms of the cp command: Source first, followed by destination. The last argument is always the new file/link you are creating.

[–] CameronDev 2 points 1 day ago

You presumably have a common usage for it, why not create an alias for that usage?

[–] [email protected] 2 points 1 day ago

@fell

@linuxquestions

I check the man everytime 😅

[–] [email protected] -2 points 1 day ago

@[email protected] After using unix (mostly not linux!) for 10 years I found that I just knew it and always got it right. I haven't had to think about ln in the last 20 years - my fingers just work. I guess that is one advantage to getting older/wiser. (I'm just hoping I don't get cognitive decline like so many do, though probably I wouldn't for 20-30 more years)