Category filter

Script to delete files based on size on Mac

Insufficient storage on corporate devices can hinder productivity as it may lead to performance issues for applications and processes. Among other methods to free up device space, administrators may choose to find and delete files larger than a specified size. If files are periodically backed-up to storage resources, deleting larger files from devices running low on storage can be extremely helpful. With Hexnode’s Execute Custom Script action, you can execute the following scripts to identify and remove files based on their size on macOS devices.

Scripting language – Bash

File extension – .sh

Disclaimer:


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

List files larger than certain size

You can deploy the following script to list all files present in a folder that are larger than a certain size (in kilobytes).

For example, to list all files present at ‘/Users/admin/Desktop/My Documents’ that are larger than 20 kilobytes:

Files present on a Mac device that are larger than 20 kb

Delete files larger than certain size

For example, to list and delete all files present at ‘/Users/admin/Desktop/My Documents’ that are larger than 20 kilobytes:

Notes:

  • The files that are deleted using the above script are deleted permanently and cannot be recovered from the Bin application. It is recommended to backup such files before executing this script to prevent any valuable data loss.
  • 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