LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 06-27-2008, 04:20 PM   #1
JakeWharton
LQ Newbie
 
Registered: Jun 2008
Posts: 2

Rep: Reputation: 0
[SOLVED] BASH: finding if two files are on the same device


I have a bash script that deals with copying very large (>4GB) files from one folder to another. To prevent loss of data I copy the file, check that the file sizes are equal, and then remove the old one. I'm looking for a way to determine when two files in different directories are on the same device so that I can merely rename the file. There most definitely will be cases of cross-device symlinking (shown in the example below) which prevents me from using something like `dirname`. I'll result to parsing a mount listing if there is no simple command(s) to resolve a file to its device.

mounts:
/dev/hda1 -> /
/dev/hda2 -> /mnt/a
/dev/hdb1 -> /mnt/b

symlinks:
/mnt/a -> /mnt/c/a/
/mnt/b -> /mnt/c/b/

files:
/mnt/c/a/old.file
/mnt/c/b/new.file

It would appear the two files are both on /dev/hda1 but in fact the old one is on /dev/hda2 and the new one is on /dev/hdb1. This is just a simple example to show my problem and in no way represents the actual directory structure (which is more complicated).

Last edited by JakeWharton; 06-28-2008 at 01:03 AM. Reason: Problem was solved
 
Old 06-27-2008, 05:00 PM   #2
beadyallen
Member
 
Registered: Mar 2008
Location: UK
Distribution: Fedora, Gentoo
Posts: 209

Rep: Reputation: 36
Use 'stat -c "%d" filename'. It gives you the device id of the file (along with loads of other stuff). The device id's are unique for each disk/partition, so it should serve your purpose.
 
Old 06-28-2008, 01:03 AM   #3
JakeWharton
LQ Newbie
 
Registered: Jun 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by beadyallen View Post
stat -c "%d" filename
Thank you very much. I'm glad the solution was so elegant and I didn't have to resort to a ghastly mount/directory interpreter.
 
Old 06-28-2008, 01:50 AM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
You can also use df:

df myfile
 
Old 06-28-2008, 08:45 PM   #5
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
I may be misundersanding your problem, sorry if so, but the mv command will rename a file, including moving it from one directory to another, for directories on the same partition/filesystem, and will make a copy if the move is to a different filesystem. It does some error checking and will not erase the original if the copy is unsuccessful. It also has some backup options you may find useful.
 
Old 06-28-2008, 08:55 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
The mv command indeed is atomic this way, but it is important to note that neither mv nor the OPs size comparison guarantees identical copies. If fidelity is critical, then perform checksums on the files and compare those as well. Unfortunately, mv makes this impossible, as once the copy (to a new file system) is complete, the original is unlinked.

So, for critical data, where the is the possibility of error, a copy/checksum/unlink operation is best.
 
  


Reply

Tags
bash, device, file, resolve, symlink


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
bash script help - finding last n log files in all sub folders jeepescu Programming 4 11-03-2007 08:57 PM
Finding files and then finding content within those files... Maeltor Linux - Software 5 03-13-2007 01:06 PM
Finding files by contents in BASH Last Attacker Linux - General 8 01-05-2007 11:44 AM
What is this Device, help finding it mussussu Linux - Hardware 1 12-20-2005 06:53 PM
finding a device Oakstaff Slackware 1 02-15-2005 07:38 AM


All times are GMT -5. The time now is 05:09 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration