PinkElephantsOnParade
PinkElephantsOnParade

Reputation: 6592

How to properly form rm with xargs -a?

A coworker showed me a nifty way of using rm and xargs for deleting filenames listed in a .txt - but I can't remember what he did.

I ran

echo | xargs -a file.txt

where file.txt contained

1
2
3
4

And it printed

1 2 3 4

My logic says that

rm | xargs -a file.txt

should delete the files I created titled 1 and 2 and 3 and 4.

But that is not the behavior I get.

How do I form this simple command?

Upvotes: 1

Views: 2338

Answers (3)

jlliagre
jlliagre

Reputation: 30813

Unless file.txt is really large, xargs is unnecessary and this is equivalent:

rm $(<file.txt)

and portable (POSIX) too.

Upvotes: 2

mshildt
mshildt

Reputation: 9342

I believe you want:

xargs -a file.txt rm

The last argument to xargs should be the command you want it to run on all of the items in the file.

The solution proposed by Lynch is also valid and equivalent to this one.

Upvotes: 5

Lynch
Lynch

Reputation: 9474

Try this command: xargs rm < file.txt

xargs take every line in input and append it to the command you specify. so if file.txt contains:

a
b

then xargs will execute rm a b

Upvotes: 3

Related Questions