4 ms·
That's like saying you can just use `find . | grep ... | wc -l` and then learning the hard way that you can have newlines in filenames. While I agree you should
by herpderperator 7y ago
That's like saying you can just use `find . | grep ... | wc -l` and then learning the hard way that you can have newlines in filenames. While I agree you should learn lots of general tools, you should not stop there. If you have a particular need you should consult the manpage; it is one of the ways you become better. In the htop example it might be fine as a quick-and-dirty method, but when teaching others like through this particular blog post you should do so the right way.
- lixtra 7y agoIf your pipeline is meant to count files then the bigger problem is that it counts directories too. If you have filenames with newlines you may have other - less nasty - stuff too. So you either get a reasonable answer in 1s (or 2s if you have short look at the output before counting). Or you spend 1h+ discussing requirements and carefully writing a program that gives a precise answer. Things to consider: - how to treat symlinks - how to treat hardlinks - what if files are added/removed to/from the directory tree while you scan - how to react to missing read permissions - is your regex on the whole path or just on the basename - ...
- herpderperator 7y agoWell, in Unix, everything is a file[0], even directories. So when I mentioned filenames, this includes directories. :-) As a real-world example for those who are curious, running mkdir twice yields errno 17, EEXIST, which is "File exists"[1]: root@vbox:~# mkdir directory root@vbox:~# mkdir directory mkdir: cannot create directory ‘directory’: File exists But sure, the fast way could be mostly right, and maybe your goal is to just get it done and not get better at these tools, in which case, sure. [0] https://en.wikipedia.org/wiki/Everything_is_a_file https://en.wikipedia.org/wiki/Everything_is_a_file [1] https://elixir.bootlin.com/linux/latest/source/include/uapi/asm-generic/errno-base.h#L21 https://elixir.bootlin.com/linux/latest/source/include/uapi/...
- Florin_Andrei 7y ago> learning the hard way that you can have newlines in filenames Speaking as someone who is using Unix-like systems since the days of Santa Cruz Operation and Linux on a 50 MB disk partition - not everything that is permissible is auspicious. And most often that's because the permissions were too lax to begin with. That's what you get when a bunch of pot-smoking hippies (X) draw your OS specifications. I avoid using even spaces in file names, for this specific reason. --- (X) - and I say that in the best way possible, though some may be inclined to disagree. "...he and brethren were attempting to make a small fortune for themselves by secreting away an item - a printed book - that was easy enough to get in 1640 but near impossible to get in Tristan's age, making it of great worth." "So you're thieves and chancers," said I approvingly. "No," he objected. -- The Rise And Fall Of D.O.D.O. by Neal Stephenson and Nicole Galland