Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Jun 21 '13 at 17:43

This question came from our site for professional and enthusiast programmers.

    
Note that echo * doesn't list hidden files but echo * .* does. –  scai Jun 21 '13 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. –  gniourf_gniourf Jun 21 '13 at 11:42

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
Thank you very much. I'm going to explore all the points you touched. –  Lavya Jun 21 '13 at 11:20

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

rm -f `echo *`
share|improve this answer
3  
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 '13 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. –  gniourf_gniourf Jun 21 '13 at 11:39
    
@depesz add -- to solve this. rm -f -- $(echo *) –  Arie Shaw Jun 21 '13 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 '13 at 11:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.