Pergunta

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

Foi útil?

Solução 2

You can do it with ln-only options:

ln -s -t /dir1 /dir2/file1

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top