3

How can I colorize directories contain files with word INCORRECT inside?

Example:

I have 3 directories which contain log file.

dir_a/log
dir_b/log
dir_c/log

The file log in dir_c contain word INCORRECT Is it possible to get colorized ls output as following?

dir_a --> blue
dir_b --> blue
dir_c --> red
2
  • 2
    No. If so, ls had to read the file first, which slows down directory listing.
    – daisy
    Commented Feb 14, 2013 at 9:05
  • You might find this useful - Colored Find output
    – mtk
    Commented Feb 14, 2013 at 9:15

2 Answers 2

1

This should do the job:
"INCORRECT" in text inside of files inside of folders and files within given folder:

SEARCH_DIR="/var/log"
for i in $(find $SEARCH_DIR -maxdepth 1 -mindepth 1); do 
    DIR_CHECK=$(grep INCORRECT $i) 
    [[ "$DIR_CHECK" =~ "INCORRECT" ]]  \
        && echo -e "\e[00;31m$i\e[00m" \
        || echo -e "\e[00;32m$i\e[00m"
done

"INCORRECT in text inside of files inside of folders in the given folder":

SEARCH_DIR="/var/log"
for i in $(find $SEARCH_DIR -maxdepth 1 -mindepth 1 -type d); do 
    DIR_CHECK=$(grep INCORRECT $i)
    [[ "$DIR_CHECK" =~ "INCORRECT" ]]  \
        && echo -e "\e[00;31m$i\e[00m" \
        || echo -e "\e[00;32m$i\e[00m"
done

"INCORRECT" in name of folders and files in given folder:

SEARCH_DIR="/var/log"
for i in $(find $SEARCH_DIR -maxdepth 1 -mindepth 1); do
    DIR_CHECK=$(echo $i | grep INCORRECT)
    [[ "$DIR_CHECK" =~ "INCORRECT" ]]  \
        && echo -e "\e[00;31m$i\e[00m" \
        || echo -e "\e[00;32m$i\e[00m"
done

"INCORRECT" in name of folders in given folders:

SEARCH_DIR="/var/log"
for i in $(find $SEARCH_DIR -maxdepth 1 -mindepth 1 -type d); do
    DIR_CHECK=$(echo $i | grep INCORRECT)
    [[ "$DIR_CHECK" =~ "INCORRECT" ]]  \
        && echo -e "\e[00;31m$i\e[00m" \
        || echo -e "\e[00;32m$i\e[00m"
done
4
  • I've tested your solution but it is not working for me. For example the [[ "$DIR_CHECK" =~ "INCORRECT" ]] trying to execute the result of of expression and report error. Commented Feb 14, 2013 at 13:16
  • oh. i see. the copy crapped something. i'll check this.
    – xx4h
    Commented Feb 14, 2013 at 13:20
  • @ xx4h I think I got your idea. I'm going to implement it in tcsh inside foreach command. Commented Feb 14, 2013 at 13:33
  • Not whitespace safe.
    – Kevin
    Commented Feb 14, 2013 at 15:01
0

No, at least in the current implementations of ls. The maximum you can do is selecting colors depending on the file type (image, sound recording etc., based on file name mask), entry type (file, directory, socket etc.) and, partially, permissions (executable flags). You are not able to apply color for directory depending even on the files it contains, not saying about the content of those files.

You must log in to answer this question.

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