ls on device without mounting it

6,998

Solution 1

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.

Solution 2

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

Solution 3

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"

Solution 4

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.

Share:
6,998
lampoon
Author by

lampoon

Updated on September 18, 2022

Comments

  • lampoon
    lampoon over 1 year

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

    # ls /dev/sda1
    
    • dawud
      dawud over 10 years
      On what purpose?
    • user2914606
      user2914606 over 10 years
      I don't think so. You'd definitely need a tool besides ls.
    • lampoon
      lampoon over 10 years
      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
    • Abbafei
      Abbafei over 5 years
      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.