≡ Menu

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:

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

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:



{ 6 comments… add one }
  • ravinder singh February 7, 2012, 3:22 pm

    good site

  • Root October 4, 2012, 9:48 pm

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

  • Sam January 6, 2013, 2:39 pm

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

    df -T /path |tail -n 1 |awk '{print $1}'
  • xyz0k October 28, 2013, 5:33 pm

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

  • Hans March 15, 2016, 1:14 am

    Thanks alot, very helpful.

  • suresh January 22, 2017, 3:20 pm

    nice example thanks

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: , , , , , , ,