Recent

Author Topic: [SOLVED] Linux: how to detect if an USB-drive has NTFS or FAT file system?  (Read 6588 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 742
I want to write files to an external USB-drive. If this has FAT file system, I must split files in portions of 4 GB. Therefore I want to detect the file system (NTFS or FAT) of the external USB-drive.

I only know, in which folder it is mounted, e.g. /media/hg6/TOSHIBA EXT/
The solution needs only to work on Linux (Ubuntu 18.04).
I would prefer FPC 3.0.4, but using 3.2.0 would be possible.

Thanks in advance.
« Last Edit: February 20, 2020, 02:02:03 pm by Hartmut »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #1 on: February 18, 2020, 07:08:20 pm »
From a console you would do something like this
Code: Text  [Select][+][-]
  1. udevadm info --name=/dev/sdf --query=property |  grep ID_FS_TYPE
and a response of
Code: Bash  [Select][+][-]
  1. ID_FS_TYPE=vfat
would let you know it was FAT.

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #2 on: February 18, 2020, 07:53:02 pm »
my version of console output:
Code: Bash  [Select][+][-]
  1. lsblk -n -o FSTYPE /dev/sdb5

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #3 on: February 18, 2020, 09:10:25 pm »
I'd have expected there to have been something accessible in either the /proc or /sys trees giving this information, but a cursory search doesn't turn up anything relevant. I'd suggest though that looking at the lsblk source might be worthwhile, since being able to read a bit of text is probably preferable to having to run a program.

However I'd caution against running grep on either /sys or /proc. The special filesystems they represent don't quite behave like normal ones, and you never know when something which /should/ be harmless will result in your having to reboot your system (guess how I've just spent the last few minutes?).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #4 on: February 18, 2020, 09:47:47 pm »
Hi!

grep crashes often with pseudo files in /proc or /sys  .
Or it hangs.

With Pascal:

Don't uses streams or loadFromFile: They rely sometimes on the filesize and that is mostly zero for the pseudo files. So it does not work.

The good old examples from the 80s work:

Read every single line of a textfile into a string - and then do whatever you like.
For example : add it to a StringList.

Never try to write to the virtual filesysestems unless you know exactly what you do.

Winni

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #5 on: February 19, 2020, 07:30:59 pm »
Thanks a lot to all for your answers.

From a console you would do something like this
Code: Text  [Select][+][-]
  1. udevadm info --name=/dev/sdf --query=property |  grep ID_FS_TYPE
I tried it, but I faced 2 problems:
 - you must specify the correct "--name=/dev/sd?", but my program knows only, in which folder the USB-drive is mounted, e.g. /media/hg6/TOSHIBA EXT/. I'm a beginner to Linux. How can my program find the corresponding "--name=/dev/sd?" for a known mounted folder?
 - I searched for the correct "--name=" manually, which was "--name=/dev/sdd", but the output did not contain something similar to "ID_FS_TYPE". I attached the output of a 500 GB FAT32 USB-drive. I'm using Ubuntu 18.04
 
my version of console output:
Code: Bash  [Select][+][-]
  1. lsblk -n -o FSTYPE /dev/sdb5
After finding out the correct "/dev/sdd" it worked perfectly.
But I face the same problem as above: How can my program find the corresponding "/dev/sd?" for a known mounted folder?

Thanks for your help.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #6 on: February 19, 2020, 07:52:18 pm »
Use your Linux software kit:

Code: Bash  [Select][+][-]
  1. mount | grep MyKnownFolder

Winni

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #7 on: February 19, 2020, 07:54:32 pm »
How can my program find the corresponding "/dev/sd?" for a known mounted folder?

Parse the output of mount; look for lines starting with "/dev/sd", the next field is the mount point. With that you can get which mount point corresponds to some disk/partition and viceversa.

ETA: Yeah, basically what Winni said :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #8 on: February 19, 2020, 09:29:35 pm »
Which means that
Code: Text  [Select][+][-]
  1. mount | grep vfat
is another solution for you.
This gives both the /dev/sd?? and volume label of any FAT-formatted usb drives.


lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #9 on: February 19, 2020, 09:58:26 pm »
Which means that
Code: Text  [Select][+][-]
  1. mount | grep vfat
is another solution for you.
This gives both the /dev/sd?? and volume label of any FAT-formatted usb drives.

The problem with that is that it won't work with fuse-mounted partitions: they give fuseblk as filesystem type. For example, clicking the icon for an NTFS partition in Nautilus i this box gives this mount result:
Code: [Select]
/dev/sda1 on /media/Selene type fuseblk (rw,nosuid, ...)
As you see it's good to get the mount point from the device and viceversa, but little else in this context. However, doing the same for a USB stick gives this:
Code: [Select]
/dev/sdb1 on /media/UUI type vfat (rw,nosuid, ... ,uhelper=udisks)so in this case yes, it works.
« Last Edit: February 19, 2020, 10:01:53 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #10 on: February 19, 2020, 11:08:13 pm »
Hi!

Another solution that  echos the correct filesystem is

Code: Bash  [Select][+][-]
  1. lsblk -fs

It echos disk, partition number, filesystem type,  uuid, available bytes and mount point

Winni
« Last Edit: February 19, 2020, 11:10:50 pm by winni »

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #11 on: February 20, 2020, 02:01:26 pm »
I learned that I can find the "mount point" of a known mount folder by parsing the output of 'mount'.

Which means that
Code: Text  [Select][+][-]
  1. mount | grep vfat
is another solution for you.
This gives both the /dev/sd?? and volume label of any FAT-formatted usb drives.
As lucamar said, this unfortunately doesn't work for some of my USB-drives, which show e.g.:
/dev/sdd1 on /media/hg6/Verbatim_HD type fuseblk (rw,nosuid,nodev,relatime,user_id=0,group_id=0,default_permissions,allow_other,blksize=4096,uhelper=udisks2)

Another solution that  echos the correct filesystem is
Code: Bash  [Select][+][-]
  1. lsblk -fs
It echos disk, partition number, filesystem type,  uuid, available bytes and mount point
On my Ubuntu 18.04 'available bytes' is not showed (with option -o +SIZE I can see the total amount of bytes), but that's no problem, I don't need it.

For me this is the best solution, because I need to excecute and parse only 1 command instead of 2 commands.

Thanks a lot to all for your help!

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #12 on: February 20, 2020, 03:06:02 pm »
Another solution that  echos the correct filesystem is
Code: Bash  [Select][+][-]
  1. lsblk -fs
It echos disk, partition number, filesystem type,  uuid, available bytes and mount point
On my Ubuntu 18.04 'available bytes' is not showed (with option -o +SIZE I can see the total amount of bytes), but that's no problem, I don't need it.

For me this is the best solution, because I need to excecute and parse only 1 command instead of 2 commands.

Note that some (older) versions and in some ystems lsblk recognizes the option (as -f or --fs) but it does nothing, i.e. it doesn't show the filesystems. In this machine (with Ubuntu 12.04), for example, it gives this:
Code: Bash  [Select][+][-]
  1. lucamar@selene:~$ lsblk --fs
  2. NAME   FSTYPE LABEL MOUNTPOINT
  3. sda                
  4. ├─sda1              /media/Selene-Win
  5. ├─sda2              /
  6. ├─sda3              /media/Backs
  7. ├─sda4              
  8. └─sda5              [SWAP]
  9. sr0                
  10. sdb                
  11. ├─sdb1              /media/XtraWin
  12. └─sdb2              /media/XtraLin
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: [SOLVED] Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #13 on: February 20, 2020, 04:04:50 pm »
Thanks lucamar for that info. On my Ubuntu 18.04 "lsblk --fs" shows the filesystems. For my program I optimized the call to "lsblk -fs -d -p -r -o NAME,FSTYPE,MOUNTPOINT".

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [SOLVED] Linux: how to detect if an USB-drive has NTFS or FAT file system?
« Reply #14 on: February 20, 2020, 04:12:03 pm »
For my program I optimized the call to "lsblk -fs -d -p -r -o NAME,FSTYPE,MOUNTPOINT".

In this system that command (adapted) gives:
Code: Bash  [Select][+][-]
  1. lucamar@selene:~$ lsblk -f -d -r -o NAME,FSTYPE,MOUNTPOINT
  2. NAME FSTYPE MOUNTPOINT
  3. sda  
  4. sr0  
  5. sdb  

Not very informative, is it? :D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018