23 Comments

$ touch '--no-preserve-root / foo' 'foo'

$ rm $(ls | grep foo)

Expand full comment
author

I've literally never found this in a real life file system, why should I worry about it?

Expand full comment

I've never used xargs. And I usually need to put it in a list that I'll read several times, like:

```

list=$(whatever) && fuser $list || rm -- $list

```

or

```

for i in $(whatever) ; do fuser "$i" || rm -- "$i" ; done

```

for example to be sure that there's no open files there (I work with databases)

Expand full comment

$ echo "bad bash" > secret.txt

$ echo "" > "-f foo && cat secret.txt"

$ ls | grep foo | sed 's/.*/rm &/' | bash

bad bash

$ echo "" > "-f foo && rm -rf ~/"

$ ls | grep foo | sed 's/.*/rm &/' | bash

Why would you even expose yourself to these risks? why not just use 'nix commands properly instead of trying to do things in an overly complicated, unsafe way?

Everyone should completely disregard all advice in this article. Use `find` and globbing instead to do your mass-deleting.

Expand full comment

None of this is good advice. You should use the find command.

Expand full comment
author

Please elaborate.

Expand full comment

Don't pipe ls! https://mywiki.wooledge.org/ParsingLs . This is what globbing is for.

Expand full comment
author

I usually detox (https://linux.die.net/man/1/detox) my FS so I don't have to worry about these subtleties.

Expand full comment

and yet you write an article that others might read, such that it becomes their worry instead? Nice.

Expand full comment

sed | sh; cmd $(cmd) is harmful, see comment https://news.ycombinator.com/item?id=27861635

Expand full comment
author

I usually detox (https://linux.die.net/man/1/detox) my FS so I don't have to worry about these subtleties.

Expand full comment

xargs -P <numprocs> will run commands in parallel until they're all done. That's why I still break it out.

Expand full comment

"I love xargs -L. It’s really great for grouping lines. No other program does it."

Ah ?

╰─○ seq 10 | xargs -L 3

1 2 3

4 5 6

7 8 9

10

╰─○ seq 10 | paste -d\ - - -

1 2 3

4 5 6

7 8 9

10

I think you're wrong .

Expand full comment
author

Maybe! I can't repro it locally, but others have talked about this

$ seq 10 | paste -d\ - - -

1 2

3 4

5 6

7 8

9 10

$ seq 10 | paste -d\ - - - -

1 2-3

4 5-6

7 8-9

10 -

$ paste --version

paste (GNU coreutils) 8.30

Copyright (C) 2018 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

Written by David M. Ihnat and David MacKenzie.

Expand full comment
Jul 19, 2021Liked by Tyler Adams

Hi Tyler , my version of paste is the same "paste (GNU coreutils) 8.32"

Be sure to type the command correctly

There are two spaces after the backslah '\' !

$ seq 10 | paste -d\ - - -

1 2 3

4 5 6

7 8 9

10

or this is working as well

$ seq 10 | paste -d' ' - - -

1 2 3

4 5 6

7 8 9

10

Expand full comment
author

Ah, that worked thanks for teaching me, this is great, I've updated and given you credit. I think substack might be eating the double space as there is only one space after the slash above.

Expand full comment

You really misunderstand the -I parameter. In your example of "echo seq 10 | xargs -IX X", the second X has to be a proper command and it is not substituted (i.e. you don't get 'seq 10' there). So you are trying to start the X window manager, which obviously doesn't work.

Perhaps re-reading the documentation of xargs might be in order. Perhaps don't fulminate against tools you don't understand.

Expand full comment
author

My point is that -I has such subtle, easily misunderstood behavior.

-I looks like a general purpose string replacer at first glance, but it's actually a more subtle special case one.

Expand full comment

Spoken like someone who's never hit the command length limit.

Expand full comment
author

If you're worried about rm $(..), then the sed | bash trick works perfectly fine.

Expand full comment

Unless there are spaces in filenames.

Expand full comment
author

That's an easy fix, sed 's/.*/rm "&"/'.

Or just detox the filesystem.

Expand full comment

no, just dont do this, please.

Expand full comment