LoginSignup
2
3

More than 5 years have passed since last update.

find と xargs

Posted at

はじめに

とあるディレクトリ群に対し、一括で同ファイルを置く必要があった

条件

  • /〇〇〇/△△△/ 直下のディレクトリ
  • シンボリックリンクは含まない
  • 特定の PATTERN に合致するディレクトリは除く
find /〇〇〇/△△△/ -maxdepth 1 -mindepth 1 -type d ! -type l ! -regex 'PATTERN' -exec cp /◇◇◇/☆☆☆/hoge.php {}/fuga.php \;

今回 find の exec オプションを使用し、目的は達成した。
しかし、「xargs を使った時との違い、分かってる?」 と聞かれ、微塵も知らなかったので調べてみた。

結論

xargs 使いましょう(`・ω・´)

find コマンド

print0 オプション

man find

-print0
True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses).
This allows file names that contain newlines or  other  types of white space to be correctly interpreted by programs that process the find output.
This option corresponds to the -0 option of xargs.

つまり、ファイルの区切り文字を null として扱ってくれる。
そして xargs の -0 に対応しているとのこと

xargs コマンド

0 オプション

man xargs

-0
Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally).
Disables the end of file string, which is treated like any other argument.
Useful when input items might contain white space, quote marks, or backslashes.
The GNU find -print0 option produces input suitable for this mode.

参考

ここに全て書いてありました

xargs
http://ja.wikipedia.org/wiki/Xargs

findとxargsの基本的な使い方
http://www.xmisao.com/2013/09/01/how-to-use-find-and-xargs.html

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3