Results 1 to 6 of 6
I created a hard link
vi file1
"some stupid text"
then ln file1 file2
cat file2
"some stupid text"
the contents of file1 are now in file2
If i delete ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 02-03-2011 #1Just Joined!
- Join Date
- Feb 2011
- Posts
- 1
Hard links - deleting files
I created a hard link
vi file1
"some stupid text"
then ln file1 file2
cat file2
"some stupid text"
the contents of file1 are now in file2
If i delete file1 the contents of file1 are still in file2
rm file1
Is there a command that removes the contents of file1 in file2?
when i remove file1 shouldn't file2 be removed?
thank you
- 02-03-2011 #2
run ln -i ... and take a look here see creating links and removing files ... and then read understanding inodes on the same page
- 02-04-2011 #3
To give a quick overview:
You have filenames. A filename actually doesn't refer to a file; it points to an "inode". The inode in turn contains information on the file: its permissions, size, and where it lives on disk.
When you run the command "ln file1 file2", you are creating a new filename (file2) that points to the same inode as "file1". Similarly, when you run "rm file1", you are simply removing the link from file1 to its inode. "rm" is in fact usually just a wrapper around the unlink system call, which better represents exactly what it does.
An inode is only actually deleted when all filenames referencing it have been removed and no programs have the file opened. Even then, the file data is not removed from the disk until it is overwritten by something else.
This is specific to *nix filesystems generally. Windows filesystems (certainly FAT, and I think NTFS) work slightly differently.
A key point to this is that inodes do not know what filenames link to them, so you cannot automatically delete all filenames for an inode without walking the entire filesystem.
One way to accomplish more of what you are talking about is "symbolic links". These are filenames that do not link to an inode, but instead to another filename.
Now file2 references the contents of file1. If file1 is deleted, then file2 is a broken link, and will have no data.Code:ln -s file1 file2
If you run a command like:
You should see an example of a symlink: /dev/cdrom probably points to another device file which is a specific disk and partition.Code:ls -l /dev/cdrom
- 02-04-2011 #4
I'll second that,use symlinks. But remember , you need to delete the file not symlink for actually file to be deleted.
ln -s file.txt sym.txt
Now you can access file's content via file.txt or sym.txt -- but if you want to delete the file remove "file.txt" ..First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-----
FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org
- 02-06-2011 #5
My strategy for removing files with hard links is:-
discover inode of file (using ls -i or stat)
locate all files with inode number find -inum put_the_file_inode_number_here
rm files listed by find
I prefer to run these commands individually ... but if you have lots of links or are feeling more confident you can use exec option to run rm with find ... something like
Ed:if your feeling brave you can drop -i from rm ... so you are not prompted before removing filesCode:find . -inum put_inode_number_here -exec rm -i {} \;
Ed2: or use Irithori suggestion below
Last edited by Jonathan183; 02-06-2011 at 01:36 PM.
- 02-06-2011 #6
Jonathan183´s method will work.
Small update:
find . -inum put_inode_number_here -delete
This should be faster.You must always face the curtain with a bow.


Reply With Quote
