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 ?
ls -qa | wc -l
but then, there are better ways to count items in a directory. – don_crissti Oct 18 '17 at 20:23ls
that can contain any bytes except the null byte or the slash. There is fundamentally no way to recover that into a list of filenames. The transformationls
does is non-reversible, even if it doesn't replace any nonprintable characters. When it replaces non-printable characters, you're in an even worse situation." – Wildcard Oct 18 '17 at 20:39