8 ms·
The first example is super super bad here. Never pipe `ls`. When you feel like you need to pipe `ls`, then you know you want to use `find`.
by pkrumins 5y ago
The first example is super super bad here. Never pipe `ls`. When you feel like you need to pipe `ls`, then you know you want to use `find`.
- nixpulvis 5y agoI would recommend `find ... -exec`, but I still haven't figured out how to make it compose properly with other UNIX tools.
- revscat 5y agoYou may want to use file globbing instead. This is one I just used yesterday afternoon. I needed to search for a string in every .js or .jsx file in my project, but didn't want to include specs in the search. rg 'MySearchString' **/*.js[x]#~*spec* Voila. Note that this is for zsh, and you need to set the EXTENDED_GLOB option. But once you do you'll find yourself rarely needing to reach for `find`.
- nicce 5y agoIs it really better option? You can find "find" from every Linux system. For this sample, you would need to install zsh and enable globbing manually as well. It works only on your machine, but on the other hand there is chance learn something which applies everywhere.
- oweiler 5y agoTo be even more pedantic, you probably want to use a glob
- tzs 5y agoThere are a lot of times one only wants non-dotfiles in the current directory. The find would be something like find . -not -path '*/\.*' -type f -depth 1 What advantages does that have over 'ls' for that case?
- jasode 5y ago>What advantages does that have over 'ls' for that case? The gp was talking about issues with piping from "ls |" and your particular case of "find" being more convoluted than "ls" isn't comparing that. Example of the topic that gp was warning about: http://mywiki.wooledge.org/ParsingLs http://mywiki.wooledge.org/ParsingLs https://unix.stackexchange.com/questions/128985/why-not-parse-ls-and-what-to-do-instead/129120#129120 https://unix.stackexchange.com/questions/128985/why-not-pars... [Also fyi... you may have meant "-maxdepth" instead of "-depth" in your example.]
- revscat 5y agoFYI the zsh glob for this is `^.*`, e.g.: echo ^.* will show all non-dotfiles in the current directory.
- inshadows 5y agoIn my Zsh echo * does not expand dotfiles. I guess there's some setopt for that.
- revscat 5y agoYou're right. Bored during meeting, go this to work: echo (*)#.* This prints all dot files and regular files in the current directory. You do need to have `setopt EXTENDED_GLOB` somewhere in your zsh config.
- DoingIsLearning 5y agoNever? ls |less
- pkrumins 5y agoNice! This is pretty the only exception. ls|more or ls|less.
- renewiltord 5y agoI solve this problem by just having no spaces on my filesystem when I act.
- devenvdev 5y agoYes, as I answered in another thread - this post is much more educational than practical. It's intended to teach how to use pipes and simple commands together. Explaining why piping `ls` is bad and what is the difference between `ls` and `find` commands would miss the point of the post and would be confusing :)
- MisterTea 5y ago> this post is much more educational than practical. I understand that you don't want to be mean but this post is neither. It's like a bad gun safety video where the alleged instructor points a loaded gun at school children and then looks down the barrel while polishing the trigger...
- zuhsetaqi 5y agoCould you explain why piping `ls` is so bad? Thanks!
- devenvdev 5y agoThis[1] has some good explanations, but as an example - spaces, special characters, etc in file names. [1] https://unix.stackexchange.com/questions/128985/why-not-parse-ls-and-what-to-do-instead https://unix.stackexchange.com/questions/128985/why-not-pars...