ffind
A sane replacement for command line file search
I tend to use the UNIX command line A LOT. I find it very comfortable to work when I am developing and follow the “Unix as IDE” way. The command line is really rich, and you could probably learn a new different command or parameter each day and still be surprised every day for the rest of your life. But there are some things that sticks and gets done, probably not on the most efficient way.
In my case, is using the command `find` to search for files. 95% of the times I use it, is in this form:
find . -name '*some_text*'
Which means ‘find in this directory and all the subdirectories a file that contains some_text in its filename’
It’s not that bad, but I also use a lot ack, which I think is absolutely awesome. I think is a must know for anyone using Unix command line. It is a replacement for grep as a tool for searching code, and works the following way (again, in my 90% usage)
ack some_text
Which means ‘search in all the files that look like code under this directory and subdirectories that contains the text some_text‘ (some_text can be a regex, but usually you can ignore that part)
So, after a couple of tests, I decided to make myself my own ack-inspired find replacement, and called it ffind. I’ve been using it for the last couple of days, and it integrates quite well on my workflow (maybe surprisingly, as I’ve done it with that in mind)
Basically it does this
ffind some_text
Which means ‘find in this directory and all the subdirectories a file that contains some_text in its filename’ (some_text can be a regex). It has also a couple of interesting characteristics like it will ignore hidden directories (starting with a dot), but not hidden files, it will skip directories that the user is not allowed to read due permissions and the output will have by default the matching text in color.
The other use case is
ffind /dir some_text
Which means ‘find in the directory ‘/dir’ and all the subdirectories a file that contains some_text in its filename’
There are a couple more params, but they are there to deal with special cases.
It is done in Python, and it is available in GitHub. So, if any of this sounds interesting, go there and feel free to use it! Or change it! Or make suggestions!
You may want to check pss too
https://bitbucket.org/eliben/pss
Yes, I know (and use) pss too 😉
Isn’t it what does grep -R /dir some_text? If not what is the difference?
I think that grep -R is to search in the content of files, and not in the filenames. I’ve tried to use that command and I haven’t been able to use it to search for a filename (I can be doing something wrong) What is the result you expect for that command?
Or, just use this one-line script, which I call ‘ack-find’ (or even just make it an alias):
#!/bin/sh
find . | ack-grep $1
That does the trick, but there are a couple of small differences.
That script searches also on the whole path, something that is deactivated by default on ffind. This limits the results, as usually, you are only interested in files and main directory, not all the files below.
Also, that script includes the hidden directories, that again, usually are not interesting when searching.
Of course, I made ffind for my personal use and it is tailored to my specific workflow and 90% use.
I stopped using ack when I switched from Bash to Zsh – it has recursive wildcards (I heard newer Bash has it too). So I do ‘ls **/*some_text*.py’, or I can press TAB to expand wildcard inline. Builtin Vim grep supports it too: ‘:vimgrep foo **/*.py’.
Grep is faster than ack.
Surely find . -regex ‘some_text’ is what you were looking for, or a slight wrapper around that?
Also, I would recommend, or even work together with, Ag:
https://github.com/ggreer/the_silver_searcher
If you already know of it, how does yours compare?
I didn’t know it (I’ll take a look), but it seems to be a way of searching text INSIDE the files. ffind search only filenames.
“ag -g” searches for filenames matching a pattern. It’s a good tool, supposed to be fast too.
Cool, and here’s my find, appropriately called ‘myfind’. GitHub link: https://github.com/amitsaha/myfind
Would you please upload it to PyPI so we can pip install ffind and start using it right away?
I’ll try to do it 😉
Sorry for the delay, I’ve just pushed it to PyPI 😉
Pingback: ffind is now available on PyPI | Wrong Side of Memphis
But since you seem to like ‘ack’ as tool I would also like to recommend the “-g” option which seems to exactly what this ffind tool is capable of:
-g REGEX Same as -f, but only print files matching REGEX
So, looking for a file named ffind.py:
ack -g “ffind\.py”
And, you could also restrict ack to look *only* for python-files that includes ffind.py in its name (as the above could, for instance, also return a java-file named “ffind.py.java”):
ack -g “ffind\.py” –py
Nice, I too use find a lot with my search term surrounded by wildcards
You might want to avoid hard-coding VT102 colour sequences. Those will make a mess out of any terminal types that use different sequences.
The proper way to do it is to use terminfo. I don’t know Python, but googling a bit revealed this which seems to do what you need: http://code.activestate.com/recipes/475116-using-terminfo-for-portable-color-output-cursor-co/
Just use an alias
Performance of this tool is nearly twice as slow as find.
– See https://github.com/jaimebuelta/ffind/issues/6
Pingback: ffind v0.8 released | Wrong Side of Memphis
Pingback: ffind v1.0.2 released! – Wrong Side of Memphis
Pingback: ffind v1.2.0 released! – Wrong Side of Memphis
Pingback: Notes about ShipItCon 2017 – Wrong Side of Memphis
Pingback: Package and deploy a Python module in PyPI with Poetry, tox and Travis – Wrong Side of Memphis