Questions tagged [hard-link]

A hard link is file system entry that associates a name with another file on a file system.

Filter by
Sorted by
Tagged with
130
votes
8answers
54k views

Why are hard links to directories not allowed in UNIX/Linux?

I read in text books that Unix/Linux doesn't allow hard links to directories but does allow soft links. Is it because, when we have cycles and if we create hard links, and after some time we delete ...
27
votes
4answers
14k views

Can a file be retrieved by its inode?

I ran following commands in the order specified: $ln a b $ls -i a b 523669 a 523669 b $rm -f a $ls -i b 523669 b I concluded from this test that the command rm actually removes only the filename (a ...
55
votes
10answers
36k views

What is the difference between symbolic and hard links?

When would you use one over the other?
137
votes
19answers
45k views

Is there an easy way to replace duplicate files with hardlinks?

I'm looking for an easy way (a command or series of commands, probably involving find) to find duplicate files in two directories, and replace the files in one directory with hardlinks of the files in ...
78
votes
6answers
24k views

Why do hard links exist?

I know what hard links are, but why would I use them? What is the utility of a hard link?
51
votes
5answers
64k views

Determining if a file is a hard link or symbolic link?

I'm creating a shell script that would take a filename/path to a file and determine if the file is a symbolic link or a hard link. The only thing is, I don't know how to see if they are a hard link. ...
51
votes
2answers
3k views

Why is '.' a hard link in Unix?

I've seen many explanations for why the link count for an empty directory in Unix based OSes is 2 instead of 1. They all say that it's because of the '.' directory, which every directory has pointing ...
15
votes
1answer
4k views

How to find all the links to a directory

When I stat a directory I get a listing that tell me there are 5 links to the directory. stat dir My question is how do I get information (names and locations) to all these 5 links?
8
votes
1answer
4k views

Hard link permissions behavior different between CentOS 6 and CentOS 7

I'm getting a permissions error in CentOS 7 when I try to create a hard link. With the same permissions set in CentOS 6 I do not get the error. The issue centers on group permissions. I'm not sure ...
6
votes
1answer
238 views

The number of links for a folder doesn't reflect the real status?

A question for ls command. root@cqcloud script]# ls /var/www/html -la total 36 drwxr-xr-x 9 root root 4096 Aug 31 01:12 . drwxr-xr-x 7 root root 4096 Aug 31 01:10 .. drwxr-xr-x 2 root root 4096 ...
52
votes
5answers
21k views

How to copy a directory recursively using hardlinks for each file

I want to create a "copy" of a directory tree where each file is a hardlink to the original file Example: I have a directory structure: dirA/ dirA/file1 dirA/x/ dirA/x/file2 dirA/y/ dirA/y/file3 ...
36
votes
8answers
6k views

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

A hard link is defined as a pointer to an inode. A soft link, also known as a symbolic link, is defined as an independent file pointing to another link without the restrictions of hard links. What is ...
40
votes
5answers
39k views

How to copy directories with preserving hardlinks?

How to move directories that have files in common from one to another partition ? Let's assume we have partition mounted on /mnt/X with directories sharing files with hardlinks. How to move such ...
37
votes
1answer
37k views

How to find all hard links to a given file? [duplicate]

How can we find all hard links to a given file? I.e., find all other hard links to the same file, given a hard link? Does filesystem keep track of the hard links to a file? The inode of a file only ...
21
votes
2answers
8k views

Dereferencing hard links

In the manual page of tar command, an option for following hard links is listed. -h, --dereference follow symlinks; archive and dump the files they point to --hard-dereference follow ...
9
votes
1answer
1k views

Finding all the hard-links of a file without traversing the file system

I have seen in this page that inodes have a link counter to know how many files (read: "directory entry") point to this inode. Is there a way to know which directories contain such entries without ...
2
votes
2answers
1k views

Is a -rw-rw-rw- file really inaccessible inside a drwxrwx— directory?

Say I have a directory with these permissions: drwxrwx--- Inside this directory, a file with these permissions: -rw-rw-rw- Is the file readable/writable by everyone or not ? If not, how secure is ...
11
votes
6answers
5k views

Breaking a hard-link in-place?

I am keeping my dotfiles under version control and the script deploying them creates hard links. I also use etckeeper to put my /etc under version control. Recently I have gotten warnings like this: ...
8
votes
5answers
7k views

Can I determine the number of sub-directories in a directory using `ls -l`?

When displaying directories using ls -l, their number of links (the second field in the output) is at least two: one for the dir name and one for . $ mkdir foo $ ls -l total 2 drwxr-xr-x 2 user ...
6
votes
2answers
3k views

Info on cp --preserve=links

I'm trying to understand what cp --preserve=links does when used by itself, from my tests it seems that it copies a normal file normally and dereferences symlinks, but it seems like it just has the ...
4
votes
4answers
513 views

How do I prevent sed from destroying hardinks?

My question is similar to How do I prevent sed -i from destroying symlinks?, but concerning hardlinks. Using sed -i to work on a file destroys all the hardlinks the file has, since sed works by ...
4
votes
2answers
2k views

Keep tracking of symbolic links?

I use symbolic links quite often, but after moving the original file, I lose track of the symbolic link. I also use symbolic links for keeping track of some files in the same directory, but again, I ...
1
vote
1answer
234 views

How to get a list all the “aliases” of a file (including of type directory) efficiently?

How can one get the list of all the aliases of a file (including of type directory) efficiently, given that a file: may have multiple hard links, multipe soft links, multi-level soft links may be ...
0
votes
2answers
1k views

rsync --link-dest not working as expected with symlinks

I'm using rsync to backup some of my files: rsync -aEN --delete --link-dest="$CURR/" "$SOURCE/" "$NEW/" The --link-dest option works fine with most files, but not with symlinks. When I was writing a ...
59
votes
3answers
58k views

What happens when you delete a hard link?

If you do rm myFile where myFile is a hard link, what happens?
21
votes
5answers
11k views

Why are hard links only valid within the same filesystem?

I am reading this intro to the command line by Mark Bates. In the first chapter, he mentions that hard links cannot span file systems. An important thing to note about hard links is that they only ...
15
votes
2answers
3k views

cp behaves weirdly when . (dot) or .. (dot dot) are the source directory

This answer reveals that one can copy all files - including hidden ones - from directory src into directory dest like so: mkdir dest cp -r src/. dest There is no explanation in the answer or its ...
21
votes
2answers
2k views

Do hard links count as normal files?

I was wondering if there was a way to register this, but since most modern search engines don't work well with phrases over about 5 words in length, I need some help on this one. I was wondering ...
5
votes
2answers
1k views

Linux: How does hard-linking to a directory work?

I'm aware that Linux does not allow hard-linking to a directory. I read somewhere, that this is to prevent unintentional loops (or graphs, instead of the more desirable tree structure) in the file-...
24
votes
3answers
7k views

Is there a limit of hardlinks for one file?

Is there a limit of number of hardlinks for one file? Is it specified anywhere? What are safe limits for Linux? And what for other POSIX systems?
15
votes
2answers
12k views

Is there a difference between hardlinking with cp -l or ln?

I am implementing a backup scheme using rsync and hardlinks. I know I can use link-dest with rsync to do the hardlinks, but I saw mention of using "cp -l" before "link-dest" was implemented in rsync. ...
14
votes
3answers
7k views

How to `du` only the space used up by files that are not hardlinked elsewhere?

Using rsync --link-dest for space-saving snapshots, how can I figure out how much space I actually saved? Or more general: How to figure out how much space a directory uses considering only files ...
14
votes
2answers
58k views

Invalid cross-device link while Hardlinking in the same file system

I have /home/myuser/Desktop/rc/.netrc file that i want hardlink to /root, ie home directory of root user. When i do: ln /home/user/Destkop/rc/.netrc /root it gives the following error: ln: ...
5
votes
1answer
2k views

unpacking tarball with hard links on a file system that doesn't support hard links

I got a tarball (let's say t.tar.gz) that contains the following files ./a/a.txt ./b/b.txt where ./b/b.txt is a hard link to ./a/a.txt. I want to unpack the tarball on a network file system (AFS) ...
5
votes
2answers
3k views

Link value is 2 by default for folders

Can anyone help me to understand the logic behind the link value being "2" for the first time when we create any folders/ directory in linux. I searched a lot , but couldn't get satisfactory logic
4
votes
2answers
3k views

Convert a hardlink into a symbolic link

It is easy to convert a symlink into a hardlink with ln -f (example) It would also be easy to convert a hardlink (filenames link and original) back to a symbolic link to link->original in the case ...
2
votes
1answer
118 views

is there some kind of symbolic links that support moving or renaming the target file?

I liked this explanation of hard and soft/symbolic links: https://unix.stackexchange.com/a/22406/42226 Now the reason why I googled about soft and hard links is simply because I was organizing my ...
0
votes
1answer
2k views

What is the difference between “find -H” and “find -L” command?

When I executed the command both commands gave the same output. I created a soft link and a hard link for a file but still both commands gave the same output. Is there a difference between find -H and ...
3
votes
4answers
2k views

Forcibly create directory hard link(s)?

I understand the reasoning why nearly every unix version doesn't allow hard-linking of directories (in fact HFS+ on OS X is the only one I know, but even that isn't made easy to do yourself). However, ...
2
votes
1answer
4k views

How do I manually modify an inode?

I am using Ubuntu Linux and, just for fun, I want to create a hardlink to a directory (as seen here). Because I'm just doing this for fun, I'm not looking for any sort of pre-developed directory-...
1
vote
1answer
101 views

Why does looking for hardlinks fail?

Here is how I find hard links located in /tmp: $ for file in /tmp/*; do if [ "$(stat -c %h -- "$file")" -gt 1 ]; then ls "$file" -l ; fi; done total 0 srwxrwxr-x 1 t t 0 Mar 20 23:01 debconf.socket ...