Is it possible to 'colorize' the output from find?
I have a find that searches /home on all of my servers, and exec 'rm' certain files. As these are mixed in with my other find results, among other things, I'd like to colorize them.
Is this possible?
What I usually do to highlight scrolling commandline text is use this bash+perl function:
highlight() { perl -pe "s/$1/\e[1;31;43m$&\e[0m/g"; }
as such:
command | highlight "desired text"
With GNU find, you can use
-printf
instead of-print
to customize the way a file name is printed. (You can also do it with standard find, but in a roundabount way through-exec sh -c 'echo ...' {}
.) For example, the following command prints executable files in green and other regular files in the default color:The dollar sign makes it match the end of every line but has nothing to highlight so it outputs even lines without matches while highlighting other things you've listed.
This is similar to @jrods answer, but this doesn't require Perl. This works with GNU grep which is installed on Darwin, Linux & FreeBSD.
You could use
grep
with colors, and pipe your command through grep:Then, to highlight the text, simply do something like this:
I have one that I use, for example, to work as an (aliased) replacement for
ls -d */ .*/
that skips the.
and..
directories:this way I get not only highlighting, but the same system highlighting that would normally apply.
I'm using the following function (defined in ~/.{ba,z}shrc):
If colors in grep are on or auto, you can simply do a short grep pipe with the extended-regexp option and the dollar sign (to still display every line).
For example piped with a find search:
which will highlight every occurance of "readme" and still display the entire output of find.
Sidenote: If grep has colors off, you could either add the --color option to it (
grep -E --color ...
) as Dennis Williamson suggests, or set it for the session (export GREP_OPTIONS="--color=auto"
).