I want to run a bash
script periodically to track the size of a shared drive on my network. The command that produces the data I want is du -sh
. I have created a Login item
to mount the shared drive when I login using System Preferences
. I mount the drive as follows:
cifs://raspberrypi3b.local/passport2tb/data_lib
I have tried numerous variations for specifying the folder location for du
, but none of them work properly. I get the following error message:
du: //raspberrypi3b.local/passport2tb/data_lib: No such file or directory
It seems there is no folder path specification that du
(or bash
) can find. Yet, I know the share is mounted. I can get the stats using File
, Get Info
in Finder (commandI).
Where in the file system is my shared folder?
Best Answer
The folder you are looking for is under
/Volumes
in your file system./Volumes
is sometimes called a hidden file in MacOS parlance.Using Finder with Hidden Files:
There are at least two ways to expose hidden files in Finder:
Use the Finder menu to navigate to arbitrary folders: Go, Go to Folder..., (ALTERNATIVELY: shiftcommandG), and then enter
/Volumes
in the text box. Of course this method requires that you know the name of the file/directory you're looking for, but once that's known this will get you there.If you're running macos Sierra (or later), you can view all hidden files/folders in Finder like so: shiftcommand. (shift-command-period). Repeating this sequence will hide the hidden folders again.
Note: To see
/Volumes
using the 2nd method, you must position Finder at the proper place in the file system; i.e. at theMacintosh HD
level (selected from Finder's sidebar if you have it enabled!).Once Finder has listed the hidden file/folder of interest, use the menu (File, Get Info) or shortcut (commandI) to yield the info window with lots of handy information.
At this point, your questions re. Finder and hidden files have hopefully been answered. Now let's move on to the rest of your question re. how to display usage stats from the command line:
Hidden Files from the Command Line:
Open a terminal window (
bash
is the default macos shell). Just like every (?) other *nix system in the universe, the file system root is simply known as/
. So, let's see what's in/
:Listed here are all of your files and folders, including links, and the hidden files, including
/Volumes
. If this makes you wonder about the conventional wisdom (or current practice) re. "ease of use" for Graphical User Interfaces (GUI), then you are on the right track :) But I digress...Now, take a look to verify that your mounted shares are listed under
/Volume
. In your case, assuming your raspberrypi share is the only one mounted, you'll see something similar to this:The first line is a link showing the mapping from the
Macintosh HD
to the file system root/
. The second line will show your mounted network drive, but note the permissions - you won't be able to use this one. It's apparently an artifact. I don't know why it's there other than to set theexecutable
permission bit for the folder; perhaps someone else can offer an explanation? At any rate, while we needn't be overly concerned about why it's there, automating this task will require that we identify the correct share name.Automating the process via
bash
script:The correct share name can be found as follows:
To recap:
grep
will filter the output ofls -l
to get the actual share (instead of the artifact),head -1
will filter duplicates (which may show up aspassport2tb-2
, etc) andawk '{print $9}'
will get the 9th field in the string which is the share name. If you have other shares mounted in/Volumes
, simply add anothergrep
stage to get the one(s) you're interested in.Knowing the location of your mounted share will allow you to successfully execute the
du
command to display the usage statistics of any folder in the mounted share; for example:In this case, the folder
data_lib
folder contains 75GB of data... and this may take a few minutes to tally, even on a fast network.Some command line gymnastics that might be useful:
du
, useawk
to strip it:To wrap this up in a script:
Finally: Save it, name it, make it executable, and add it to your
crontab
to be executed periodically.This answer has gotten long-winded, so I'll leave this as an exercise for the reader.