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

Scripting language – Bash

File extension – .sh

 Disclaimer: 
The sample scripts provided below are adapted from third-party open-source sites.

 

Hide file/folder
----------------

Script to hide files and folders on macOS devices

\#!/bin/bash chflags hidden 'path to 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.

Script to hide files on macOS devices

\#!/bin/bash chflags hidden '/Users/admin/Desktop/Notes.txt'

   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.

Script to hide folders on macOS devices

\#!/bin/bash chflags hidden '/Users/admin/Desktop/MyDocuments' 

   1

2

  \#!/bin/bash 

chflags hidden '/Users/admin/Desktop/MyDocuments' 

   

 

  

Unhide file/folder
------------------

 Note: 
The script cannot be used to unhide system files or folders that are hidden by default.

 

Script to unhide files and folders on macOS devices

\#!/bin/bash chflags nohidden 'path to 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.

Script to unhide files on macOS devices

\#!/bin/bash chflags nohidden '/Users/admin/Desktop/Notes.txt'

   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.

Script to unhide folders on macOS devices

\#!/bin/bash chflags nohidden '/Users/admin/Desktop/MyDocuments' 

   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.

 Note: 
The script will also reveal system files and folders that are hidden by default.

 

Script to show all hidden files on macOS devices

\#!/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"

   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.

Script to hide all revealed hidden files on macOS devices

\#!/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" 

   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" 

   

 

  

 Notes:- 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.