Help us understand the problem. What is going on with this article?

xargsとseqを組み合わせて楽をする

More than 5 years have passed since last update.

※覚え書きです。便利なのに忘れそうで。

たとえばこんな時に

vm1, vm2, vm3, ... にscpでファイルtest1.txt, test2.txt, test3.txt, ...をそれぞれ転送する。

こう使う

seq 1 5 | xargs -i scp test{}.txt vm{}:/tmp

こう書くのと同じ

scp test1.txt vm1:/tmp
scp test2.txt vm2:/tmp
scp test3.txt vm3:/tmp
scp test4.txt vm4:/tmp
scp test5.txt vm5:/tmp

解説

seq

seqは開始と終了の数値を指定して数列(sequence)を生成する。

xargs

xargsは"-i"オプションを利用すると、{}に受け取った数値を代入して引数のコマンドを実行する。

なお、xargsは入力した値ごとにコマンドを実行する。

補足

seqは開始と終了の間に刻み幅を指定できる。

# 4 7 10という数列を作る場合
seq 4 3 10
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away