Category filter

Script to List all Files in a Folder on Mac

In macOS devices, the Finder application is used to view and configure all files and folders on the system. The Finder application provides a great graphical user interface to interact with files on the system. What if device administrators need to remotely view all files in a folder. The solution is to run custom scripts using Hexnode UEM to list all files under the required folder.

Scripting Language – Bash

File extension – .sh

Disclaimer:


The Sample Scripts provided below are adapted from third-party Open-Source sites.

View all files in a folder

For example:
ls –R /Users/QA/Desktop/Wallpapers

The ls command lists all the files that are stored inside the folder. The –R command expands out subdirectories and lists the files contained within them.

View all hidden files or folders

For example:
ls –a /Users/QA/Desktop/Wallpapers

The -a command displays all hidden files or folders in the specified folder path.

View detailed file/folder information

For example:
ls –l /Users/QA/Desktop/Wallpapers

The –l command is used to display detailed information of the files and folders present in the given folder path. Used with the –l command, the –h command displays the size of the file or folder in a human-readable format alongside other details.

For example – ls –lh /Users/QA/Desktop/Wallpapers

Sort files in a folder

For example:
ls –t /Users/QA/Desktop/Wallpapers

The -t command sorts the files by Time of Last Modification (latest first) instead of Name.

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