LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > slac-in-the-box
User Name
Password

Notices


Rate this Entry

Interacting with Thumbdrive using Terminal Console

Posted 02-10-2023 at 03:53 PM by slac-in-the-box

Howdy LQ. I responded to a thread about thumbdrive on linux, and provided a gentle introduction to interacting with a thumbdrive using the terminal console.

The Original Poster provided a screenshot of the tail end of the output of the dmesg command (right after inserting her thumbdrive into her linux computer), and I reproduce that screenshot below:



I am also reproducing my response here, in case it helps others, because thread responses can get buried over time, and because someone suggested I do so (thanks @hazel):

Awesome! That screenshot says it all: your thumbdrive is being named sdb (scsi disk b)--scsi is old (well-established) protocol for communiating with serial devices--used to have real wide ribbons to connect to old scanners and the like, and even though your thumbdrive is not one of these old scsi (pronounced "scuzzi" with a short u like cup) devices, many storage devices still use this well established protocol and so get named sda, sdb, sdc, and so on, and referenced in the filesystem in the /dev directory. So to reference that fist partition on your thumb drive when describing it to commands on at the terminal's prompt, you would type:
Code:
/dev/sdb1
That screenshot also informed us that the filesystem on the first partition of your thumbdrive is a FAT filesystem (my mind likes having words to say for the all the letters in techno acronyms, so I see File Allocation Table when I see FAT, even though I am no file system expert and could not tell you how a file allocation table works: knowing the words for the letters, when possible, make it all more familiar somehow, as the combination of letters are nolonger arbitrary sequences, but mere abreviations.

This file system is common on thumbdrives because it is recognized by most operating systems without the user having to install additonal packages, and when cleanly unmounted, it mounts read/write on those operating systems. Your screenshot also reports that the FAT file system on your thumbdrive was not cleanly unmounted. (This means that you pulled it out of the usb slot on your computer without first performing the unmount operation on it).

In the graphical clicky file managers, there is the "right click" of the mouse menu that brings up "unmount" on removable media, and so best to right click and select unmount before taking them out of the usb slots. Or, if you are at the terminal prompt, now that we know the name of your device and partiton, we can tell it to unmount it just like chatting to an old pal:
Code:
sudo umount /dev/sdb1
.

After stating that the FAT file system had not been cleanly unmounted, the output in your screenshot suggests running fsck (file system check), and that until you do, the file system will only be mounted in "read-only" mode.

File System Check can do more than check: it repairs. There are more than one way to access it. The way described by rokytnji (running Gparted) will work and Gparted knows how to run fsck for you.

But another way is manually, at the terminal prompt. File System Check has different versions for different file systems, and linux likes to put a V for virtual in front of FAT: virtual file allocation table, and so vfat is how that file system is named in linux, File System Check for Vfat is typed like
Code:
fsck.vfat
. When typing commands to this terminal prompt, sometimes the commands are so powerful, that they need additional instructions on how to be used, and these instructions are usually given in the form of a dash followed by a letter, usually selected to be related to the instructions to be easy to remember, such as "p" for print, or "v" for verbose, or in this example, "a" for automatic. So to tell fsck.vfat to automatically repair the file system, we have to type a "-a" in the command; and to tell it to report what its doing to us (warning might look like gobbldegook, but at least you know its doing something) we give it the verbose flag, "-v", like this:
Code:
sudo fsck.vfat -a -v /dev/sdb1
The file system check and repair only works with the file system already unmounted. After it finishes repairing you can mount the thumbdrive, and it will mount properly with you able to read and write to the file system. To mount the thumbdrive, you can do it automatically by merely removing it and re-inserting it, in which case, your desktop environment will recognize it and automatically mount it for you, but you need to right click and unmount before removing it, or this could happen again.

The mount thing is a strange word to use for what it does: tells linux where in its tree of directories and files to look for the directories and files on your storage device. It uses an empty directory, that it calls a mount point. After mounting a device to a mount point, the directory is no longer empty but filled with the contents of the device. After unmounting a device, the directory is empty again. So when you rip the thumb drive out without unmounting, it thinks all those files are still attached to that directory, which confuses it the next time you plug it in.

All the storage devices connect together with mounts. The one that the operating system starts up from is called the root file system, and it gets mounted at just a "/" for a description of where it is in the tree. Then the first directories in that tree organize linux for us, and it is very common to keep some blank directories to use as mountpoints in both /media, and /mnt. Often ones home folder might be on its own storage device, or partition, and have a separate mount point. All of how and where your computers storage devices get connected to the root mountpoint is described in a file, called fstab, for file system table, and located at /etc/fstab. When you pull your thumb-drive out and replug it in, your desktop envirnoment will select one of the blank mountpoints and mount it for you. But for manual control, you can tell it which blank folder to mount your storage device to.

So instead of removal and re-insertion to trigger your computer to automatically mount your thumbdrive, you could, from that terminal prompt, use the mount command:
Code:
sudo mount /dev/sdb1 /mnt/tmp
After that the contents of your storage device are inside /mnt/tmp. You can use "cd" to change directory to /mnt/tmp and then view what is stored there with "ls" for list. Hopefully, it won't look like the gobbldegook of your second screenshot!

So to put everything together; after sticking in the corrupted sticK:

Code:
umount /dev/sdb1
fsck.vfat -a -v /dev/sdb1
mount /dev/sdb1 /mnt/tmp
cd /mnt/tmp
ls
When I read all of it together, it makes enough sense to me, that it's not too much to remember, and I prefer interacting with my devices at this terminal prompt over remembering when to right click, and which graphical utility application does what I want; the looks and feel to those graphical utilities change from one release to the next, whereas that sequence of steps at the terminal prompt remains constant and nearly immutable, though occasionally the syntax of these commands can change, and occasionally a newer command comes out that does the same thing, but better, so everyone switches... but this is much slower and seems like there's less flux than with the graphical utilities, that I found it rewarding to find out what's going on at that command prompt.
Posted in helpful tech
Views 357 Comments 2
« Prev     Main     Next »
Total Comments 2

Comments

  1. Old Comment
    Nice answer compared to my short answer using gparted. Kudos bro.

    I just supplied easy peasy because the OP seemed to need something like that.

    Again. Very good writeup.
    Posted 02-10-2023 at 05:13 PM by rokytnji rokytnji is offline
  2. Old Comment
    Very good writeup indeed.
    Thanks for sharing this.
    Posted 05-30-2023 at 01:00 AM by ychaouche ychaouche is offline
 

  



All times are GMT -5. The time now is 04:08 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration