0

This question already has an answer here:

I have been told that it is unwise to parse the output of LS due to security precautions.

The adage used was "parse sparsely and dont eat parsley"

I was attempting to count the amount of files within a directory by using

ls -la | wc -l  

I was told to never, never do this but I cant grasp why.

I was told one reason is because a file name could be shuffled into another command by accident. For example, if a file was within the directory named '-l'.

I was also told about using '--', for example ls -la --. If I understand correctly this is supposed to tell bash where to signal the end of commands but even if you used this you still shouldn't parse the output of ls.

Why ?

marked as duplicate by jasonwryan, dhag, Christopher, don_crissti, Jeff Schaller Oct 18 '17 at 20:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • If it's only counting you can do it safely with ls -qa | wc -l but then, there are better ways to count items in a directory. – don_crissti Oct 18 '17 at 20:23
  • the problem with "why not parse LS" is the answers are so complicated I cant get anything out of them – Oscalation Oct 18 '17 at 20:28
  • Don can you share the better way to count items in a directory? I'm assuming its with the find command? – Oscalation Oct 18 '17 at 20:29
  • Search the site for "count files in a directory"... – don_crissti Oct 18 '17 at 20:31
  • 1
    The Bash Wooledge page on it is a bit more clear. And Why is looping over find's output bad practice? is fairly relevant, also. Hope that helps. – Wildcard Oct 18 '17 at 20:32

Browse other questions tagged or ask your own question.