Category filter

Script to Find File path on Mac

Files can accumulate on the device after a significant period of use, making it difficult to find the correct location of a specific file or folder. On a Mac, the user can view the full location path of the target file by right-clicking on the file and clicking the Get Info option. With a large number of files to be managed, the process becomes a cumbersome task for the admins. Using Hexnode UEM, IT admins can remotely find the file location with just the file name and its extension by executing a custom script on the target endpoint device.

Scripting Language – Bash

File extension – .sh

Disclaimer:


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

Find file path

For example:
find / -name document.txt -print 2>/dev/null

Replace ‘file name with extension’ with the target file. The Terminal may take several minutes to complete this process. To see the output of the custom script, navigate to Manage > Devices > select your device > Action History and select the Show Output option corresponding to the executed action. Even if some scripts are marked as Failed, you can still see the file location by selecting the Show Output option.

Notes:

  • A file can exist at both the system level and the user level, which can return two different file paths for the same file.

List files with specific file extension

For example: find / -name "*.txt" -print 2>/dev/null | head -n 10

This example finds all files with the .txt extension and lists the file paths for the first 10 files only. Replace <extension> with the appropriate file extension and <count> with the desired number of outputs according to your requirement.

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