Category filter

Script to find file or folder size on Mac

Have you ever wondered how big a specific file or folder on a Mac is? You can view the file or folder size using the Get Info option by right-clicking on the required file or folder on a Mac. However, manually checking the file or folder size on multiple endpoints may be tiresome for the admins. Now, IT admins can remotely fetch device details like file or folder size by running the Execute Custom Script action on the endpoint devices using Hexnode UEM. This doc includes the Terminal commands to retrieve the file or folder size on macOS devices.

Scripting language – Bash

File extension – .sh

Disclaimer:


The sample scripts provided below are adapted from third-party open-source sites.

Find file/folder size

The script requires the complete file path, including the filename with its extension, to be provided.

The above command provides the disc usage for the file ‘Document.txt’, as shown below:

Output of the script to display the disc usage of the file

The du command is a standard command that allows users to gain disk usage information quickly. Some of the options supported with the command are

-h

  • flag prints size outputs in Byte format, this provides a unit of measure.

-a

  • flag lists the sizes of all files and directories in the given file path. For example, du –a ~/Downloads/Wallpapers

-c

  • flag adds a line to the bottom of the output that displays the grand total of all the disk usage for the file or folder path given. For example, du –c ~/Downloads
Notes:

  • In Bash, before inserting space while defining file or folder names, we use a backslash ‘\’ to separate the characters. This will prevent the shell interpreter from interpreting the space as a separator and assuming they were two different arguments. Hence, if the path is New Folder/myFile, we write it as New\ Folder/myFile or use single quotes as ‘New Folder/myfile’ instead.
  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository