Live data from Hacker News

A common mistake involving wildcards and the find command

blog.robertelder.org

41–50 of 148 posts

Re: A common mistake involving wildcards and the find command

#41
I almost appreciate the idea of writing a C program whose sole purpose is to show you the arguments send to it. That's some serious overkill.

But I think echo *.py would not just be easier, but more effective at demonstrating what your find command line will actually look like after shell expansion.

Re: A common mistake involving wildcards and the find command

#43
> You can type: man glob

Translation: you cannot type "man" followed by an asterisk because that would have required forethought in how one learns a programming language.

Argle: Hey Bargle, is that new bridge built to spec?

Bargle: It's like I always say, man: good enough for shell script manual operator discoverability.

Argle: Yeah, you're always saying that...

Re: A common mistake involving wildcards and the find command

#45
post #32

Another one I've seen a few times with find (although it is actually more an xargs thing than a find thing), usually not with any bad consequences at least, is something like this: find . -type f | xargs grep foo You expect to see all the "foo" lines from your files, each prefixed with the file name and a colon. And that's what you get most of the time. But sometimes you might get a foo line without the filename pref…

A “better” fix would be: find . -type f -exec grep -H foo {} +

Yeah, find ¦ xargs is definitely an antipattern, but in practice I use it all the time. Either because I forget the syntax for find -exec, or because I'm feeling principled that find -exec breaks the Unix philosophy of doing one thing well and being composable, or because my script started using ls and got changed to use find.

Re: A common mistake involving wildcards and the find command

#46

Tangential: another safeguard you can adopt is avoiding "hard delete" commands like rm and find -delete. Untrain yourself from these commands by never using them. On Mac systems, the "trash" program (brew install trash) sends files to your system trash. You can use `trash [file]` and `find .. -print0 | xargs -0 trash --`. rm is a dangerous command you should only very rarely be using. I fish something out of the tras…

Trash is the poor man's backup system.

Re: A common mistake involving wildcards and the find command

#47
post #31

This was obvious to me, but one version of this that surprised me is when using scp. If you glob a remote destination like "scp myserver:*.jpg ./" It will probably work! But how? Because the remote path will likely not match any local files and the path with the asterisk will be passed to scp and scp will do the globbing on the remote side.

I mostly use ssh and tar instead of scp, because I'm usually after more than one file. Like, "ssh remote 'cd /whatever; tar -cf - someglobpattern' | tar -xvf"

Tip: get into the habit of using && rather than ;

So...

> ssh remote 'cd /whatever && tar -cf - someglobpattern' | tar -xvf

Re: A common mistake involving wildcards and the find command

#50
post #45

Earlier quoted context omitted.

A “better” fix would be: find . -type f -exec grep -H foo {} +

Yeah, find ¦ xargs is definitely an antipattern, but in practice I use it all the time. Either because I forget the syntax for find -exec, or because I'm feeling principled that find -exec breaks the Unix philosophy of doing one thing well and being composable, or because my script started using ls and got changed to use find.

xargs is also more efficient than -exec, as it stuffs multiple files to the target command. Esp. for rm, instead of executing a copy for each file.
Post reply on HN