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
View all files in a folder
1 2 |
#!/bin/bash ls –R 'folder path' |
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
1 2 |
#!/bin/bash ls –a 'folder path' |
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
1 2 |
#!/bin/bash ls –l 'folder path' |
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
1 2 |
#!/bin/bash ls –t 'folder path' |
For example:
ls –t /Users/QA/Desktop/Wallpapers
The -t
command sorts the files by Time of Last Modification (latest first) instead of Name.