+ uniq -c と perl 正規表現の検索と並べ替え

+ uniq -c と perl 正規表現の検索と並べ替え

/srv/my-old-disk/_oldstuff/一部の古いファイル/フォルダをからにコピーしました/srv/my-normal-disk/_oldstuff

この分析をどのように取得できますか/srv/my-old-disk/_oldstuff/

その後、特定のファイル形式(ファイル、ディレクトリ、リンク)の変更に対して不要な権限(400など)を必要な権限セット(644など)の配置にどのように設定できますか?

ベストアンサー1

+ uniq -c と perl 正規表現の検索と並べ替え

一括でファイル権限を変更する

0) 必要な分析ファイルの生成

  • A)findといくつかの-printfフラグを使用してリストを取得する

  • B)統計情報を取得するには、sort + uniq -cを使用してください。

  • C)「一般」権限のないファイル/ディレクトリリストのインポート

    $ cd /srv/dev-disk-by-uuid-f0233da3-473e-450d-9a18-b2ae254fc439/_get/_move
    
    $ find . -printf "%m %y %p\n" > ~/filelist-1a-perms-type-path-findraw.txt
    
    $ cat ~/list-perms-type-path-findraw.txt | sort > ~/filelist-1b-perms-type-path-sorted.txt
    
    $ find . -printf "%m %y\n" > ~/filelist-1c-perms-type-findraw.txt
    
    $ find . ~/filelist-1c-perms-type-findraw.txt | sort | uniq -c | sort > ~/filelist-1d-perms-type-extraordinary.txt
    
    $ perl -ne 'print if /^(?!755 |644 )/' ~/filelist-1b-perms-type-path-sorted.txt ~/filelist-1f-perms-type-path-extraordinary.txt
    
  • これにより、次のサンプルコンテンツを含む次のファイルが取得されます。


~/filelist-1a-perms-type-path-findraw.txt

  • find の生出力です。

  • findは辞書式で探索するのではなく、論理的にファイルシステムを探索することに注意してください。 (これを行う方法を正確に説明できる人がいる場合は、コメントを残してください。)

    755 d .
    644 f ./Installers-Notes.txt
    644 f ./Installers-Bootdisk.txt
    755 d ./Software
    755 d ./Software/Multiplatform
    644 f ./Software/Multiplatform/.DS_Store
    644 f ./Software/Multiplatform/somesfiles.tgz
    755 d ./Software/Multiplatform/Some Extensions
    644 f ./Software/Multiplatform/Some Extensions/.DS_Store
    644 f ./Software/Multiplatform/Some Extensions/some_plugin.xpi
    ...
    

~/filelist-1b-perms-type-path-sorted.txt

  • 一番左の列から一番右の列に並べ替え

  • まず、権限をクリックしてから(ファイル、ディレクトリ、リンクなど)を入力し、ファイルパスを入力します。

  • 必要に応じて別のソート順を作成することもできます。

    444 f ./Software/Mac/Software1.zip
    444 f ./Software/Mac/Software5.cdr
    444 f ./Software/Mac/Software7.zip
    ...
    
    640 f ./Software/Mac/Software2.dmg
    640 f ./Software/Mac/Software3/Software3.dmg
    640 f ./Software/Mac/Software4.zip
    640 f ./Software/Mac/Software6.dmg
    640 f ./Software/Mac/Software7.zip
    ...
    

~/filelist-1c-perms-type-findraw.txt

  • 1cは1dを生成するための補助ファイルです。

  • パイピングに直接スキップすることもできます。

  • ただし、時間がかかる長いリストの場合は、どこかで間違えても最初から再生成する必要がない中間ファイルを好みます。

  • 後でこれで終了できます。

    755 d
    644 f
    644 f
    755 d
    755 d
    644 f
    644 f
    755 d
    644 f
    644 f
    644 f
    

~/filelist-1d-perms-type-extraordinary.txt

  • このファイルはいくつかの統計を提供します。

  • これに基づいて、1f「特別な」分析ファイルにリストする一般的ではないファイル/フォルダを決定し、それに応じて正規表現を調整します。

          2 555 f
          6 700 d
          7 645 f
         13 600 f
         20 444 f
         22 700 f
         57 640 f
        124 744 f
        423 777 l
      12315 755 d
      35606 755 f
      60759 644 f
    

~/filelist-1e-perms-type-extraordinary-annotated.txt

  • これは単に一次元のトリックにあなたの研究/アイデアに関するコメントを加えたものです。

    # Files:
          2 555 f
          7 645 f
         13 600 f
         20 444 f # Your remark here why this perms good/bad
         22 700 f
         57 640 f
        124 744 f
      35606 755 f
      60759 644 f
    
    # Directories:
          6 700 d
      12315 755 d
    
    # Links:
        423 777 l
    

~/filelist-1f-perms-type-path-extraordinary.txt

  • このファイルは、権限を大量に変更するなど、後で分析してアクションを実行するのに役立ちます。
  • このファイルに対して取得する行は、次から始まります。perl -ne 'print if /^(?!755 |644 )/'
  • 正規表現の説明:
    • 路線出発地点^

    • これが続かない場合は、いわゆる(?!expression)ネガティブLookaheadになります。

    • 式は755 |(論理的OR)です。644

    • 数字の後のスペースは、「7550」のように一致しない可能性がある4桁の権限表現ではないため、必要ありません。しかし、私はそれについてもっと確信しています。

      444 f ./Software/Mac/SoftwareA/File1.bom
      444 f ./Software/Mac/SoftwareA/Fil2.pax.gz
      444 f ./Software/Mac/SoftwareA/File3/Contents/PkgInfo
      444 f ./Software/Mac/SoftwareA/File3/Contents/File1.sizes
      444 f ./Software/Mac/SoftwareA/File3/Contents/File2.plist
      444 f ./Software/Mac/SoftwareA/File3/Contents/File3.info
      ...
      
      
      555 f ./Software/Mac/CategoryA/SoftwareB/README.txt
      555 f ./Software/Mac/_Audio Production Software/Propellerheads Reason 5/Source.nfo
      555 f ./Software/Mac/CategoryA/SoftwareC/Source.nfo
      ...
      
  • このファイルのリストは、調整が必要なディレクトリ/ファイルを理解するのに役立ちます。
  • また、検索を使用して特定の種類(ファイル/ディレクトリ/リンク)と特定の権限を持つファイルエントリを選択的に検索し、必要な他の権限に変更することもできます。

特定の権限セットを持つファイル/フォルダを見つけて別の権限セットに変更する

すべてのファイルに対する権限を444から644に変更するとします。

1)不要な特定の権限セットのすべてのファイルを印刷します。

$ find . -type f -perm 444 -printf "%m %y %p\n"
  • -type f ファイルのみ検索
  • -perm 444特権444
  • -printf私たちがしていることを制御するために適切な出力を取得してください。
    • printfでなければならないことに注意してください。後ろにすべてのフィルタ
    • それ以外の場合、ifはこれまでに発生した内容を印刷しますが、次のフィルタチェーンが適用された後に残った内容は印刷しません。
444 f ./Software/Mac/SoftwareA/File1.bom
444 f ./Software/Mac/SoftwareA/Fil2.pax.gz
444 f ./Software/Mac/SoftwareA/File3/Contents/PkgInfo
444 f ./Software/Mac/SoftwareA/File3/Contents/File1.sizes
444 f ./Software/Mac/SoftwareA/File3/Contents/File2.plist
444 f ./Software/Mac/SoftwareA/File3/Contents/File3.info
...

2) 今一括変更してみましょう。

$ find . -type f -perm 444 -printf "%m %y %p\n" -exec chmod 644 {} \;
  • -exec chmod 644 {} \;最後に追加しました
  • フィルタを通過したすべてのファイルに対して実行chmod 644
  • 前の手順とまったく同じリストを取得する必要があります(それ以降はファイルの内容が変更されていない場合)。
  • エラーが発生した場合は、さらに取得できます。

3) うまく実行されたか最終確認

$ find . -type f -perm 444 -printf "%m %y %p\n"
  • ステップ3と同じコマンドライン
  • 今回は、findを適用したパス(現在の作業ディレクトリ).の下に444個のファイルがなくなっているため、NULL(ファイルリストなし)を返す必要があります。

おすすめ記事