# 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](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) 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).

Script to list files greater than certain size

\#!/bin/bash CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }') CurrentUserUID=$(id -u "$CurrentUser") launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" find 'path to folder' -type f -size +'minimum size limit'k -print 

   1

2

3

4

  \#!/bin/bash 

CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')

CurrentUserUID=$(id -u "$CurrentUser")

launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" find 'path to folder' -type f -size +'minimum size limit'k -print 

   

 

  

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

Script to list files greater than 20 kb

\#!/bin/bash CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }') CurrentUserUID=$(id -u "$CurrentUser") launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" find 'Users/admin/Desktop/My Documents' -type f -size +'20'k -print

   1

2

3

4

  \#!/bin/bash 

CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')

CurrentUserUID=$(id -u "$CurrentUser")

launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" find 'Users/admin/Desktop/My Documents' -type f -size +'20'k -print

   

 

  

[![Files present on a Mac device that are larger than 20 kb](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/07/Script-to-list-files-larger-than-certain-size-on-Mac.png "Script to list files larger than certain size on Mac")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/07/Script-to-list-files-larger-than-certain-size-on-Mac.png)Delete files larger than certain size
-------------------------------------

Script to delete files greater than certain size

\#!/bin/bash CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }') CurrentUserUID=$(id -u "$CurrentUser") launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" find 'path to folder' -type f -size +'minimum size limit'k -print -delete 

   1

2

3

4

  \#!/bin/bash 

CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')

CurrentUserUID=$(id -u "$CurrentUser")

launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" find 'path to folder' -type f -size +'minimum size limit'k -print -delete 

   

 

  

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

Script to delete files greater than 20 kb

\#!/bin/bash CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }') CurrentUserUID=$(id -u "$CurrentUser") launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" find 'Users/admin/Desktop/My Documents' -type f -size +'20'k -print -delete 

   1

2

3

4

  \#!/bin/bash 

CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')

CurrentUserUID=$(id -u "$CurrentUser")

launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" find 'Users/admin/Desktop/My Documents' -type f -size +'20'k -print -delete 

   

 

  

 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.