2010-01-06 01:56:57 +0000 2010-01-06 01:56:57 +0000
593
593

ファイル以外のすべてのディレクトリを再帰的にchmodする方法は?

755すべてのディレクトリをchmodする方法ですが、ファイルはありません(再帰的に)?

回答 (8)

821
821
821
2010-01-06 01:58:44 +0000

再帰的に ディレクトリの読み取り&実行権限を与えるために:

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 &001

302
302
302
2010-01-06 03:22:25 +0000

この種のことでよくあるのは、ディレクトリは755に設定しても、ファイルは644に設定することです。この場合、nikのfindの例よりも少し早い方法があります:

chmod -R u+rwX,go+rX,go-w /path

意味:

  • -R = 再帰的に;
  • u+rwX = ユーザーは読み書きと実行ができる;
  • go+rX = グループと他の人は読み書きと実行ができる;
  • go-w = グループと他の人は書き込めない

ここで注意すべき重要なことは、大文字のXは小文字のxとは異なる振る舞いをするということです。マニュアルでは次のように読めます:

ファイルがディレクトリである場合、または、execute/search ビットのいずれかが元の (変更されていない) モードで設定されている場合、execute/search ビットが設定されています。

14
14
14
2010-01-22 16:14:43 +0000

644に設定されていて、パス内にexecuteフラグを持っているファイルがあることを確認したい場合は、まずexecuteフラグを削除する必要があります。+Xはすでにそれを持っているファイルから実行フラグを削除しません。

例:

chmod -R ugo-x,u+rwX,go+rX,go-w path

更新: これは最初の変更 (ugo-x) がディレクトリを実行不能にするので、その下にあるすべてのファイルが変更されていないので、失敗するように表示されます。

4
4
4
2012-09-18 14:00:17 +0000

Recursive chmod script for dirs and/or files - Gist :

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にすべてをリセットしたりします)。また、いくつかのエラーシナリオをチェックします。

2
2
2
2018-02-21 11:58:41 +0000

再帰的にディレクトリの読み取り&実行権限を与えるために:

find /path/to/base/dir -type d -exec chmod 755 {} \;

再帰的にファイルの読み取り権限を与えるために:

find /path/to/base/dir -type f -exec chmod 644 {} \;

私は正しい側にnikの答えをアップグレードさせてくれないよりも良い遅さ。私の解決策は遅いですが、それはファイル名の任意のシンボルで、任意の数のファイルで動作し、あなたは普通にsudoでそれを実行することができます(しかし、それはsudoで異なるファイルを発見する可能性があることに注意してください)。

0
0
0
2013-03-26 04:37:44 +0000

この 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の周りにトライ/キャッチが必要でした。

-1
-1
-1
2018-02-16 09:34:58 +0000

また、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' -- '{}'
``` を追加します。
-2
-2
-2
2010-01-29 11:56:05 +0000

以下のようなbashスクリプトを例に挙げることができます。実行可能なパーミッション(755)を与えてください。カレントディレクトリには./autochmod.shを、別のディレクトリを指定するには./autochmod.sh <dir>を使用してください。

関連する質問

5
12
7
8
16