Linux: Find Out What Partition a File Belongs To

How do I find out that /users/f/foo/file.txt file belongs to a specific partition? How do I find out on what partition a file exits?

The df command report file system disk space usage including file names and directory names. The syntax is as follows:

ADVERTISEMENTS

df
df /path/to/dir
df /path/to/file

In this example find out partition name for a file called /users/f/foo/file.txt, enter:
$ df -T /users/f/foo/file.txt
Sample outputs:

Filesystem    Type   1K-blocks      Used Available Use% Mounted on
/dev/sda5     ext4   472439072 146088944 302351616  33% /

The above command indicates that the file called “/users/f/foo/file.txt” belongs to /dev/sda5 partition. The following command only shows partition name:

df /users/f/foo/file.txt | awk '/^\/dev/ {print $1}'

OR

awk '/^\/dev/ {print $1}' <<<"$(df /users/f/foo/file.txt)"

Sample outputs:

/dev/sda5

I recommend that you place the following bash function in your ~/.bashrc file

# find partition name for a given filename
findpart() { [ -e "$1" ] && df -P "$1"  | awk '/^\/dev/ {print $1}' || echo "$1 not found"; }

Sample usage:
$ findpart /foo/bar
$ findpart /etc
$ findpart /home/vivek/test.txt

Sample session:

Linux df Command To Find Out On What Partition a File (Directory) Exits?

df Command To Find Out On What Partition a File (Directory) Exits

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
6 comments… add one
  • ravinder singh Feb 7, 2012 @ 15:22

    good site

  • Root Oct 4, 2012 @ 21:48

    How do you change a partiton path? For example /dev/sda3 to /dev/sda4.

  • Sam Jan 6, 2013 @ 14:39

    Not every partition is in /dev (eg. encrypted directories).
    You’d better use:

    df -T /path |tail -n 1 |awk '{print $1}'
  • xyz0k Oct 28, 2013 @ 17:33

    That was an useful tip. Thank you!! :)

  • Hans Mar 15, 2016 @ 1:14

    Thanks alot, very helpful.

  • suresh Jan 22, 2017 @ 15:20

    nice example thanks

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.