creating soft links with the same name as the target file
Asked Answered
E

2

8
ln -s /dir1/file1   /dir2/file1

I'd like to create a softlink in target dir1 with same filename as source in dir2 How is this done without typing the file1 name over in the target path

Excisable answered 8/2, 2014 at 2:5 Comment(0)
L
5

You can do it with ln-only options:

ln -s -t /dir1 /dir2/file1
Linettelineup answered 8/2, 2014 at 2:10 Comment(2)
so I assume I can also do this right ls -s -t /d1 /d2/* tyvmExcisable
I ended up using this one cp -rs /dir/<search pattern> /dest1Excisable
P
9

It is very frustrating typing the name over and over again if you're creating several symlinks. Here's how I bypass retyping the name in Linux.

Here's my example file structure:

source/
 - file1.txt
 - file2.js
 - file3.js
target/


Create symlink to single file

~$ ln -sr source/file2.js target/

Result:

source/
 - file1.txt
 - file2.js
 - file3.js
target/
 - file2.js


Create symlink to all files with matching extension in source

~$ ln -sr source/*.js target/

Result:

source/
 - file1.txt
 - file2.js
 - file3.js
target/
 - file2.js
 - file3.js


Create symlinks to all files in source

~$ ln -sr source/* target/

Result:

source/
 - file1.txt
 - file2.js
 - file3.js
target/
 - file1.txt
 - file2.js
 - file3.js




Relativity

Notice the r option. If you don't include -r the link source must be entered relative to the link location.

  • ~$ ln -s ../source/file1.txt target/ Works
  • ~/target$ ln -s ../source/file1.txt . Works
  • ~$ ln -s source/file1.txt target/ Does not work

See also:

How to create symbolic links to all files (class of files) in a directory?

Linux man pages

Pleader answered 11/7, 2019 at 15:53 Comment(0)
L
5

You can do it with ln-only options:

ln -s -t /dir1 /dir2/file1
Linettelineup answered 8/2, 2014 at 2:10 Comment(2)
so I assume I can also do this right ls -s -t /d1 /d2/* tyvmExcisable
I ended up using this one cp -rs /dir/<search pattern> /dest1Excisable

© 2022 - 2024 — McMap. All rights reserved.