Creating a symlink or alias that point to current user´s home sub-folder

0

Is it possible to create, using terminal on a mac, for a mac, a symlink or an alias to ANY currently logged in user of a mac?

This symlink/alias is to be included as a "drop destination" for a folder in a software package.

There are a few issues explaining how to make absolute vs relative symlinks, but this has to take into account that it is to be ANY system´s currently logged in user. And only be a symlink or an alias file, not an installer. That´s a bigger thing.

Here is my current best attempts:

symlink with ln -s and relative link, works well, but lacks the user account wild-card:

cd Desktop/any-folder/

ln -s "../symlinks" symlink-file

That one references a directory, "symlinks", in ../ (Desktop/), also on Desktop. Works if the relational structure is intact. I know that I need only remove the "" marks to make it a absolute link path.

But my attempt to create a omni-functional user folder link is not working:

ln -s "~/Desktop/symlinks" symlink-file

That´s the one I need to solve, if possible. Other libraries or ways are also welcomed.

andyNilson

Posted 2020-01-18T06:18:33.827

Reputation: 3

Answers

0

A symlink is to an actual unique location, so you can't have a symlink pointing to a user-dependent location.

Note that "~" is not a wildcard, it is more like a shell typing shortcut(*) that the shell immediately replaces by $HOME. Try

bash -c 'echo $1' foo ~/bar 

and you will see that what you get in $1 is /home/yourid/bar (or some OSX equivalent) and not ~/bar.

You could have more luck with OSX aliases, but I doubt it, this kind of feature looks like a disaster waiting to happen.

(*) See tilde expansion in the Bash reference manual

xenoid

Posted 2020-01-18T06:18:33.827

Reputation: 7 552

Okay, that´s the answer I was afraid of. So in your own account it works, but delivering to another person´s system is not going to work. Adding a shell script will work, but would require an actual installer. My solution is instead an Automator .app, a droplet for dropping the files on that first checks the user´s home folder and adds a path, then copies the files. That works. Thanks for the help. – andyNilson – 2020-01-19T00:25:09.480

0

"~" is expanded to your home directory only of its is "bare", ie ls ~ lists your home but ls "~" will try to list a '~' file/directory. In a script you should use $HOME instead:

ln -s "$HOME/Desktop/symlinks" symlink-file

xenoid

Posted 2020-01-18T06:18:33.827

Reputation: 7 552

Thanks, and for my own system that works great. But in my VMware os with OSX on it does not work at all. The $HOME is *my* home, not *any* user´s home folder. How do I do that? – andyNilson – 2020-01-18T20:21:11.147

An alias from where to where? And create by which id? You initial question is not very clear. Should there be a directpry with links to every user's home?In any case if your run in some admin/root id to create links to users you have to find a way to enumerate these users home dirs. On Linux that would be enumerating the subdirectories of /home/ or processing the contents of /etc/passwd. And remember that the id that creates the link owns it, so you may have to chown/chmod the link. – xenoid – 2020-01-18T20:32:33.623

I see. So there is no "wildcard" character or add-on to ln that automatically finds the current user on *any system* that uses the intended alias then? The alias/symlink is to be included in a .dmg file for easy installation into any user´s account on any computer running macos. When a user drops my application files onto the symlink or alias, the files are copied to the correct location.

Example:

  1. a user opens a dmg, downloaded from the web.
  2. he/she drags an application folder onto a symlink/alias that then copies it to his/her user-folder/sub-folder.
  3. < – andyNilson – 2020-01-18T21:13:15.980

EDIT of previous comment, first sentence: * I see. So there is no "wildcard" character that is the equivalent of "the current user name"? * – andyNilson – 2020-01-18T21:20:23.520