SATOHOST
No Result
View All Result
Thứ Sáu, Tháng Một 22, 2021
  • Login
  • vps giá rẻ
  • cpanel nulled
  • bitcoin exchange script
  • Home 1
Subscribe
SATOHOST
  • vps giá rẻ
  • cpanel nulled
  • bitcoin exchange script
  • Home 1
No Result
View All Result
SATOHOST
No Result
View All Result
Home Blog

Linux / UNIX: Change File Permissions Recursively ( conditional )

admin by admin
Tháng Sáu 3, 2019
in Blog
5 min read
0
152
SHARES
1.9k
VIEWS
Share on FacebookShare on Twitter

How do I recursively change files with 777 permissions to 755 in /home/user/demo directory? I have a number of files in this directory and I need to change permission from 0777 to only if that file has 777 permissions. Is there an easy way out to achieve this on a Linux or Unix-like systems?

To change file access permissions you need to use the chmod command. It has -R or –recursive option that change files and directories recursively. [donotprint][/donotprint]The find command can be used to find files and directories. The chown command can be used to change user and group permission.

chmod Command Examples

In this example, you are setting permission to 0755:
$ chmod -R 0755 directoryNameHere
However, if you need to apply conditional file permissions recursively, you need to use combination of the find and chmod command. To find all files in /home/user/demo directory, enter:
$ find /home/user/demo -type f -print
To find all files in /home/user/demo directory with permission 777, enter:
$ find /home/user/demo -type f -perm 777 -print
Finally, apply new permission using the -exec option as follows:
$ find /home/user/demo -type f -perm 777 -print -exec chmod 755 {} ;
To select directories and subdirectories use the following syntax:

$ find /var/www/html -type d -perm 777 -print -exec chmod 755 {} ;

Sample Shell Script To Change Permission Recursively

#!/bin/bash
# Purpose: Set correct webserver files and dir permissions
# Author: SXI ADMIN < vivek@nixcraft.com >
# This script is released under GPL version 2.0 or above
# Set root permission as follows for the Apache / Lighttpd / Nginx DocumentRoot
# + Dirs/Subdirs: read-only and execute to others
# + Files: read-only permission
# Tested on Debian Linux v3/4/5/6 and RHEL v2/3/4/5/6
# -------------------------------------------------------------------------------------------------
_dir="${1:-.}"
_fperm="0444"
_dperm="0445"
_ugperm="root:root"
_chmod="/bin/chmod"
_chown="/bin/chown"
_find="/usr/bin/find"
_xargs="/usr/bin/xargs"
 
echo "I will change the file permission for webserver dir and files to restrctive read-only mode for "$_dir""
read -p "Your current dir is ${PWD}. Are you sure (y / n) ?" ans
if [ "$ans" == "y" ]
then
	echo "Changing file onwership to $_ugperm for $_dir..."
	$_chown -R "${_ugperm}" "$_dir"
 
	echo "Setting $_fperm permission for $_dir directory...."
	$_chmod -R "${_fperm}" "$_dir"
 
	echo "Setting $_dperm permission for $_dir directory...."
	$_find "$_dir" -type d -print0 | $_xargs -0 -I {} $_chmod $_dperm {}
fi

#!/bin/bash
# Purpose: Set correct webserver files and dir permissions
# Author: SXI ADMIN < vivek@nixcraft.com >
# This script is released under GPL version 2.0 or above
# Set root permission as follows for the Apache / Lighttpd / Nginx DocumentRoot
# + Dirs/Subdirs: read-only and execute to others
# + Files: read-only permission
# Tested on Debian Linux v3/4/5/6 and RHEL v2/3/4/5/6
# ————————————————————————————————-
_dir="${1:-.}"
_fperm="0444"
_dperm="0445"
_ugperm="root:root"
_chmod="/bin/chmod"
_chown="/bin/chown"
_find="/usr/bin/find"
_xargs="/usr/bin/xargs" echo "I will change the file permission for webserver dir and files to restrctive read-only mode for "$_dir""
read -p "Your current dir is ${PWD}. Are you sure (y / n) ?" ans
if [ "$ans" == "y" ]
then
echo "Changing file onwership to $_ugperm for $_dir…"
$_chown -R "${_ugperm}" "$_dir" echo "Setting $_fperm permission for $_dir directory…."
$_chmod -R "${_fperm}" "$_dir" echo "Setting $_dperm permission for $_dir directory…."
$_find "$_dir" -type d -print0 | $_xargs -0 -I {} $_chmod $_dperm {}
fi

You can run this script as follows:
./script /var/www/html/
./script /usr/lib/cgi-bin/

Share on Facebook Twitter

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Tags: blog
admin

admin

Related Posts

Blog

LINUX BACKUP & RESTORE FULL OS

Tháng Mười Hai 16, 2020
Blog

VPS Ram 4GB Chỉ 199k/tháng Miễn Phí Gsuite,

Tháng Tám 29, 2020
Blog

Tài Khoản Google Drive Unlimited 2020 50k – Google Drive Không Giới Hạn – Google drive unlimited 2020

Tháng Bảy 11, 2020
Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)
Blog

Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)

Tháng Bảy 11, 2020
Blog

Hướng dẫn kích hoạt bản quyền Windows Server 2012 R2 không cần crack

Tháng Sáu 10, 2020
Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z
Blog

Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z

Tháng Sáu 4, 2020
Hướng dẫn Tự Tạo Ứng Dụng Họp Online Phòng Học Online Miễn Phí với Jitsi
Blog

Hướng dẫn Tự Tạo Ứng Dụng Họp Online Phòng Học Online Miễn Phí với Jitsi

Tháng Tư 13, 2020
Hướng dẫn sử dụng Remote Desktop để truy cập VPS
Blog

Hướng dẫn sử dụng Remote Desktop để truy cập VPS

Tháng Tư 12, 2020
Download Kali Linux 2020.1
Blog

Download Kali Linux 2020.1

Tháng Tư 12, 2020
vps giá rẻ
  • Trending
  • Comments
  • Latest
Hướng dẫn cài DirectAdmin Nulled mới nhất 2020

Hướng dẫn cài DirectAdmin Nulled mới nhất 2020

Tháng Sáu 10, 2020
Wowza Streaming Engine 4.7.7 Crack Linux/Windows

Wowza Streaming Engine 4.7.7 Crack Linux/Windows

Tháng Mười Một 15, 2020
Nén và giải nén trong linux centos 6 centos 7 nén zip, tar.gz và tar.bz2.

Khuyến mãi vps 50k/tháng SSD 50Gb

Tháng Sáu 15, 2019
Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)

Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)

Tháng Bảy 11, 2020

Cpanel Nulled Cpanel/whm Nulled on Centos VPS/Dedicated Servers

Tháng Mười 8, 2020
Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z

Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z

Tháng Sáu 4, 2020

LINUX BACKUP & RESTORE FULL OS

0

Milestones & Timeline

0

The Economics of Websites

0

The Landing Page Guide

0

The Next Big Thing

0

UX Design Is Easy

0

LINUX BACKUP & RESTORE FULL OS

Tháng Mười Hai 16, 2020

VPS Ram 4GB Chỉ 199k/tháng Miễn Phí Gsuite,

Tháng Tám 29, 2020

Tài Khoản Google Drive Unlimited 2020 50k – Google Drive Không Giới Hạn – Google drive unlimited 2020

Tháng Bảy 11, 2020
Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)

Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)

Tháng Bảy 11, 2020

Hướng dẫn kích hoạt bản quyền Windows Server 2012 R2 không cần crack

Tháng Sáu 10, 2020
Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z

Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z

Tháng Sáu 4, 2020
  • vps giá rẻ
  • cpanel nulled
  • bitcoin exchange script
  • Home 1
Call us: 0975757375

© 2020

No Result
View All Result
  • vps giá rẻ
  • cpanel nulled
  • bitcoin exchange script
  • Home 1

© 2020

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In