web-dev-qa-db-ja.com

スクリプトまたはnautilusメニューを介して再帰的なchmodのみのフォルダーまたはファイルのみ?

これは以前に議論されました here

私が知りたいのは、これらをどのように変えるかです:

このフォルダー内の再帰的なchmodのみのファイル:

find . -type f -exec chmod 0600 {} \;

このフォルダー内の再帰的なchmodのみのフォルダー:

find . -type d -exec chmod 0755 {} \;

bashスクリプトに入れると、次のようになります。

ファイルの場合:

rchmodf 744 .

ディレクトリの場合:

rchmodd 755 .

そして...それが可能であれば、おそらくnautilusの右クリックメニューオプションに移動することもできます。

8
DM8

次のスクリプトは、最初の引数としてモードを渡し、後続の引数として1つ以上のディレクトリ名を渡すことで呼び出すことができます。 Linuxでは、ディレクトリ名を渡さない場合、_._(現在のディレクトリ)を渡した場合と同じになります。このスクリプトにrchmodfという名前を付けて実行可能にし(_chmod a+rx /path/to/rchmodf_)、_$PATH_のどこかに配置します。

_#!/bin/sh
mode=$1; shift
find "$@" -type f -exec chmod "$mode" {} +
_

説明:_mode=$1; shift_は、変数modeをスクリプトの最初の引数に設定し、その最初の引数をリストから削除します。 _"$@"_は、すべての引数のリストに展開されます。

必要に応じて、ディレクトリモードとファイルモードの両方を受け入れるスクリプトを作成できます。

_#!/bin/sh
dir_mode=$1; shift
file_mode=$1; shift
find "$@" -type d -exec chmod "$dir_mode" {} + -o -type f -exec chmod "$file_mode" {} +
_

744は便利なファイルモードではないことに注意してください。 644(ユーザーが書き込み可能で誰でも読み取り可能)と755(これも世界中で実行可能)の方がはるかに一般的です。また、ツリー内のすべてのファイルを実行可能または実行不可能に変更しても、ほとんど役に立ちません。おそらく、このスクリプトを_+rX_(大文字のX、すでに実行可能なディレクトリとファイルに対してのみ実行可能ビットを設定するため)のような引数で呼び出したいと思うでしょう。実際、Xシンボリックモードは、おそらくこれらのスクリプト__chmod -R +rX ._を使用した後のものです。

Bashまたはzshを使用すると、再帰的に動作するがディレクトリのみで動作する別の方法があります。 bashの場合、バージョン4が必要で、最初に_shopt -s globstar_を実行します。

_chmod a+rx **/*/
_

Zshでは、_(.)_:chmod a+r **/*(.)を接尾辞として付けることによってのみファイルを操作できます。

ノーチラスの質問を渡します。

スクリプトはuser23538によってリンクされています。

私はそれを試しました、そしてそれはうまくいきます。使用する場合はご注意ください。パス引数として(スクリプトが配置されているのと同じディレクトリで実行)、実際にはスクリプトのファイル権限を644に変更するため、上のディレクトリに配置します。

#!/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
1
Kluny

基本的に上記を実行するスクリプトを作成しましたが、コマンドラインオプション(ディレクトリやファイルのアクセス許可、または両方を除外することにより、すべてを755-644に自動的にリセットします)にも柔軟性を提供します。また、いくつかのエラーシナリオをチェックします。

http://bigfloppydonkeydisk.blogspot.com.au/2012/09/recursively-chmod-only-files-or.html

0
user23538