如何chmod 755所有目录但没有文件(递归)?
反过来说,如何只对文件进行chmod(递归)而不对目录进行chmod?
递归地给予目录读&执行权限。
find /path/to/base/dir -type d -exec chmod 755 {} +
递归给予文件读取权限。
find /path/to/base/dir -type f -exec chmod 644 {} +
或者,如果有很多对象需要处理。
chmod 755 $(find /path/to/base/dir -type d)
chmod 644 $(find /path/to/base/dir -type f)
或者,为了减少 "chmod "的产卵。
find /path/to/base/dir -type d -print0 | xargs -0 chmod 755
find /path/to/base/dir -type f -print0 | xargs -0 chmod 644
这类事情的一个常见原因是将目录设置为755,但文件设置为644。在这种情况下,有一个比nik的`find'例子稍快的方法。
chmod -R u+rwX,go+rX,go-w /path
意思是说。
-R
= 递归。u+rwX
= 用户可以读、写和执行。go+rX
= 小组和其他人可以阅读和执行。go-w
= 小组和其他人不能写这里需要注意的是,大写的X
与小写的x
作用不同。在手册中,我们可以阅读。
如果文件是一个目录或任何一个执行/搜索位在原始(未修改的)模式下被设置,则执行/搜索位。
换句话说,在一个文件上的chmod u+X不会设置执行位;而g+X只会在用户已经设置了执行位的情况下设置。
我决定自己写一个小脚本。
#!/bin/sh
#
# chmodr.sh
#
# author: Francis Byrne
# date: 2011/02/12
#
# Generic Script for recursively setting permissions for directories and files
# to defined or default permissions using chmod.
#
# Takes a path to recurse through and options for specifying directory and/or
# file permissions.
# Outputs a list of affected directories and files.
#
# If no options are specified, it recursively resets all directory and file
# permissions to the default for most OSs (dirs: 755, files: 644).
# Usage message
usage()
{
echo "Usage: $0 PATH -d DIRPERMS -f FILEPERMS"
echo "Arguments:"
echo "PATH: path to the root directory you wish to modify permissions for"
echo "Options:"
echo " -d DIRPERMS, directory permissions"
echo " -f FILEPERMS, file permissions"
exit 1
}
# Check if user entered arguments
if [ $# -lt 1 ] ; then
usage
fi
# Get options
while getopts d:f: opt
do
case "$opt" in
d) DIRPERMS="$OPTARG";;
f) FILEPERMS="$OPTARG";;
\?) usage;;
esac
done
# Shift option index so that $1 now refers to the first argument
shift $(($OPTIND - 1))
# Default directory and file permissions, if not set on command line
if [ -z "$DIRPERMS" ] && [ -z "$FILEPERMS" ] ; then
DIRPERMS=755
FILEPERMS=644
fi
# Set the root path to be the argument entered by the user
ROOT=$1
# Check if the root path is a valid directory
if [ ! -d $ROOT ] ; then
echo "$ROOT does not exist or isn't a directory!" ; exit 1
fi
# Recursively set directory/file permissions based on the permission variables
if [ -n "$DIRPERMS" ] ; then
find $ROOT -type d -print0 | xargs -0 chmod -v $DIRPERMS
fi
if [ -n "$FILEPERMS" ] ; then
find $ROOT -type f -print0 | xargs -0 chmod -v $FILEPERMS
fi
: chmodr.sh
#!/bin/sh
#
# chmodr.sh
#
# author: Francis Byrne
# date: 2011/02/12
#
# Generic Script for recursively setting permissions for directories and files
# to defined or default permissions using chmod.
#
# Takes a path to recurse through and options for specifying directory and/or
# file permissions.
# Outputs a list of affected directories and files.
#
# If no options are specified, it recursively resets all directory and file
# permissions to the default for most OSs (dirs: 755, files: 644).
# Usage message
usage()
{
echo "Usage: $0 PATH -d DIRPERMS -f FILEPERMS"
echo "Arguments:"
echo "PATH: path to the root directory you wish to modify permissions for"
echo "Options:"
echo " -d DIRPERMS, directory permissions"
echo " -f FILEPERMS, file permissions"
exit 1
}
# Check if user entered arguments
if [ $# -lt 1 ] ; then
usage
fi
# Get options
while getopts d:f: opt
do
case "$opt" in
d) DIRPERMS="$OPTARG";;
f) FILEPERMS="$OPTARG";;
\?) usage;;
esac
done
# Shift option index so that $1 now refers to the first argument
shift $(($OPTIND - 1))
# Default directory and file permissions, if not set on command line
if [ -z "$DIRPERMS" ] && [ -z "$FILEPERMS" ] ; then
DIRPERMS=755
FILEPERMS=644
fi
# Set the root path to be the argument entered by the user
ROOT=$1
# Check if the root path is a valid directory
if [ ! -d $ROOT ] ; then
echo "$ROOT does not exist or isn't a directory!" ; exit 1
fi
# Recursively set directory/file permissions based on the permission variables
if [ -n "$DIRPERMS" ] ; then
find $ROOT -type d -print0 | xargs -0 chmod -v $DIRPERMS
fi
if [ -n "$FILEPERMS" ] ; then
find $ROOT -type f -print0 | xargs -0 chmod -v $FILEPERMS
fi
它基本上是做递归chmod,但也为命令行选项提供了一点灵活性(设置目录和/或文件权限,或排除两者,它自动将一切重置为755-644)。 它还会检查一些错误情况。
我也写了关于它的文章在我的博客上。
1:
试试这个python脚本。 它不需要生成进程,而且每个文件只做两次系统调用。 除了用C语言实现外,它可能是最快的方法(我需要它来修复一个有1500万个文件的文件系统,这些文件都被设置为777)。
#!/usr/bin/python3
import os
for par, dirs, files in os.walk('.'):
for d in dirs:
os.chmod(par + '/' + d, 0o755)
for f in files:
os.chmod(par + '/' + f, 0o644)
在我的例子中,由于chmodd一些特殊文件失败,所以在最后一次chmod的周围需要进行尝试/捕捉。
你也可以使用tree
。
tree -faid /your_directory | xargs -L1 -I{} bash -c 'sudo chmod 755 "$1"' -- '{}'
如果你想同时查看该文件夹,请添加一个回声。
tree -faid /your_directory | xargs -L1 -I{} bash -c 'sudo chmod 755 "$1" && echo$1' -- '{}'
你可以用下面的bash脚本作为例子。 一定要给它可执行的权限(755).只要用./autochmod.sh来表示当前的目录,或者./autochmod.sh <dir>。 简单的使用./autochmod.sh来指定当前目录,或者使用./autochmod.sh <dir> 来指定一个不同的目录。
#!/bin/bash
if [ -e $1 ]; then
if [ -d $1 ];then
dir=$1
else
echo "No such directory: $1"
exit
fi
else
dir="./"
fi
for f in $(ls -l $dir | awk '{print $8}'); do
if [ -d $f ];then
chmod 755 $f
else
chmod 644 $f
fi
done