Is it possible to run
ls on a device without mounting it first?
Something like this:
# ls /dev/sda1
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
You can using the
debugfs program of e2fsprogs. Despite its historical name, it will work on ext2/3/4 filesystems. The usage is simple:
# debugfs -R "ls -l" /dev/sda6 2 40755 (2) 1001 1001 4096 17-Sep-2013 04:03 . 2 40755 (2) 1001 1001 4096 17-Sep-2013 04:03 .. 16 100644 (1) 1001 1001 9085 17-Sep-2013 04:03 avserver.conf 17 100644 (1) 1001 1001 2177 17-Sep-2013 04:03 bash.bashrc 26 100644 (1) 1001 1001 722 17-Sep-2013 04:03 crontab …
where "ls -l" is a
debugfs specific command that acts mostly like
ls -l; you can't use any arbitrary shell command there. By default, debugfs opens a drive in read-only mode so this is relatively safe; for example trying this on a non ext2/3/4 partition just gives a diagnostic:
# debugfs -R "ls -l" /dev/sda2 /dev/sda2: Bad magic number in super-block while opening filesystem ls: Filesystem not open
I can't say I consider this recommendable practice, but it will do what you ask.
The whole point of mounting a filesystem is to access its files. So no, in general, you can't access the files of a filesystem without mounting it.
For an ext2/ext3/ext4, rather than invoke the dangerous and hard to use program
debugfs, you can list its last mount location:
tune2fs -l /dev/sda1 | grep 'Last mounted'
However this is only useful if the last mount location has been recorded.
Most filesystem types support giving the filesystems labels (e.g.
tune2fs -L foo). So give all your filesystems a unique label. Then mount the filesystem by label:
mount /dev/disks/by-label/foo /media/foo
mount LABEL=foo /media/foo
Simple answer: you cannot.
ls uses standard libc routines which translate into system calls that are served by the file system driver, hence it's not possible to use
ls (or anything using those system calls for that matter) without mounting the file system.
You could write a utility that would circumvent this by having its own copy of some parts of the file system driver (such thing exists e.g. for reading Ext2 file systems from Windows), but that is quite impractical.
If your problem is just identifying the partitions, you probably should start using GPT which has space for partition names (72 bytes per partition). Some overlying solutions (Linux MD or LVM for example) allow naming as well.
As earlier described, debugfs works well.
debugfs -R "ls -l" /dev/sda1
For NTFS, there's ntfsls from ntfs-3g.
For FAT, there's mdir, from mtools.
mdir -i /dev/sdc1
For exFAT I'm not sure though. Seems exfat-utils does not include any tools similar to the ones described above. There's dumpexfat though, but I don't think it'll provide the information asked for.
To find out more details about attached drives, there's lsblk.
Personally I prefer this, and have set an alias for it:
alias lsblk="lsblk -o MODEL,TRAN,NAME,FSTYPE,LABEL,MOUNTPOINT,SIZE,MAJ:MIN"