LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 09-12-2007, 03:26 PM   #1
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Test if two files are on the same file-system


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?
 
Old 09-12-2007, 03:59 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
You may be able to find a utility for this on Source Forge or Fresh Meat.

http://sourceforge.net

http://freshmeat.net
 
Old 09-12-2007, 06:30 PM   #3
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
I checked already :-( I was hoping the answer would lie in the options of a familiar command.
 
Old 09-12-2007, 06:41 PM   #4
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
 
Old 09-12-2007, 07:45 PM   #5
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
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.
 
Old 09-12-2007, 08:07 PM   #6
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
Code:
stat  .bashrc
  File: `.bashrc'
  Size: 101             Blocks: 2          IO Block: 4096   regular file
Device: fc00h/64512d    Inode: 4712796     Links: 1
Access: (0600/-rw-------)  Uid: ( 1001/  melvin)   Gid: ( 1001/  melvin)
Access: 2007-09-12 14:55:35.000000000 -0400
Modify: 2007-05-27 17:48:36.000000000 -0400
Change: 2007-06-10 11:17:01.000000000 -0400

stat  /etc/passwd
  File: `/etc/passwd'
  Size: 1793            Blocks: 8          IO Block: 4096   regular file
Device: 303h/771d       Inode: 422733      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2007-09-12 21:08:42.000000000 -0400
Modify: 2007-08-23 20:27:32.000000000 -0400
Change: 2007-08-23 20:27:32.000000000 -0400
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.
 
Old 09-12-2007, 08:25 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,099

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
"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.
 
Old 09-12-2007, 10:24 PM   #8
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
Great! Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File system and > 2TB Files Squeesh Linux - Distributions 3 07-27-2006 05:43 PM
mkdir: cannot create directory `test': Read-only file system punt Linux - General 2 04-16-2005 09:58 PM
File test to make sure files aren't damaged/corrupt bluedevlx Linux - Newbie 1 11-04-2004 12:11 PM
File Test to make sure files aren't corrupt/damaged bluedevlx Programming 2 11-04-2004 11:50 AM
need help recovering files on an ext2 file system rmanocha Linux - Software 2 11-06-2003 10:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration