Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is there any command to test if two files reside on the same file system? I would ideally like the test to dereference sylinks, so knowing they are in the same directory may not be enough.
I see that when I 'stat' a file, I am given a device identifier, but what if two files are on the same device (hard disk) but different partitions and thus different file-systems? Or do different partitions have different "device" numbers?
You see these things for Windows all the time. I had expected someone to create one for Linux by now.
Well, you could write a bash script to do this. If I wanted this I would write a bash script that would generate an MD5 sum for each file on the system. The sum and the name of its file would be put into a text file. When all of the files have been summed I would sort that file based on the sum, which would be the first element in each line. Then look for duplicates. Thinking about this reminded me of my college days.
I'm not sure I've been clear about what I'm trying to solve. I want to test if two files reside on the same filesystem. I shouldn't need to checksum my entire hard drive to see if a file is on the flash drive or CD.
Ooops. I thought you wanted to find duplicate files. Don't ask why. I don't know. All right then.
You can use the stat command as you mentioned in your original post. The following example shows a stat for /etc/passwd and for ~/.bashrc. These files on on different partitions of the same disk.
You can see that the device identifier is different for each file. The same thing is true for soft links on different partitions.
Hard links have got to reside on the same partition/file system. Hard links are exactly the same as all other hard links pointing to the same file so they do not need to be dereferenced. The inode number will be the same in all hard links pointing to the same file.
Last edited by stress_junkie; 09-12-2007 at 08:23 PM.
"stat -t" should work - that device number (field 7) is a combined major/minor by the looks of it. 0xMMmm - where the leading zero gets dropped. Same device (hard disk) will have same major number - each partition will have a different minor number.
Simple check for equality should suffice.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.