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.
[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.
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 12:03 AM.
Reason: Problem was solved
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.