29

When I use df or mount, I'm most of all interested in physical disk partitions. Nowadays the output of those commands is overwhelmed by temporary and virtual filesystems, cgroups and other things I am not interested in on a regular basis.

My physical partitions in the output always start with '/', so I tried making aliases for df and mount:

alias df1="df | egrep '^/'"
alias mount1="mount | egrep '^/'"

That works OK for mount1 (although it shows the '/' in red), but for df1 I would sometimes like to add the -h option to df and cannot do df1 -h. I would prefer not to have an alias for every option combination I might want to use. Do I really have to look into defining functions in bash (I would prefer not to)? Is there a better solution for df1?

10 Answers 10

17

You can solve the df1 argument issue by using the following alias:

alias df1='df --type btrfs --type ext4 --type ext3 --type ext2 --type vfat --type iso9660'

make sure to add any other type (xfs, fuseblk (for modern NTFS support, as @Pandya pointed out), etc) you're interested in. With that you can do df1 -h and get the expected result.

The filtering works the other way around too, exclude the FS types you don't want to see:

alias df1='df -x tmpfs -x efivarfs '

mount does have a -t option but you cannot specify it multiple times (only the last is taken), there I would use:

alias mount1="mount | /bin/grep -E '^/'"

I am using grep -E as egrep is deprecated and using /bin/grep makes sure you're not using --colour=auto from an alias for grep/egrep

4
  • df --type ntfs isn't showing 5 ntfs partition and hence skipping them in result for me
    – Pandya
    Commented Mar 4, 2016 at 9:26
  • I've to use --type fuseblk for that
    – Pandya
    Commented Mar 4, 2016 at 9:29
  • @Pandya that is what I indicated, you need to add other types you're intrested in. I haven't had ntfs partitions in my system for several years, so I'm not sure where that came from in the alias that I use.
    – Anthon
    Commented Mar 4, 2016 at 9:31
  • 1
    @Pandya looks like that --type ntfs was in my alias from the time there was minimal NTFS support (the time where you had to do a filesystem check on your NTFS partition after rebooting into Windows %-) ). Updated my answer, thanks.
    – Anthon
    Commented Mar 4, 2016 at 10:38
13

Reading man mount

Listing the mounts
The listing mode is maintained for backward compatibility only. For more robust and customizable output use findmnt(8), especially in your scripts.

showed me another way to get exactly the desired output.

I created a function lsmounts

lsmounts () {
  findmnt -D -t nosquashfs,notmpfs,nodevtmpfs
}

Explanation for the arguments:

  • -D: provide output like we know it from df
  • -t: select the types of file systems you want to see. Prepending no excludes the mentioned file systems.

So I get the desired output

SOURCE                       FSTYPE   SIZE   USED  AVAIL USE% TARGET
/dev/mapper/vgubuntu-lvroot  ext4    78,3G  42,5G  31,7G  54% /
/dev/mapper/vgubuntu-lvhome  ext4   284,5G 146,4G 124,5G  51% /home
...
1
  • 3
    This should be the top answer
    – rep_movsd
    Commented Dec 25, 2020 at 9:56
7

If switching to a different df is an option, use pydf instead, as it doesn't show filesystems having 0 blocks by default, and has a gauge, colors, and some other properties enabled by default. It works fairly well aliased to df, and the original is always available using \df anyway.

2
  • For me the most useful answer here. Sadly, this doesn't exist for mount.
    – emk2203
    Commented Nov 28, 2018 at 12:57
  • 2
    A bit late, but for me pydf showed my single physical drive and 33 /dev/loop## drives when all I want is the physical drive. The accepted df answer works, but I prefer this form instead: alias dfh='df -h -x squashfs -x tmpfs' Commented Jun 21, 2019 at 11:35
6

This answer attempts to address yves's following preoccupations:

I would prefer not to have an alias for every option combination (1)

and

although it shows the '/' in red (2)

and (3) add an important piece of info to the varying output, which is df's header line that changes according to the supplied flags.

(1) You need function, because by definition it takes arguments. And, it is very simple to define and use as you'll find out below.

(2) the red / is produced by grep --color, which is a common alias to grep itself (you can check that by running alias).

code

function df1 
{ 
    df $* | sed -n '1p;/^\//p;'
}

The newlines above are intended to improve readability, you can replace them by space.

Copy and paste this snippet directly to an interactive bash session or, even better, append it to your ~/.bashrc.

Example usage

$ df1 
Filesystem                        1K-blocks      Used Available Use% Mounted on
/dev/disk1                        487358464 316093104 171009360  65% /
/dev/disk2                           524032    302620    221412  58% /Volumes/Packer

$ df1 -hT
Filesystem                        Type   Size  Used Avail Use% Mounted on
/dev/disk1                        hfs    465G  302G  164G  65% /
/dev/disk2                        hfs    512M  296M  217M  58% /Volumes/Packer

$ df1 -hT .
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/disk1     hfs   465G  302G  164G  65% /

What it does

The function df1 calls df with whatever arguments you pass it and pipes (|) the output to sed, which is invoked with -n to suppress automatic printing of the pattern space. The rest expresses the sed "scripts", two actually:

  1. the script 1p prints the 1st line, which in our case is df's header (I use the same for ps piping),
  2. the script /^\//p matches any line starting with /, then prints it
  3. ; separates the 2 sed scripts
5

mount

Use findmnt --real instead of mount:

findmnt --real -t nosquashfs

findmnt is the recommended way of listing mounts, according to the mount manual page:

The listing mode is maintained for backward compatibility only.

For more robust and customizable output use findmnt(8), especially in your scripts.

The --real option excludes virtual filesystems (like proc, devtmpfs, or cgroup).

Furthermore, you may want to exclude squashfs filesystems (if you use snap) to remove some clutter using -t nosquashfs. You can exclude more filesystems separated by comma, e.g. -t nosquashfs,nofuse.sshfs.

df

df -x tmpfs -l

df doesn't list most virtual systems by default (unless you pass the -a option), so the main thing you'll want to exclude is tmpfs filesystems (-x tmpfs), as well as perhaps non-local ones (-l). If your df shows squashfs filesystems (mine doesn't), you may have to pass -x tmpfs -x squashfs to exclude them.

3

You can define the function as follows:

function df1() { df "$@" | grep -E '^/'; }

Example output:

$ df1 -h
/dev/sda8        25G  8.1G   16G  35% /
/dev/sda4        25G   20G  5.8G  78% /media/pandya/Documents+Edu
/dev/sda3       9.5G  7.1G  2.0G  79% /media/pandya/Ext4
/dev/sda7        24G   17G  6.9G  71% /media/pandya/Extra+Other
/dev/sda6        26G   25G  448M  99% /media/pandya/Media+Game
/dev/sda10       15G  7.9G  7.1G  53% /media/pandya/Miscellaneous
/dev/sda5        36G   22G   14G  63% /media/pandya/Software+OS

Here $@ lets you to input your arguments! [Note that $@ should always be written within double quotation marks unless you have a concrete reason not to. -ed]

0
2

For me - having only ext4 as physical discs - df -t ext4 and mount -t ext4 are producing the needed output.

For df you may use multiple -t commands, and for mount a comma separated list for example:

df -t ext4 -t tmpfs and mount -t ext4,tmpfs

1

I use alias df='df -hT -xtmpfs -xdevtmpfs'.

Pretty much sucks that you have to use an alias to make GNU df actually usable on Linux these days. There should be an option to only show "real" filesystems (whatever that means).

Acually, df -hxtmpfs takes most of the crap out and remains usable without an alias. Not really worse than netstat -tulnp and such... *sigh*

1
  • "Pretty much sucks that you have to use an alias to make GNU df actually usable"? GNU df is open source. You are welcome to download the source and improve it. Or, use the alias, but remember what's undernesth.
    – waltinator
    Commented Jul 30, 2023 at 19:35
1

Alternative di

I have been frustrated by this problem with df and found a good replacement in the Ubuntu/Debian repos: di (disk information)

apt install di

It looks and feels almost identical to df enter image description here

but can be configured with a env var DI_ARG so in my ~/.bash_profile I have the following line to show full names, human readable disk space for physical drives:

export DI_ARGS="-f SMBuv2T -H -st -xnone,tmpfs"
$ source ~/.bash_profile
$ di

enter image description here

1
  • Actually, you can just do it oneliner DI_ARGS="-f SMBuv2T -H -st -xnone,tmpfs" di
    – annahri
    Commented Jul 20, 2021 at 3:57
0

alias df='df -hT --type ext4' alias lsmount=' findmnt -D -t nosquashfs,notmpfs,nodevtmpfs,nofuse.lxcfs,nobpf,nofuse '

1
  • This does not work for VFAT, ext2, ext3, NFS, etcetera. Also, the question is over 5 years old and has already many (better) answers. Commented Apr 23, 2021 at 10:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .