Search code examples
linuxbashgrepxargsrm

What is wrong in following approach (rm + xargs)


Can anyone tell me what is wrong in following way of file deletion which starts with "#".

Files in directory :

ola@ola:~/.scratch/hmm$ ls
five  #four  #one  six  #three  #two
ola@ola:~/.scratch/hmm$

ola@ola:~/.scratch/hmm$ ls . |grep "#.*" |xargs rm -rf
ola@ola:~/.scratch/hmm$ ls
five  six
ola@ola:~/.scratch/hmm$

Ps : Asking in reference of following question

Bash: How do I delete all my textfiles starting with #?


Solution

  • ls might list the files in several columns, and your regexp is not anchored to the beginning of the filename, either. Filenames containing whitespace will also be problematic.