Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
2  
No. If so, ls had to read the file first, which slows down directory listing. –  warl0ck Feb 14 '13 at 9:05
    
You might find this useful - Colored Find output –  mtk Feb 14 '13 at 9:15
add comment

2 Answers

up vote 1 down vote accepted

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
share|improve this answer
    
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. –  Roman Kaganovich Feb 14 '13 at 13:16
    
oh. i see. the copy crapped something. i'll check this. –  xx4h Feb 14 '13 at 13:20
    
@ xx4h I think I got your idea. I'm going to implement it in tcsh inside foreach command. –  Roman Kaganovich Feb 14 '13 at 13:33
    
Not whitespace safe. –  Kevin Feb 14 '13 at 15:01
add comment

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.

share|improve this answer
add comment

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.