3

I realise that every file is a hard link. This is what I mean precisely: if an inode has more than one file pointing to it, how can I copy the inode so that every file is pointing to a separate inode with the same content?

For example:

echo "Example" > one
ln one two

How can I make the file two have the same contents as one, without sharing an inode? I would like to "reduplicate", if you like, the files.

Flimm
  • 4,218

1 Answers1

2

This can be achieved with the command

find -samefile filename -exec sed -i ';;' {} \;

or if you now the inode number of the file

find -inum inode -exec sed -i ';;' {} \;

Note both these commands only find the files with matching inodes in subdirectories of the current working directory. If you need to search all files on your file system, you will need to run this command from the root directory.

The first part find -samefile filename, finds all the files which share the same inode. Then it executes sed -i ';;' which copies the file to a file with the same name (note we use the sed script ';;' instead of ';', otherwise, find will interpret the argument ; as the end of the -exec command).

ᅟᅟᅟ
  • 136
  • Great solution! This doesn't seem to work for the sed on macOS though: when running sed -i ';' FILENAME, I get this error: sed: 1: "FILENAME": invalid command code F – Flimm Sep 27 '16 at 14:50
  • I suggest you post this answer at http://unix.stackexchange.com/q/66815/9041 before I beat you to it ;) – Flimm Sep 27 '16 at 14:53
  • @GAD3R, why did you reject my edit? You and techraf both said "This edit was intended to address the author of the post and makes no sense as an edit. It should have been written as a comment or an answer" and that is clearly wrong. – Flimm Sep 28 '16 at 09:22
  • @techraf, you too, why did you reject my edit, selecting a clearly wrong reason? – Flimm Sep 28 '16 at 09:23
  • Note that this method modifies the timestamp. If you wish to preserve file attributes, check the linked question at the top. – syockit Oct 14 '20 at 13:22