8

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?

JPerkSter
  • 285
  • 3
  • 12

7 Answers7

7

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"

violet
  • 486
  • 3
  • 6
  • 1
    On my own boxes I put that function into .bashrc, when I'm working on other people's boxes, I just paste that as a one-liner. – violet Sep 13 '10 at 22:22
  • Nice. But why not use grep --color? :) It's available on gnu grep on all platforms for many years. – Not Now Oct 28 '11 at 18:54
  • 2
    grep also discards non-matching lines :( Dennis Williamson's answer is roughly equivalent, though :D – violet Oct 29 '11 at 18:02
6

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 \)
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.

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • +1 I really like that! If color for grep is on by default (or set to `auto`) it is a really short pipe for highlighting, that is available pretty much everywhere: `grep -E 'word|$'`. This should get more attention! – Levite Jul 22 '14 at 07:13
  • Even with typing `export GREP_OPTIONS='--color=auto'` ONCE (e.g. on other people's boxes, that don't have grep colors on by default), `grep -E 'i|$'` is still shorter than most of the other solutions here. – Levite Jul 22 '14 at 07:33
2

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"
Stefan Lasiewski
  • 23,667
  • 41
  • 132
  • 186
1

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.

0

I'm using the following function (defined in ~/.{ba,z}shrc):

HLCOLOR="\x1b[30;47m"
NOCOLOR="\x1b[0m"

#Usage: find [dir] [mask1] [mask2]...
#where maskN will become <<-iname '*maskN*'>> param for 'find'
#'dir' should be an existing directory. otherwise it will be recognized as mask.
function findm {
        local it cl sp;
        [ -d "$1" ] && cl="'$1'" && shift;
        for it in "$@"; do
                cl="${cl} -iname '*${it}*' -or";
                sp="${sp}${it}\\|";
        done;   
        cl="${cl%-or}";
        sp="s/\\(${sp%\\|}\\)/${HLCOLOR}\\0${NOCOLOR}/ig";
        eval find ${cl} | sed -e "${sp}";
}
0

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).

grep -E 'word|$'

For example piped with a find search:

find . -type f | grep -E 'readme|$'

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").

Levite
  • 121
  • 3