Linux: Differencebetween These Two Symbolic Link Commands

linux: what is the difference between these two symbolic link commands

A symbolic link is implemented as a file containing the name of the target.

There is a minor difference, as you've seen: one of the symlinks has a trailing /, and the other doesn't. You can see the difference in the output of ls -l; on a lower level, this shows up as a difference in the path returned by the readlink() system call.

But there should be no functional difference between them -- as long as the target is a directory. Either can be used to access the linked directory.

For a target that's not a directory, this:

ln -s /etc/motd good_link
ln -s /etc/motd/ bad_link

will result in good_link being a valid way to access /etc/motd, and bad_link resulting in a not a directory error.

What is the difference between a symbolic link and a hard link?

Underneath the file system, files are represented by inodes. (Or is it multiple inodes? Not sure.)

A file in the file system is basically a link to an inode.

A hard link, then, just creates another file with a link to the same underlying inode.

When you delete a file, it removes one link to the underlying inode. The inode is only deleted (or deletable/over-writable) when all links to the inode have been deleted.

A symbolic link is a link to another name in the file system.

Once a hard link has been made the link is to the inode. Deleting, renaming, or moving the original file will not affect the hard link as it links to the underlying inode. Any changes to the data on the inode is reflected in all files that refer to that inode.

Note: Hard links are only valid within the same File System. Symbolic links can span file systems as they are simply the name of another file.

Symbolic Link Edits and differences to hard link

When you do cd link/, your current directory becomes /path/to/real and any changes you make in the directory are in 'the real directory'.

Beware of cd -L vs cd -P — see POSIX on cd — and similarly with pwd.

Is there a difference between commands cp -P and cp -d?

They have the same effect on symbolic links. But -d has the additional effect of preserving hard links. That is, with -d or --preserve=links, if an invocation of cp encounters multiple links to the same file, it will create multiple links to the same file in the destination. Ordinarily cp doesn't pay attention to hard links and creates files that happen to have identical contents if two source files are hard links.

$ touch foo
$ ln foo bar
$ mkdir d; cp -d foo bar d
$ mkdir P; cp -P foo bar P
$ ls -log d P
P:
total 0
-rw-rw-r-- 1 0 Apr 11 17:09 bar
-rw-rw-r-- 1 0 Apr 11 17:09 foo

d:
total 0
-rw-rw-r-- 2 0 Apr 11 17:09 bar
-rw-rw-r-- 2 0 Apr 11 17:09 foo

What's the difference between ln -s and alias?

They're entirely different things, though in this case they can be used for similar purposes.

This:

alias subl="/Applications/path/to/subl"

creates an alias, so that typing subl as a shell command is equivalent to typing /Applications/path/to/subl.

In bash, functions are generally preferred to aliases, because they're much more flexible and powerful.

subl() { /Applications/path/to/subl ; }

Both these things are specific to the shell; they cause the shell to expand sub1 to a specified command.

ln -s, on the other hand, creates a symbolic link in the file system. A symbolic link is a reference to another file, and for most purposes it can be treated as if it were the file itself. It applies to anything that accesses it, not just to the shell, it's immediately visible to all processes running on the system, and it persists until it's removed. (A symbolic link is implemented as a small special file containing the name of the target file.)

How to find given file is a symbolic (soft) link or hard link

No, you need to use lstat() to be able to detect if a file is a soft link.

Also make sure that you understand that typically, there are not three different types of files: files, hard links to files, and soft links to files. Instead, there are only two: hard links to files and soft links. What you might think of as "the file" is in fact a hard link too, it's just typically the single link.



Related Topics



Leave a reply



Submit