Category filter

Script to Find File/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 a custom script 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 size

For example:
du –h document.txt

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 command 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