Category filter
Script to hide/unhide files and folders on macOS devices
On a Mac, certain system files and folders are hidden by default. This is to protect the system files from accidental/unwanted modifications by users. But, if you want to hide more sensitive files/folders present on your devices or make them unavailable to the user unless they are useful, or unhide a file you previously hid, you can execute these scripts using the Execute Custom Script action.
Scripting language – Bash
File extension – .sh
Hide file/folder
1 2 |
#!/bin/bash chflags hidden 'path to file/folder' |
For example, to hide a file named Notes.txt
stored at /Users/admin/Desktop
, deploy the following script.
1 2 |
#!/bin/bash chflags hidden '/Users/admin/Desktop/Notes.txt' |
Although they remain present on the device, the hidden files are neither accessible to the user nor do they appear in Finder.
Similarly, to hide a folder named My Documents
stored at /Users/admin/Desktop
deploy the following script.
1 2 |
#!/bin/bash chflags hidden '/Users/admin/Desktop/MyDocuments' |
Unhide file/folder
1 2 |
#!/bin/bash chflags nohidden 'path to file/folder' |
For example, to unhide a file named Notes.txt
stored at /Users/admin/Desktop
, deploy the following script.
1 2 |
#!/bin/bash chflags nohidden '/Users/admin/Desktop/Notes.txt' |
Similarly, to unhide a folder named My Documents
stored at /Users/admin/Desktop
, you can deploy the following script.
1 2 |
#!/bin/bash chflags nohidden '/Users/admin/Desktop/MyDocuments' |
Reveal all hidden files/folders
You can reveal all hidden files and folders on your device by deploying the following script. However, the icons of the hidden files and folders appear faded.
1 2 3 4 |
#!/bin/bash loggedInUser="" loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'` su -l "$loggedInUser" -c "defaults write com.apple.finder AppleShowAllFiles -boolean true; killall Finder" |
This will reveal all hidden files and folders, allowing the currently logged-in user to access them.
Hide all revealed hidden files/folders
Once revealed, if you would like to hide all the revealed files and folders again, you can do so by deploying the following script.
1 2 3 4 |
#!/bin/bash loggedInUser="" loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'` su -l "$loggedInUser" -c "defaults write com.apple.finder AppleShowAllFiles -boolean false; killall Finder" |