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"
jrod : 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.JPerkSter : You rock Jrod! Thanks!!!!From jrod -
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 \)
JPerkSter : Is there a way to only have it print what we're searching for?JPerkSter : [root@server /home]# find . \( -maxdepth 1 -iname "cpmove-*" -printf '\033[32m%p\033[0m\n' -or -print \)JPerkSter : This is matching everything, but only colorizing 'cpmove' filesGilles : @JPerkSter: That was the intent of my example. If you don't want to print everything, remove `-or -print`.From Gilles -
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"
From Stefan Lasiewski -
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.
From Dennis Williamson -
Hi all, 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}"; }
From Vitaly
0 comments:
Post a Comment