再帰的に移動する方法`:``` xargs 'または `exec`を使って特定の値を渡す

再帰的に移動する方法`:``` xargs 'または `exec`を使って特定の値を渡す

<user_id>:を<group_id>特定の値として使用するか、再帰的に移動しようとしています。出力を変数に渡す関数を使用することは困難です。私はUbuntu 20.04を実行しています。xargsexecfindstat

find . -type f,d | xargs chown $(($(stat --printf %u {})+1)):$(($(stat --printf %g {})+1)) {}

stat: cannot stat '{}': No such file or directory

stat: cannot stat '{}': No such file or directory

chown: cannot access '{}': No such file or directory

ベストアンサー1

ここでは以下を使用しますzsh

#! /bin/zsh -
# enable the stat builtin (which btw predates GNU stat by several years)
zmodload zsh/stat || exit

# enable chown (and other file manipulation builtin)
zmodload zsh/files || exit
ret=0

for f (./**/*(DN.,/) .) {
  stat -LH s $f && # store file attributes in the $s hash/associative array
    chown $((s[uid] + 1)):$((s[gid] + 1)) $f || ret=$?
}
exit $ret # report any error in stat()ing or chown()ing files

Ddotfileは隠しファイルが含まれていることを意味し、nullglobは通常のファイルや類似のディレクトリが見つからない場合はNエラーとして扱われないことを意味します。).,/-type f,d

Ubuntu 20.04などのGNUシステムでは、次のこともできます。

find . -type f,d -printf '%p\0%U\0%G\0' | # print path, uid, gid as 3
                                          # nul-delimited records
  gawk -v RS='\0' -v ORS='\0' '{
    file = $0; getline uid; getline gid
    print (uid+1) ":" (gid+1); print file}' | # outputs a uid+1:gid+1
                                              # and path record for each file
  xargs -r0n2 chown # pass each pair of record to chown

chownただし、これにはファイルごとに1つずつ実行する操作が含まれているため(このアプローチではモジュールに組み込まれている関数を実行するzsh)、はるかに効率的ではありません。chownzsh/files

おすすめ記事