Recursively modify permissions for subdirectories and files

Source: Internet
Author: User
Tags chmod file permissions

Background: When our virtual machines and Windows systems share a directory on Windows (Note: a), the directory under the directory (b) moved (MV) to any directory under Linux, this directory B and the following subdirectory or file permissions are rwx (777), This is their permissions under Windows. So with ll command to list these files, you will find that the obvious difference, because the permissions are different.

So I wrote a bash script that recursively modifies the permissions of these moved files to the default values . The code is as follows:

#!/bin/bash

# Modify the subdirectory or file of the specified directory to the default permissions
# The following function is the right to recursively modify subdirectories and files, but there is another way to copy the entire directory, and the permissions will naturally be set to the default
function Chmoddefault ()
{
	If [-d ' $]; then
		chmod 755 ' $ ' for I-in-

	$1/* do
		if [-f ' $i]; then
			chmod 644 "$i"
		elif [D "$i"]; then
			chmod 755 "$i"
			chmoddefault "$i"
		fi
	done
}
 if ["$" = "]; Then
	echo "Usage:sh $ <dir>"
else
	chmoddefault "$"
fi


After testing, the script was found to be correct. But also found another way to change the permissions of the directory to move over, is to use the Copy command (CP), which is equivalent to the current user's identity to create a new directory, so the permissions are naturally the default value!

such as: cp/mnt/hgfs/a/aa/home/zcm/download/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.