- 10 Oct 2024
- 2 Minutes to read
- Print
- PDF
Lock Folders
- Updated on 10 Oct 2024
- 2 Minutes to read
- Print
- PDF
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.
Copy and save the contents below to a UTF-8 editor like notepad++ OR Sublime Text in Windows or gedit in Ubuntu.
If you are using notepad++ then use the bottom right panel to change the type to Unix (LF).
Or click here to download the file.
#!/bin/bash # Define directories to lock DIRECTORIES=("Documents" "Downloads" "Music" "Pictures" "Videos" "snap" "Public" "Templates") # Function to lock down and make folders immutable for a given user lock_and_immute_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 lock it for DIR in "${DIRECTORIES[@]}"; do local folder="$home_dir/$DIR" if [ -d "$folder" ]; then sudo chattr +i "$folder" if [ $? -eq 0 ]; then echo "Locked $folder for user $user" else echo "Failed to lock $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 lock_and_immute_folders "$user" fi done echo "All specified directories are now locked for all users."
Follow our guide to upload & publish the script using Scalefusion Dashboard.
Note:
Some of the scripts and their contents are sourced from internet and yes, our new friend ChatGPT.
Please validate the scripts on a test machine before deploying them on all your managed devices.
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.