This post will guide you how to set readonly permmissions for all files under a specific directory in your Linux server. How to change file permission as read only for specific files under a directory in Linux.
You can use chmod command to change file permission as readonly for all files on CentOS/RHEL/Ubuntu/Unix/MacOS system.
Set Readonly Permissions for All Files
To set readonly permissions for all files in your current directory, just type the following command:
# chmod 0444 ./*
To set readonly permission for all files in /root directory, type:
# chmod 0444 /root/*
To set readonly permissions for all .txt files in /var/log/html directory, type:
# chmod 0444 /var/log/html/*.txt
To set readonly permissions for all files only under /var/log/html directory and sub-directories, type:
# find /var/log/html/ -type f -iname "*" -print0 | xargs -I {} -0 chmod 0444 {}
Set Readonly Permissions For Directoreis Only
To set readonly permission for a specific directory, such as: set the current directory in readonly mode, type:
# chmod 0444 ./
To set readonly permission for /var/log/html/ directory only, type:
# chmod 0444 /var/log/html/
To set readonly permissions for all files and sub-directories under /var/log/html directory, type:
# chmod 0444 -R /var/log/html/
To set readonly permissions for all directories and sub-directories only under /var/log/html directory, type:
# find /var/log/html/ -type d -print0 | xargs -0 -I {} chmod 0444 {}