9

Is it possible to run ls on a device without mounting it first?
Something like this:

# ls /dev/sda1
  • On what purpose? – dawud Sep 17 '13 at 6:59
  • I don't think so. You'd definitely need a tool besides ls. – strugee Sep 17 '13 at 7:00
  • No. Why don't you tell us what you are trying to achieve? – jasonwryan Sep 17 '13 at 7:01
  • 1
    for those times I want to mount some device but don't remember if its sda1/2/3/4/5/6 etc. Just needs to list the files on that device to give me a clue whether its the device i'm looking for – lampoon Sep 17 '13 at 7:21
  • By the way: if you have an image of a disk (not a partition), you may be able to see its files on testdisk by running the testdisk command with an image file path as its first parameter on the command line. – Abbafei Oct 21 '18 at 2:31
11

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.

3

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.

There are a few utilities that access a file directly without mounting, such as debugfs for ext2/ext3/ext4 filesystems and mtools for FAT filesystems.

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

or

mount LABEL=foo /media/foo
2

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.

1

As earlier described, debugfs works well.

debugfs -R "ls -l" /dev/sda1

For NTFS, there's ntfsls from ntfs-3g.

ntfsls /dev/sda2

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.

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.