[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ale] Ale Digest, Vol 140, Issue 10



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ale-request at ale.org wrote:
> Message: 7
> Date: Wed, 11 Apr 2007 14:02:23 -0400
> From: "Michael B. Trausch" <fd0man at gmail.com>
> Subject: Re: [ale] link
> To: Atlanta Linux Enthusiasts <ale at ale.org>
> Message-ID: <1176314544.27582.3.camel at sage>
> Content-Type: text/plain; charset="utf-8"
> 
> On Wed, 2007-04-11 at 13:06 -0400, Bj?rn Gustafsson wrote:
> 
>> > Hard linking is decidedly quicker than a copy, because you are only
>> > creating a directory entry, not actually copying any data.  (It does
> 
> A question that comes to mind:  Is it possible using C or Python or
> something to get the inode of a file, find all links to that inode, and
> remove them all individually, so as to delete a hardlinked file without
> leaving anything behind?  I know that given a directory entry, you can
> stat it and find the file metadata, including the inode, but can you do
> the reverse somehow?

Mike,

In C, you can use the stat(2) system call to obtain information about a file
given a file name.  You can also use "ls -i filename" to obtain the inode
number.  You already have utilities at hand to accomplish what you desire.
Perhaps the following shell script will help:

#!/bin/bash
#
file="/tmp/file"
#
# Find the mount point of the file system containing the file
mountpoint=$(df "$file" | sed -e 1,1d | awk '{print $6;}')
#
# Find the inode number of the file in question
inode=$(ls -i "$file" | awk '{print $1;}')
#
# Traverse the file system looking for file name
# references to a specific inode.
find "$mountpoint" -mount -inum $inode

If you want to delete the files, just add "-exec rm {} \;" to the end of your
find.  Only when the last file name reference to the inode is removed (i.e. it's
link count goes to zero) does the file and its contents go away.

Hope this helps out some.

Enjoy,
    - Chuck

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGHTOXiR3HaLbYCa4RAtZkAJ9F0ZzRhG55Ehg4Nrrjw6BKZhixKQCeL+bC
k/52WCcdXs1QMaxohwAVXIg=
=GCRb
-----END PGP SIGNATURE-----