LoginSignup
8
6

More than 1 year has passed since last update.

Linux | xargs の -I オプションで 後続コマンドに引数を渡す(的なことをする)

Last updated at Posted at 2016-06-13

xargs に -I オプションをつけると「後続のコマンドに対して値を渡す」的なことが出来る。

( 大文字のアイ )

たとえば、ディレクトリに次のファイルが存在する場合。

  • File_A
  • File_B
  • File_C

ls | xargs-I FILE を渡すと。
後続の echo の中で、 FILE と書いた部分が、実際のファイル名に変換されているのが分かるだろう。

$ ls | xargs -I FILE echo "FILE exists."

./ exist.
../ exist.
File_A exists.
File_B exists.
File_C exists.

ちなみに

echo する内容を引用符 "" で囲わなくても、動作は同じだった。

$ ls | xargs -I FILE echo FILE exists.

./ exist.
../ exist.
File_A exists.
File_B exists.
File_C exists.

これは「後続のコマンドに対して、引数が展開される」のではなくて。
「xargs の後に続く 文字列自体 が、いちど テキスト変換 されてから、後続のコマンドが実行される」という動作のような気がした。

補足

ここに明確に replace-str って書いてあった。
https://www.gnu.org/software/findutils/manual/html_node/find_html/xargs-options.html

-i[replace-str]
Replace occurrences of replace-str in the initial arguments with names read from standard input. Also, unquoted blanks do not terminate arguments; instead, the input is split at newlines only. If replace-str is omitted (omitting it is allowed only for ‘-i’), it defaults to ‘{}’ (like for ‘find -exec’). Implies ‘-x’ and ‘-l 1’. The ‘-i’ option is deprecated in favour of the ‘-I’ option.

参考

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

8
6
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
8
6