1

Why does echo * | rm -f not work? as per my understanding, echo * should list all the files in the directory and pipe should send this list as an argument to the rm -f command, which should delete the files. What am I missing?

2
  • Note that echo * doesn't list hidden files but echo * .* does.
    – scai
    Jun 21, 2013 at 11:16
  • 2
    @scai with echo .* you'll also have . and .. which isn't a good idea at all. With shopt -s dotglob, echo * will expand to all files, including the hidden ones, but not . nor ..; it will be safer. Jun 21, 2013 at 11:42

2 Answers 2

7

Pipe sends data to stdin. And not to arguments list. These are two very different concepts.

Arguments are what is in line after command line. Stdin is basically just like a file that is already opened, from which you can read.

Please also note that doing things like this: echo * | xargs rm -f is bad idea because it might do weird things in case of files with spaces in names, or with "-" at the beginning.

1
  • Thank you very much. I'm going to explore all the points you touched.
    – Lavya
    Jun 21, 2013 at 11:20
1

Pass output of echo * as arguments rather than stdin inputs from pipe.

rm -f `echo *`
4
  • 4
    This is very, very bad idea. Consider file named "-r ." Of course it would solve the problem the poster is asking, but doing this is just plain wrong.
    – user7385
    Jun 21, 2013 at 11:20
  • @depesz then rm -f -- $(echo *) will do the trick (if you have a smart enough rm). Otherwise rm -f $(echo ./*) will do. Jun 21, 2013 at 11:39
  • @depesz add -- to solve this. rm -f -- $(echo *)
    – Arie Shaw
    Jun 21, 2013 at 11:40
  • 3
    This solves parameter problem, but still can yield bad results in case of files with spaces in names. Really - there is no reason to use echo, and it will cause pain in any way.
    – user7385
    Jun 21, 2013 at 11:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .