Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

6 Answers 6

up vote 4 down vote accepted

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

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"

share|improve this answer
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. –  jrod Sep 13 '10 at 22:22
    
You rock Jrod! Thanks!!!! –  JPerkSter Sep 13 '10 at 22:33
    
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
1  
grep also discards non-matching lines :( Dennis Williamson's answer is roughly equivalent, though :D –  jrod Oct 29 '11 at 18:02

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"
share|improve this answer
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.

share|improve this answer
    
+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! –  Levit Jul 22 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. –  Levit Jul 22 at 7:33

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.

share|improve this answer

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}";
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.