I know that when creating symbolic links the paths must be absolute, or relative to the destination location

If I'm making a link in /usr/local/bin that points to a file in the current directory, is there an easier way - something, for example, that works with tab-completion in bash - than to do ...

ln -s $(pwd)/target_file /usr/local/bin/

(Where $(_something_) evaluates the _something_ and substitutes it into the shell command.)

  • ln -s $PWD/<tab> works for me. – muru Nov 5 '18 at 21:51
  • Well, I guess using the shell variable is better than using substitution. Thanks for the info. (Why not make it an answer?) – user3.1415927 Nov 5 '18 at 22:05
  • Wasn't sure if it worked for you. – muru Nov 5 '18 at 22:10
  • Indeed it does. – user3.1415927 Nov 5 '18 at 22:10

Tab-completion for files works with the $PWD variable for me:

bash-4.4$ touch bar baz
bash-4.4$ ln -s $PWD/ba<tab>
bar  baz

For completion, some alternatives in zsh:

  • ~0 (or ~+0) is a slightly shorter alternative for $PWD (and filename completion works just as well after $PWD/fi<Tab> than after ~0/fi<Tab>, or $(pwd)fi<Tab>); ~1, ~2... are the previous directories you've been in, the completion system can also be configured to show you which they were and be expanded (upon ~+<Tab>).
  • One can use ln -s file(:A) /usr/local/bin, where (:A) causes file to be expanded to its absolute path (beware that it resolves symlinks to their targets though).

Also note the -r/--relative option of GNU ln:

ln -rs file /usr/local/bin/

Would make a /usr/local/bin/file symlink that points (as a relative path, so maybe something like ../../../home/you/file) to the file in the current directory, similar to:

ln -s "$(realpath -s --relative-to=/usr/local/bin file)" /usr/local/bin

(with GNU realpath).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.