[SOLVED] Linux: how to detect if an USB-drive has NTFS or FAT file system? lazarus / 32-Linux ---------------------------------------------------------------------- Hartmut · 2020-02-18 17:37 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. ---------------------------------------------------------------------- howardpc · 2020-02-18 18:08 From a console you would do something like this Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i udevadm info --name=/dev/sdf --query=property |  grep ID_FS_TYPEand a response of Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ID_FS_TYPE=vfat would let you know it was FAT. ---------------------------------------------------------------------- bylaardt · 2020-02-18 18:53 my version of console output: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i lsblk -n -o FSTYPE /dev/sdb5 ---------------------------------------------------------------------- MarkMLl · 2020-02-18 20:10 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 ---------------------------------------------------------------------- winni · 2020-02-18 20:47 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 · 2020-02-19 18:30 Thanks a lot to all for your answers. Quote from: howardpc on February 18, 2020, 07:08:20 pmFrom a console you would do something like this Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i udevadm info --name=/dev/sdf --query=property |  grep ID_FS_TYPEI 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 Quote from: bylaardt on February 18, 2020, 07:53:02 pmmy version of console output: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i lsblk -n -o FSTYPE /dev/sdb5After 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 · 2020-02-19 18:52 Use your Linux software kit: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i mount | grep MyKnownFolder Winni ---------------------------------------------------------------------- lucamar · 2020-02-19 18:54 Quote from: Hartmut on February 19, 2020, 07:30:59 pmHow 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 ---------------------------------------------------------------------- howardpc · 2020-02-19 20:29 Which means that Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i mount | grep vfatis another solution for you. This gives both the /dev/sd?? and volume label of any FAT-formatted usb drives. ---------------------------------------------------------------------- lucamar · 2020-02-19 20:58 Quote from: howardpc on February 19, 2020, 09:29:35 pmWhich means that Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i mount | grep vfatis 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. ---------------------------------------------------------------------- winni · 2020-02-19 22:08 Hi! Another solution that  echos the correct filesystem is Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i lsblk -fs It echos disk, partition number, filesystem type,  uuid, available bytes and mount point Winni ---------------------------------------------------------------------- Hartmut · 2020-02-20 13:01 I learned that I can find the "mount point" of a known mount folder by parsing the output of 'mount'. Quote from: howardpc on February 19, 2020, 09:29:35 pmWhich means that Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i mount | grep vfatis 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,rela time,user_id=0,group_id=0,default_permissions,allow_other,blksize=4096 ,uhelper=udisks2) Quote from: winni on February 19, 2020, 11:08:13 pmAnother solution that  echos the correct filesystem is Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i lsblk -fsIt 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 · 2020-02-20 14:06 Quote from: Hartmut on February 20, 2020, 02:01:26 pmQuote from: winni on February 19, 2020, 11:08:13 pmAnother solution that  echos the correct filesystem is Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i lsblk -fsIt 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i lucamar@selene:~$ lsblk --fs NAME   FSTYPE LABEL MOUNTPOINTsda               ├─sda1              /media/Selene-Win├─sda2              /├─sda3              /media/Backs├─sda4              └─sda5            [SWAP]sr0                 sdb                 ├─sdb1              /media/XtraWin└─sdb2              /media/XtraLin ---------------------------------------------------------------------- Hartmut · 2020-02-20 15:04 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 · 2020-02-20 15:12 Quote from: Hartmut on February 20, 2020, 04:04:50 pmFor 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i lucamar@selene:~$ lsblk -f -d -r -o NAME,FSTYPE,MOUNTPOINTNAME FSTYPE MOUNTPOINTsda  sr0  sdb Not very informative, is it? ---------------------------------------------------------------------- Hartmut · 2020-02-20 16:18 Hmm. When I use "lsblk -f ..." instead of "lsblk -fs ..." then I also get no filesystems info. Why did you change "-fs" into "-f" ? Alternatively I could use "lsblk --fs -p -r -o NAME,FSTYPE,MOUNTPOINT". Both variants show me (some lines deleted): Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i NAME FSTYPE MOUNTPOINT/dev/sda1 ntfs /dev/sda2 vfat /media/D/dev/sda9 vfat /media/X/dev/sdd1 ntfs /media/hg6/Verbatim_HD ---------------------------------------------------------------------- lucamar · 2020-02-20 17:36 Quote from: Hartmut on February 20, 2020, 05:18:51 pmHmm. When I use "lsblk -f ..." instead of "lsblk -fs ..." then I also get no filesystems info. Why did you change "-fs" into "-f" ? Because my lsblk didn't like the single dash "-fs" QuoteAlternatively I could use "lsblk --fs -p -r -o NAME,FSTYPE,MOUNTPOINT". Both variants show me (some lines deleted): Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  ... Yes, my more modern boxes (xxbuntu 16 plus) show that info too but this one (with 12.04) doesn't. Quite logical: it's quite old (both the machine and the OS version). ---------------------------------------------------------------------- winni · 2020-02-20 18:39 Hi! Suse  Tumbleweed shows this Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i NAME FSTYPE MOUNTPOINT/dev/sda  /dev/sda1  /dev/sda2 ext4 //dev/sda3 swap [SWAP]/dev/sdb  /dev/sdb1 ntfs /Windows/dev/sdb2 ntfs /Data/dev/sdb3 ntfs /MMedia/dev/sdc  /dev/sdc1 ext4 /dev/sr0 iso9660 Everything is fine. Works also with -f or -fs It is : lsblk --version lsblk from util-linux 2.34 Winni