Linux Colorize Find?

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?


Solution 1:

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:

find . -type f \( -perm +100 -printf '\033[32m%p\033[0m\n' -or -print \)

Solution 2:

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"

Solution 3:

find ... | grep -E --color 'words of interest|more good stuff|$'

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.

Solution 4:

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:

export GREP_OPTIONS="--color=auto"

Then, to highlight the text, simply do something like this:

find / -name "perl" |grep "your_string_here"

Solution 5:

I have one that I use, for example, to work as an (aliased) replacement for ls -d */ .*/ that skips the . and .. directories:

find . -mindepth 1 -maxdepth 1 -type d -exec ls --color=auto -d {} \;

this way I get not only highlighting, but the same system highlighting that would normally apply.