Unlock Folders
  • 10 Oct 2024
  • 2 Minutes to read
  • PDF

Unlock Folders

  • PDF

Article summary

Use this script to lock the following folders: Documents, Downloads, Music, Pictures, Videos, snap, Public, Templates for both Standard and Sudo Users on Linux machines.

  1. Copy and save the contents below to a UTF-8 editor like notepad++ OR Sublime Text in Windows or gedit in Ubuntu.

    1. If you are using notepad++ then use the bottom right panel to change the type to Unix (LF).

  2. Or click here to download the file.

    #!/bin/bash
    
    # Define directories to unlock
    DIRECTORIES=("Documents" "Downloads" "Music" "Pictures" "Videos" "snap" "Public" "Templates")
    
    # Function to unlock folders for a given user
    unlock_folders() {
        local user="$1"
        local home_dir="/home/$user"
        
        echo "Processing user: $user"
    
        # Check if home directory exists
        if [ ! -d "$home_dir" ]; then
            echo "Error: Home directory $home_dir not found for user $user"
            return
        fi
        
        # Loop through each directory and unlock it
        for DIR in "${DIRECTORIES[@]}"; do
            local folder="$home_dir/$DIR"
            if [ -d "$folder" ]; then
                sudo chattr -i "$folder"
                if [ $? -eq 0 ]; then
                    echo "Unlocked $folder for user $user"
                else
                    echo "Failed to unlock $folder for user $user"
                fi
            else
                echo "Directory $folder does not exist for user $user"
            fi
        done
    }
    
    # Check if script is run with root privileges
    if [ "$(id -u)" != "0" ]; then
        echo "This script must be run with root privileges. Exiting."
        exit 1
    fi
    
    # Discover all user directories under /home except for system users (UID >= 1000)
    for user_dir in /home/*; do
        user=$(basename "$user_dir")
        if [ "$user" != "lost+found" ] && [ $(id -u "$user") -ge 1000 ]; then
            unlock_folders "$user"
        fi
    done
    
    echo "All specified directories are now unlocked for all users."
  3. Follow our guide to upload & publish the script using Scalefusion Dashboard.

Note:

  1. Some of the scripts and their contents are sourced from internet and yes, our new friend ChatGPT.

  2. Please validate the scripts on a test machine before deploying them on all your managed devices.

  3. Scalefusion has tested these scripts, however Scalefusion will not be responsible for any loss of data or system malfunction that may arise due to the usage of these scripts.


Was this article helpful?