My distro is RHEL so I have a DIR_COLORS folder and an LS_COLORS variable. I know how to modify the colors. I also know how to check if a folder is a git repo. What I don't know is how to have ls change the color of the directory if that folder is a repo (default is blue, the repo folder would be purple or something).

Here's my attempt at bad sudo code

for d in ls
    if git_repo($d)
        LS_COLORS = git_colors
        LS_COLORS = normal
    print ls[$d]

I know that probably doesn't make "legal" sense but hopefully you get the gist. I know I can manually do it by echo'ing it out in a single column and manually changing the text color (in theory) but I want it to look like a normal ls with multi columns.

If there's a better way I'll take it. I also want to throw this into my bashrc into a function so I can alias ls to that function


Git prompts

Rather than roll your own here I'd recommend using what's provided by the Git project out of the box.

On a CentOS 7.x box if you yum install git everything is already included with Git to do both commandline completion + the git prompt.

$ rpm -ql git|grep completion

Here's the steps to do this manually, they'd need to be added your ~/.bash_profile to make them permanent.

$ . /usr/share/git-core/contrib/completion/git-prompt.sh
$ mkdir somedir && cd somedir
$ git init
$ PS1="$GREEN\t$RED-$BLUE\u$YELLOW\w\[\033[m\]$MAGENTA\$(__git_ps1)$WHITE \$ "

Doing the above will set your prompt so that it goes from this:

[root@centos7 somedir]#


13:18:42-root/home/vagrant/somedir (master) $

NOTE: it's now displaying the branch that I'm currently on in this repo, in my prompt.

Where's the colors?

To get this colored you need to define the colors so that the variable PS1 for the prompt's color variables have corresponding escape codes for their respective colors:


Now reset the PS1 again to pick these up:

13:24:26-root/home/vagrant/somedir (master) $ PS1="$GREEN\t$RED-$BLUE\u$YELLOW\w\[\033[m\]$MAGENTA\$(__git_ps1)$WHITE \$ "
13:24:35-root/home/vagrant/somedir (master) $

And it'll look like this now:


And what about changing the color of ls?

If is probably something I'd discourage your from because you need to then run a git status against every directory in each ls command to determine if it's a Git repo or not.

Imagine this:

$ ls somedir

would result in 7 git status commands to determine which directory is a Git repo and which is not, so that you can decide whether to color it as purple or not.

  • This is definitly useful for if I'm inside a repo. However my question is specific to if I'm in a parent directory where I have folders that some are cloned repos and some arent. From what I could tell you don't neccessarily need to do a git status against each one. What I've done in the past is cd, do if [ -d .git ] , and then you know if it's a repo
    – Excal
    Jul 13 '18 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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