Find the answer to your Linux question:
Results 1 to 6 of 6
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #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

  2. #2
    Linux Guru Jonathan183's Avatar
    Join Date
    Oct 2007
    run ln -i ... and take a look here see creating links and removing files ... and then read understanding inodes on the same page

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Seattle, WA, USA
    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.

    ln -s file1 file2
    Now file2 references the contents of file1. If file1 is deleted, then file2 is a broken link, and will have no data.

    If you run a command like:
    ls -l /dev/cdrom
    You should see an example of a symlink: /dev/cdrom probably points to another device file which is a specific disk and partition.

  4. $spacer_open
  5. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    3rd rock from sun - Often seen near moon
    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 Online Linux Terminal

  6. #5
    Linux Guru Jonathan183's Avatar
    Join Date
    Oct 2007
    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

    find . -inum put_inode_number_here -exec rm -i {} \;
    Ed:if your feeling brave you can drop -i from rm ... so you are not prompted before removing files
    Ed2: or use Irithori suggestion below
    Last edited by Jonathan183; 02-06-2011 at 01:36 PM.

  7. #6
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts