Author: Masons Source: https://www.bysocket.com/2021-10-26/mac-create-files-from-the-root-directory.html

The Mac operating system is very suitable for developers to write code. Recently, I encountered a problem, the problem is how to create a folder in the root directory of macOS. Different macOS versions handle differently, let’s expand on

1. Why create a folder in the root directory of Mac

Some scene programs need to access a specific folder in the root directory, so you need to create a folder in the root directory of macOS.

For example, when the Spring Boot project is running locally on the Mac operating system, the company will default to specify /home/data/log to store the log of the project operation.

So how to create a folder in the root directory of macOS, the following is divided into different macOS versions to solve:

  • macOS@Catalina version
  • macOS@Big Sur version

2. How to create a folder in macOS@Catalina version

Step 1: Turn off the computer and restart it, press and hold the command + R keys when restarting to start the built-in macOS recovery system

Step 2: Find the terminal tool from the menu bar, run the following command, and then restart:

csrutil disable

The purpose of this command is to close SIP. SIP is called "System Integrity Protection" or "System Integrity Protection". You can check its SIP status csrutil status

Step 3: After restarting, remount the root directory first, open the terminal tool and run the following command:

sudo mount -uw /

Step 4: Create the corresponding /Users/XXX/home/data/log folder, and then soft link the corresponding file directory to the root directory. Just run the following command:

sudo ln -s /Users/XXX/home /home

Note: You need to use a soft link to solve it, because if you create a folder directly in the root directory, once the computer is restarted, the previously created directory will have read-only permissions again.

Finally, restart the command + R keys, start the built-in macOS recovery system, and reopen SIP:

csrutil enable

3. How to create a folder in macOS@Big Sur version

Step 1: Start the built-in macOS recovery system and close SIP

Step 2: Run the following command to modify the synthetic.conf file

sudo vi /etc/synthetic.conf

Step 3: Edit the file, enter the following content, and map the corresponding folder to the root directory

home    /Users/XXX/home

Notice:

  • Create the mapped folder in advance
  • Tab in the middle, not spaces

Finally, after restarting the system, the corresponding folder will appear in the system root directory, and the implementation is also in the form of a soft link

Four, summary

Several points can be summarized:

  • Try not to operate some commands in the mac root directory, such as chmod, etc.
  • After SIP is opened, close in time
  • Mac supports file soft link form

    Author: Masons (public name "Programmer Masons") Source: https://www.bysocket.com Welcome to reprint, and please keep this statement. Thanks!

子木聊出海
1.4k 声望3.1k 粉丝

公号:「子木聊出海」