456 points · by sharkdp · 9y ago · 215 comments
Thinking of stuff like fd and:
ripgrep: https://github.com/BurntSushi/ripgrep (grep/ag alt)
xsv: https://github.com/BurntSushi/xsv (csv tool)
exa: https://the.exa.website/ (ls)
una: https://github.com/jwiegley/una (multi-compression utils wrapper)
tokei: https://github.com/Aaronepower/tokei (loc stats)
And of course this: https://github.com/uutils/coreutils
Knowing the right incantations is useful in that context and keeps me from installing better tools until they become part of the distro we deploy with.
I wrote some of the tools on coldtea's list, and I'm pretty much the same way in that I stick to distro tooling in vanilla setups. But I spend enough time on my local workstation that having "better" (read: creature comforts) is worth it there! But yeah, if you spend most of your time in vanilla setups, then tools not in the standard distro repos are a hard sell.
That's why I wrote about it being nice to have them all in distros -- perhaps in a single package like coreutils.
Then wherever you are, they'd be just a "package-manager install rustutils" away.
Generally, if one is not an admin of random hodgepodge of systems (e.g. in big enterprises), then you are:
(1) doing work on your own workstation
(2) administering machines you control the provision (e.g. a person administering a startup's Cloud servers)
(3) doing development on some kind of vm
In those cases you can quite easily install a bundle package and make sure those tools are there.
I, for one, don't go out in unknown systems day after day -- even if we have 100s of servers we manage, we DO manage them.
I'm glad these tools work for you and I hope the community keeps making them, I just threw in my perspective.
It strikes me as an odd choice to accept the least common denominator of tools. As a technologist, that sounds frustrating. And as someone in leadership, I can’t imagine hamstringing my team by making them work within an environment that won’t let them use the best tools for the task at hand.
The popular package management tools rely heavily on FHS and like to install stuff into directories that require root permission.
Imagine if there was a tool that could download binaries of modern tools from a certain repo and install it to our ~/bin. Imagine if we could use new command line tools as easily as we can download a fully functional complex applications securely into a browser tab just by typing its URL!
You can also add ~/lib (for example) to your LD_LIBRARY_PATH path if you wanted to install custom libraries and even have your own man page path too.
All of this is already possibly on Linux and Unix however I'm not sure it's something you actively want to encourage as allowing users to install whatever they want on servers lowers the security to the level of the worst user on that server. If it was really deemed necessary that users should be able to install whatever they want then I'd sooner roll out a dedicated VM per user so at least their damage is self-contained (barring any visible networked infrastructure)
It even used to be easier to make stuff run from your homedir. We are moving from it, not towards it. It is really a shame for the few multi-user setups out there.
That way I don't even have to learn the new command (or risk forgetting about ls / be frustrated when I can't use it).
jq: https://stedolan.github.io/jq/ (lightweight JSON client)
On the other hand, you may want to keep some smart dotfiles around that installs them in ~/bin
I use "x" in oh-my-zsh plugins instead of una. https://github.com/robbyrussell/oh-my-zsh/tree/master/plugin...
I use "ag" instead of ripgrep. https://github.com/ggreer/the_silver_searcher
I use "fpp" instead of vgrep. https://github.com/facebook/PathPicker
Anybody has tried them and has an opinion on which is better?
One could argue that tool like this makes no sense since make is installed almost everywhere but I'm already using fish shell so one more non standard tool makes no difference. I appreciate simplicity of "just".
Consider also the "fuzzy finder" FZF.
Httpie is my goto http cli for all systems. Pretty much PostMan (that a lot of ppl use for no reason) but without annoying UI and on a single line. Formats JSON and colors it right in the terminal as well. Has a super nice query, header and post body syntax. Works well with sessions and cookies.
I can't see anywhere in the httpie docs about capturing variables from and/or running tests on the HTTP responses - it's an extremely powerful feature of Postman when you're debugging non-trivial HTTP flows.
$ userId=$(http get https://jsonplaceholder.typicode.com/posts/1 | jq '.userId')
$ http get "https://jsonplaceholder.typicode.com/users/$userId" | jq '{name,email}'
{
"name": "Leanne Graham",
"email": "Sincere@april.biz"
}Was this a thing back when the BSD and GNU greps were being developed? Was it common for people to have huge directory trees (e.g., `node_modules` or `.git`) lying around that were causing significant search slow downs? Not sure, but it seems to have taken a while for it to become a popular default!
There are of course other tricks, and most of those are inspired by changes in hardware or instruction sets. For example, back in the day, a correctly implemented Boyer Moore was critical to avoid quadratic behavior by skipping over parts of the input. But that's lessish important today because SIMD routines are so fast that it makes sense to optimize your search loop to spend as much time in SIMD routines as possible. This might change how you approach your substring search algorithm!
... so what's my point? My point is that while sometimes optimizations are classic in the sense that you just need to change your heuristics as hardware updates, other times the optimization is in the way the tool is used. Maybe you can squeeze the former into existing tools, but you're probably not going to make much progress with the latter.
I remember when I first put out ripgrep. Someone asked me why I didn't "just" contribute the changes back into GNU grep. There are a lot of reasons why I didn't, but numero uno is that the question itself is a complete non-starter because it would imply breaking the way grep works.
>Was this a thing back when the BSD and GNU greps were being developed? Was it common for people to have huge directory trees (e.g., `node_modules` or `.git`) lying around that were causing significant search slow downs? Not sure,
Sure, actually. It was called “build” and it was moved out of source code hierarchy to not interfere with tools. Pretty clever, and you don’t have to patch every tool each time new build-path appears. This should lead us to some conclusion, but I cannot figure out which. Do you?
gz-sort: http://kmkeen.com/gz-sort/
The only reason for using ack these days is if you’re already invested in a Perl workflow and can use some of the Perl magic powers, and don’t care about comparatively poor performance. That’s not many people.
Other reasons that you may want to use ack:
* You want to define your own filetypes
* You want to define your own output using Perl regexes
* You need the portability of an uncompiled tool in a single source file that you can drop into your ~/bin when you can't install or compile anything on the box.
* You don't want to have to deal with a Rust runtime.
So I can have regex like 'abc\K(def)(?=ghi)'. That will highlight ONLY 'def' in the text but only if that string is preceeded by 'abc' and 'ghi' follows.
- ripgrep cannot do that as it does not support PCRE
ag support --passthrough, but I haven't yet felt the need to try that as ack --passthru has been baked into my workflow and aliases for ages.
I use ripgrep (rg) otherwise by default.
The absense of -delete, -execdir, -mtime, and the ability to chain multiple patterns together for the non-trivial use-cases, means this is practically useless in most places where `find` is used in day-to-day work. Not to mention the "opinionated" choice to ignore a dynamic set of files and directories (what files were skipped? In any directory, or just git repos? Does it back track to find a git directory and .gitignore? Does it query out to git? Does it respect user/global .gitignore settings? Does it skip Windows hidden files from a unix prompt, or a dotted file when on Windows?), the options hidden behind two separate flags.
Perhaps it's just because I'm used to using 'find', but when I reach for it, it's because I need -delete or -execdir, or I'm writing automation that really needs -mtime and chained patterns.
So, I would suggest that you don't call this an alternative to find; it's not. A replacement for shell globs, sure. A replacement for poor file finding in text editors, OK. Just... not find.
EDIT: Oh, 'fd' also already exists. https://github.com/knu/FDclone
> So, I would suggest that you don't call this an alternative to find; it's not. A replacement for shell globs, sure. A replacement for poor file finding in text editors, OK. Just... not find.
It's literally called "a simple [..] alternative to find" and the very first sentence in the README says that "it does not seek to mirror all of find's powerful functionality". I'm not sure anymore how far I have to back off until everyone will be okay with my "advertising" :-)
Maybe advertize it as "a better ls -R | grep -i"?
Yeah except in most places where `find` is used in day-to-day work it is just for `find -iname 'foo'`. Anything fancier is probably less than 10% of uses. Here is my .bash_history:
find .
find .
find
find .
find . -name "debug.csv"
find .
man find
find . -type f
find . -iname "signal"
I completely agree with that.
And would in fact go slightly further: one of my huge annoyances with `find` is that I can never get alternative (-o) non-trivial patterns to work correctly, I think there's room for an alternative tool here but it should learn from set-theoretic API and make non-trivial predicates easy to write and test. Something like mercurial revsets — using fileset patterns and predicates instead of revs obviously — would be a godsend.
That has bitten me a few times when using `rg`. Sometimes (but not often enough to remember the switch) I want to grep a lot of binary .so files for a string and am left wondering why nothing was found before just replacing rg with grep. Just calling `TOOL PATTERN` without switches is the most intuitive thing, and there I agree with `fd` to make `fd PATTERN` the default. Unlike BSD find which always needs at least a `.` to function.
> chained patterns.
The -or/-and and are indeed nice, but the -prune logic should get an update. Maybe when fd grows from 80 to 90% of use cases.
Protips: `rg -u` stop looking at `.gitignore` files. `rg -uu` searches hidden files/dirs. `rg -uuu` searches binary files.
$ grep FOO *
Binary file libBar.so matches
foo.txt:file with FOO in it
rg/rg -u/rg -uu/ ignore libBar.so completely, whereas rg -uuu spits out binary garbage.Here grep's default behavior is what I expected: I want to know which binary files contain some symbol, details are less important (yes, I could use objdump or smarter tools, but grep is good enough 99% of the cases).
rg -u should list the matching binary file, maybe rg -uu should walk a few (printable!) chars to the left and right of the binary match and -uuu should just assume it is text, i.e. the user is right and the auto-guess wrong.
That said, the good thing about open source is you don't have to use it. I imagine the opinionated choices they made are popular amongst that crowd who seem like the types who mainly use the terminal to code--hence the .gitignore awareness; so, I imagine it's ok for others to use it if they find it makes themselves more productive for what they do. But, the good news is fd won't become a default anytime soon for that reason, so I have nothing to worry about this affecting me.
This is only because it's so much harder to support every possible Linux distro. I actually don't use macOS on my own, but 'Homebrew' seems like a really nice package manager. (Also, the first install option is via "cargo" which is platform independent.)
> fd .py | xargs stat
Thank you sharkdp -- really nicely done. Appreciated.
Unless I’m missing something, that’s not supported with this tool.
'-exec' is not supported at the moment. It's definitely something I would consider adding.
We do have '-0'/'--print0' to separate results by NULL, so 'fd' can be used in combination with 'xargs'.
find -type f -exec sed -i s/foo/bar/ {} +
This only invokes the command twice for 100k files (assuming we can pass in 64k arguments to sed)Admittedly I wrote that nearly 20 years ago so I guess it doesn't invalidate your point :-)
- There's the issue of command line length, pipe too many files and xargs will just fail.
- There's predictibility. With -exec you get to see the entire command line structure, what is escaped, what is not, what names go where. With xargs, a couple of badly named files and you are out of luck.
- Find's syntax is just more general. Xarg is limited to pushing parameters into a script, while -exec can run whatever scrit you want, with single or multiple parameters, or flags, or whatever.
Performance is almost never an issue.
especially this quote:
> find's business is evaluating expressions -- not locating files. Yes, find certainly locates files; but that's really just a side effect.
I already know how to use it. It works.
> It's often painfully slower than piping through xargs
I've tried it. It has different semantics. First attempt always leads to errors and doing the wrong things. I always have to come up with some workaround to make things work as I expect they would (i.e. how -exec does things by default).
This lack of confidence also means I would never dare using xargs for anything remotely destructive (like rm).
What reason do I have not to use -exec? Why swap a perfectly perfect hammer for a broken screwdriver?
Besides: IMO the timesaving aspects of find is not strictly tied to how fast find runs. It's tied to what I can automate.
fd -0 '\.log$' | xargs -0 tail
Edit: Swore I had gotten ARG_MAX complaints out of xargs in the past. Replies are correct though. I'm wrong here...xargs adjusts on the fly. Perhaps long ago, or some obscure use of xargs?
what?
Side question: why is speed important? Which tools use fd to gain required performance?
No, fd does not aim to be a drop-in replacement for find. It was actually designed as a "user-friendly" (I know, I know...) alternative to find for most of the use-cases: "fd pattern" vs. "find -iname 'pattern'", smart case, etc. Also, in my view, the colored output is not just a way to make it look fancy, but actually helps a lot when scanning a large output in the terminal.
The speed is not hugely important to me, but I wanted it to be at least comparably fast to find (that's why I decided to try out Rust for this project). Otherwise, nobody would even consider using it.
I know that it will not be for everyone and I also know that it's a lot to ask someone to learn a new tool, but I'm happy if there are some people that enjoy using it. I certainly do ;-)
The GNU team did this when they added or changed behavior to the original tools. A lot of them have flags like -posix and such which give them a strict set of behaviors.
alias fd=“function _fd(){find . -iregex ‘.*’$1’.*’};_fd”
would suffice for these 99? (not tested, but hope you got the idea)* does fd find files by type, mtime, depth, logical operators? I missed that in readme. If not, maybe “just pattern” argument is not too fair at all.
I think we need a saner alternative to the man pages of the classic unix tools collection. Something less focus on the seldom used options, but more focus on the traps to avoid and the useful tricks. The bad quality of documentation is more annoying than the incongruous syntax of tools.
find -type d -empty -delete
find -type l -exec readlink -f {} +
etc. are not covered at all."fd filename" does exactly what I want, and what I'd expect. Gave me what I wanted on the first try.
For my purposes, find is fully replaced, with something actually usable. Small, simple utils are nifty.
ls src/**/*_test.go
It might be marginally slower than find or FD, but it usually doesn't matter because I usually deal with a small number of files.1. ls [star]/foo
2. ls [star]/[star]/foo
3. ls [star]/[star]/[star]/foo
4. "I guess it doesn't exist."
[star][star] would have been useful to know. But now I have fd, so that's nice. :-)
Edit: Silly markdown.
ls (some dir)/**/*.py
zsh: argument list too long
Yeah, I'll keep using findThat has certainly been my experience in the past when experimenting with this sort of thing, that more threads makes a lot of difference.
I did. You are right, multi-threading does not give a linear speed up, but it makes fd about a factor of three faster on my machine (8 virtual cores). With `--threads 1`, fd is on-par with 'find -iname', but still faster than 'find -iregex'.
The mysql-server repository[1] should be a fun one to try out, because of this:
$ wc -l .gitignore
3122 .gitignore
The combination of fast glob matching[2] and parallel traversal should be a boon.[1] - https://github.com/mysql/mysql-server
[2] - https://github.com/BurntSushi/ripgrep/blob/e7c06b92fb996adcb...
> time fd -HIe jpg > /dev/null
5,12s user 2,03s system 785% cpu 0,911 total
> time ag --depth 100 -uuu -g '\.jpg$' > /dev/null
0,95s user 1,66s system 99% cpu 2,628 totalrg -g '*.foo' --files
It can't even search specifically for directories, as far as I can see, nevermind searching for files/dirs with certain permissions, ages, etc.
It's also uselesss for non-interactive uses such as scripting.
I like fzf, but it's really not anywhere close to a complete find replacement.
I suspect the GP meant that fzf replaced the pattern 'find |xargs $binary' for small interactive use cases. It's much nicer to do '$binary <invoke fzf>'. I use fzf the same way with key bindings to select directories and files powered by find.
fzf just actually calls find by default, so a naked call to fzf will actually give you the same results as find | fzf.
Search specifically for directories, that's find -type d | fzf.
https://github.com/libarchive/libarchive/wiki/LibarchiveForm...
GNU tar is so limited, but unfortunately still the default on most systems.
EDIT: The idea being that if you install it with your system package manager it can have unzip etc. as dependencies so that's taken care of automatically.
> time fd -sIe jpg > /dev/null
1,24s user 0,77s system 758% cpu 0,265 total
> time ls ~/**/*.jpg > /dev/null
0,53s user 0,97s system 98% cpu 1,518 totalThe benchmarks that are mentioned in the README are performed for a "warm cache", i.e. I'm running one of the tools first to fill the caches. Then, I'm performing multiple runs of each tool (using bench for some nice statistics) such that both tools profit from the warmed-up cache.
I also perform other benchmarks where I clear the caches. In this case, 'fd' is usually even faster.
The scripts for both warm and cold cache are here: https://gist.github.com/sharkdp/4bc3e5f5ea9df2f29c02ede50634...
'bench' turns out to be this tool: https://github.com/Gabriel439/bench
It seems to be quite useful, and I was not aware of it, thanks! Would probably be nice to have it packaged in distributions...
Do you need to be in the current git directory for this to happen, or all git dirs that happen to be traversed?
And are you using an NFA based regex engine?
fd uses Rusts regex engine (https://github.com/rust-lang/regex) which is based on finite automata.
I would prefer to let the switch enable the .gitignore logic, but as I don't know the authors use-case, theirs might be valid too.
So of course one does not trumpet that one's project is written in Python, C or Go.
n.b. I said "absolute sense" because all of this this is, of course, inapplicable when searching for libraries specifically for a language such as Python, C, or Go.