Help!

How to get filesystem in bash script


Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> UK RSS
Next:  Which PCI ADSL modem card?  
Author Message
ajp
External


Since: Jan 11, 2007
Posts: 11



PostPosted: Sun Jul 01, 2007 1:16 pm    Post subject: How to get filesystem in bash script
Archived from groups: uk>comp>os>linux (more info?)

Hi,

Is there some way of extracting the filesystem id/name given a file name in
bash? I want to test whether 2 files are in the same filesystem.

I thought I could use `stat -fc "%i" $filename` but it always responds with
0

Alan
--
email =~ s/nospam/fudokai/
Back to top
Gordon Henderson
External


Since: Jun 10, 2007
Posts: 15



PostPosted: Sun Jul 01, 2007 1:16 pm    Post subject: Re: How to get filesystem in bash script [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <46879b1b$0$8737$ed2619ec@ptn-nntp-reader02.plus.net>,
ajp <ajp.DeleteThis@nospam.org.uk> wrote:
>Hi,
>
>Is there some way of extracting the filesystem id/name given a file name in
>bash? I want to test whether 2 files are in the same filesystem.
>
>I thought I could use `stat -fc "%i" $filename` but it always responds with
>0

You can use df, but you'll then need to parse the output somewhat. Eg:

gordon @ yakko: pwd
/usr/src/linux
gordon @ yakko: df Makefile
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda4 75340676 65492216 6021376 92% /var

gordon @ yakko: df /etc/logrotate.conf
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda1 482248 210237 247111 46% /

This might give you what you want, but I'm sure there might be easier
ways to do it:

df /etc/logrotate.conf | fgrep /dev/ | awk '{printf $1;}'

Another way might be the -i option to ls to give the files inode
number. If it's the same file on the same filing system it will have
the same inode number, but there is a small possiblity that it might
just have the same inode number on a different filing system..

Gordon
Back to top
Paul Martin
External


Since: Sep 21, 2004
Posts: 173



PostPosted: Sun Jul 01, 2007 2:02 pm    Post subject: Re: How to get filesystem in bash script [Login to view extended thread Info.]
Imported from groups: per prev. post (more info?)

This message is not archived
Back to top
Simon Waters
External


Since: Aug 21, 2005
Posts: 194



PostPosted: Sun Jul 01, 2007 2:27 pm    Post subject: Re: How to get filesystem in bash script [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Sun, 01 Jul 2007 14:02:52 +0000, Paul Martin wrote:

> In article <46879b1b$0$8737$ed2619ec@ptn-nntp-reader02.plus.net>,
> ajp wrote:
>> Hi,
>
>> Is there some way of extracting the filesystem id/name given a file
>> name in bash? I want to test whether 2 files are in the same
>> filesystem.
>
>> I thought I could use `stat -fc "%i" $filename` but it always responds
>> with 0
>
> if [ `stat -c "%D" $filename1` = `stat -c "%D" $filename2` ] then
> echo Same filesystem
> else
> echo Different filesystem
> fi

Out of curiosity what is "%l" ? Since it sounds like it ought to be
useful here as well, but isn't.
Back to top
Paul Martin
External


Since: Sep 21, 2004
Posts: 173



PostPosted: Sun Jul 01, 2007 2:50 pm    Post subject: Re: How to get filesystem in bash script [Login to view extended thread Info.]
Imported from groups: per prev. post (more info?)

This message is not archived
Back to top
ajp
External


Since: Jan 11, 2007
Posts: 11



PostPosted: Sun Jul 01, 2007 4:25 pm    Post subject: Re: How to get filesystem in bash script [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Paul Martin wrote:

> In article <46879b1b$0$8737$ed2619ec@ptn-nntp-reader02.plus.net>,
> ajp wrote:
>> Hi,
>
>> Is there some way of extracting the filesystem id/name given a file name
>> in bash? I want to test whether 2 files are in the same filesystem.
>
>> I thought I could use `stat -fc "%i" $filename` but it always responds
>> with 0
>
> if [ `stat -c "%D" $filename1` = `stat -c "%D" $filename2` ]
> then
> echo Same filesystem
> else
> echo Different filesystem
> fi
>
>
Thanks - that seems to do the trick Smile

We must have different man pages - what I see in man stat ...

Interpreted sequences for file stat are:
* %D - Device number in hex
Interpreted sequences for file system stat are:
* %i - File System id in hex

hence my thoughts and confusion.

Alan
--
email =~ s/nospam/fudokai/
Back to top
Nix
External


Since: Jul 29, 2004
Posts: 680



PostPosted: Sun Jul 01, 2007 7:30 pm    Post subject: Re: How to get filesystem in bash script [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 1 Jul 2007, Paul Martin uttered the following:
> %l isn't anything. "%i" is inode number, which is filesystem dependent.
> The root of many filesystems will be inode zero, hence stat -fc "%i"
> being always zero.

Um, the root of every ext*fs filesystem is inum 2...

--
`... in the sense that dragons logically follow evolution so they would
be able to wield metal.' --- Kenneth Eng's colourless green ideas sleep
furiously
Back to top
Paul Martin
External


Since: Sep 21, 2004
Posts: 173



PostPosted: Mon Jul 02, 2007 6:55 am    Post subject: Re: How to get filesystem in bash script [Login to view extended thread Info.]
Imported from groups: per prev. post (more info?)

This message is not archived
Back to top
Paul Martin
External


Since: Sep 21, 2004
Posts: 173



PostPosted: Mon Jul 02, 2007 6:55 am    Post subject: Re: How to get filesystem in bash script [Login to view extended thread Info.]
Imported from groups: per prev. post (more info?)

This message is not archived
Back to top
Nix
External


Since: Jul 29, 2004
Posts: 680



PostPosted: Mon Jul 02, 2007 7:51 pm    Post subject: Re: How to get filesystem in bash script [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2 Jul 2007, Paul Martin stated:
> Interestingly the values of, %D (file) and %i (file system) are
> identical on my system for the root file system, but not for a ramdisk.
> eg. /dev is %D = "d", %i = "0".

The root of tmpfs and ramfs filesystems is always inode number
zero. (The rootfs, of course, is one such.)

--
`... in the sense that dragons logically follow evolution so they would
be able to wield metal.' --- Kenneth Eng's colourless green ideas sleep
furiously
Back to top
Paul Martin
External


Since: Sep 21, 2004
Posts: 173



PostPosted: Mon Jul 02, 2007 10:10 pm    Post subject: Re: How to get filesystem in bash script [Login to view extended thread Info.]
Imported from groups: per prev. post (more info?)

This message is not archived
Back to top
Nix
External


Since: Jul 29, 2004
Posts: 680



PostPosted: Tue Jul 03, 2007 5:47 pm    Post subject: Re: How to get filesystem in bash script [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2 Jul 2007, Paul Martin said:
> %i has a different meaning if you're using the -f flag:
>
> %i Inode number
>
> %i File System ID in hex

WTF...

> The formatting is screwed for the latter part of the "stat" manpage, so
> you might not have noticed this.

Indeed not, so I was talking rubbish.

What bloody genius invented *that*?

--
`... in the sense that dragons logically follow evolution so they would
be able to wield metal.' --- Kenneth Eng's colourless green ideas sleep
furiously
Back to top
Display posts from previous:   
Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> UK All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum