How to find out mount/partition a directory or file is on? (Linux Server)
df -P file/goes/here | tail -1 | cut -d' ' -f 1
Check if directory mounted with bash
Running the mount
command without arguments will tell you the current mounts. From a shell script, you can check for the mount point with grep
and an if-statement:
if mount | grep /mnt/md0 > /dev/null; then
echo "yay"
else
echo "nay"
fi
In my example, the if-statement is checking the exit code of grep
, which indicates if there was a match. Since I don't want the output to be displayed when there is a match, I'm redirecting it to /dev/null
.
How do you locate which files are on which partition with linux ubuntu
/dev/sda7 4.6G 4.4G 0 100% /var
varrun 1.8G 92K 1.8G 1% /var/run
varlock 1.8G 0 1.8G 0% /var/lock
I re-arranged your df -h
output a little and trimmed it to the most meaningful lines.
You need to remove content in /var/
that is not in /var/run
or /var/lock
. A very fast way to free up a giant pile of free space on Debian-derived systems (including Ubuntu) is to run apt-get autoclean
-- this will remove old packages from /var/cache/apt/archives/
. apt-get clean
will free up even more space by removing all packages from that directory. (These packages are kept around for your troubleshooting.) If you're not sure which to run, apt-get clean
is my suggestion -- you'll almost never need those packages anyway.
But that's not a long-term solution to your problem. You should probably store your SQL databases in /home
instead. You have 202 gigabytes free there and you probably have a backup solution of some sort in place on your /home
partition -- right? -- that you might not have thought to back up from /var/
. Make a new directory in /home/
for your SQL databases, make it owned by the user and group accounts for your SQL server, move your databases, and configure the database server to use the new locations.
Where is a file mounted?
This is what I've come up with. It turns out there's usually no need to iterate through the parent directories. All you have to do is get the file's device number and then find the corresponding mount entry with the same device number.
struct mntent *mountpoint(char *filename, struct mntent *mnt, char *buf, size_t buflen)
{
struct stat s;
FILE * fp;
dev_t dev;
if (stat(filename, &s) != 0) {
return NULL;
}
dev = s.st_dev;
if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
return NULL;
}
while (getmntent_r(fp, mnt, buf, buflen)) {
if (stat(mnt->mnt_dir, &s) != 0) {
continue;
}
if (s.st_dev == dev) {
endmntent(fp);
return mnt;
}
}
endmntent(fp);
// Should never reach here.
errno = EINVAL;
return NULL;
}
Thanks to @RichardPennington for the heads up on realpath()
, and on comparing device numbers instead of inode numbers.
What is the purpose of mounting a partition and giving it a directory (i.e. mount point)?
A filesystem is really just a big array of bytes stored (typically) in a partition. Mounting is how you get access to the files within it.
Every filesystem has its own root directory. In Windows, you have drive letters (like C:
) that refer to the root directories of different filesystems, but Unix and Linux use a different approach. There's a single "virtual" directory hierarchy, but any directory can be used as a mountpoint for the root of another filesystem.
So when you mount your new filesystem on /mnt/lfs
, it makes /mnt/lfs
be an alias for that filesystem's root directory — think of it as a sort of fancy drive letter. As you follow the LFS instructions, you'll be creating subdirectories like bin
and etc
in there, and they're actually being placed under the root of the filesystem you created. Later, when you boot your finished LFS system, that same filesystem will be mounted as the root filesystem (/
), so its contents will appear as /bin
, /etc
, and so on.
There's nothing special about the path /mnt/lfs
. You could've called it /mnt/foo
or /foo/bar
or whatever. All that really matters is that you have a path that refers to the root of your newly-created filesystem so that you can start copying things into it.
Unix find mount point permission when filesystem mounted
There is no way to examine the mount point once a filesystem is mounted there. The inode is remapped deep within the kernel and the original attributes are not needed anymore.
The only hack around this (that I can think of) is to open the raw volume and parse the volume for the directory. It might be possible to mount the containing volume again (at a different mount point) and then examine the directory. However, either it won't be possible to doubly mount the volume, or doing so would get wires crossed with the already mounted instance and the mount point would again be hidden. A few simple experiments would determine the feasibility of this very hair-brained scheme.
Your language is not clear: "When filesystem mounted". Of course, before the mount occurs, it is trivial to determine the mount point permission. During the mount makes no sense. After the mount is explained above.
Shell script to know whether a filesystem is already mounted
You can check the type of the filesystem.
$ stat -f -c '%T' /
xfs
$ stat -f -c '%T' /dev/shm
tmpfs
You could also check whether a directory is a mountpoint by comparing its device with its parent's.
$ stat -c '%D' /
901
$ stat -c '%D' /home
fe01
$ stat -c '%D' /home/$USER
fe01
Related Topics
Where Is Hardware Timer Interrupt
Bash Script Read All the Files in Directory
Setting Limit to Total Physical Memory Available in Linux
Best Approach to Writing a Generic Installer for a Linux Application
Bash: Add String to the End of the File Without Line Break
How to Send Data to Local Clipboard from a Remote Ssh Session
Linux Usb: Turning the Power on and Off
Using Putty to Scp from Windows to Linux
Postgresql -Bash: Psql: Command Not Found
Finding Process Count in Linux via Command Line
How to Record What Process or Kernel Activity Is Using the Disk in Gnu/Linux
Using Linux How to Pass the Contents of a File as a Parameter to an Executable
Android Studio 2.3 Using Emulator from Console, "/Dev/Kvm Device: Permission Denied" for Root User
How to Write and Execute a Hello World Program in File for R
How to Make Sure the Numpy Blas Libraries Are Available as Dynamically-Loadable Libraries
How to List All Users in a Linux Group
Randomly Pick Lines from a File Without Slurping It with Unix