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:
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 3 comments… read them below or add one }
good site
How do you change a partiton path? For example /dev/sda3 to /dev/sda4.
Not every partition is in /dev (eg. encrypted directories).
You’d better use:
df -T /path |tail -n 1 |awk '{print $1}'