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?

7 Answers 7

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 \)
4
  • Is there a way to only have it print what we're searching for?
    – JPerkSter
    Sep 13, 2010 at 22:51
  • [root@server /home]# find . ( -maxdepth 1 -iname "cpmove-*" -printf '\033[32m%p\033[0m\n' -or -print )
    – JPerkSter
    Sep 13, 2010 at 22:52
  • This is matching everything, but only colorizing 'cpmove' files
    – JPerkSter
    Sep 13, 2010 at 22:52
  • @JPerkSter: That was the intent of my example. If you don't want to print everything, remove -or -print. Sep 13, 2010 at 23:12
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"

3
  • 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, 2010 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, 2011 at 18:54
  • 2
    grep also discards non-matching lines :( Dennis Williamson's answer is roughly equivalent, though :D
    – violet
    Oct 29, 2011 at 18:02
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.

2
  • +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, 2014 at 7: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, 2014 at 7: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"
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").

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .