Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating soft links with the same name as the target file

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

like image 897
user1874594 Avatar asked Feb 08 '14 02:02

user1874594


People also ask

Can you create multiple hard links to the same file?

Because hard links point to an inode, and inodes are only unique within a particular file system, hard links cannot cross file systems. If a file has multiple hard links, the file is deleted only when the last link pointing to the inode is deleted and the link count goes to 0.

What command is used to create soft link to a file?

Ln Command to Create Symbolic Links Use the -s option to create a soft (symbolic) link. The -f option will force the command to overwrite a file that already exists.

What happened to the soft link in case the original file is renamed?

Answer. What happens to symlink if we rename a file ? Once you move a file to which symlink points, symlink is broken aka dangling symlink. You have to delete it and create new one if you want to point to the new filename.

Is symbolic link same as soft link?

A symbolic link, also termed a soft link, is a special kind of file that points to another file, much like a shortcut in Windows or a Macintosh alias. Unlike a hard link, a symbolic link does not contain the data in the target file. It simply points to another entry somewhere in the file system.


2 Answers

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

like image 69
Michael Cox Avatar answered Sep 30 '22 12:09

Michael Cox


You can do it with ln-only options:

ln -s -t /dir1 /dir2/file1
like image 25
cnicutar Avatar answered Sep 30 '22 13:09

cnicutar