How can I format the output of a tree command so that the formatting of ls, e.g. highlighting and colour scheme of directories and certain file types applies to the output? The problem is when I restrict the depth of tree to say 2, if there are any directories in the final level, there is not a noticeable difference between dirs and files. So someway to easily distinguish between the two, whilst displaying both files and dirs would be great. Any help would be appreciated.
tree -C
-C
Turn colorization on always, using built-in color defaults if the
LS_COLORS
environment variable is not set. Useful to colorize output to a pipe.
see man page.
I just had the same issue. An answer from the comments helped me.
TL;DR
export CLICOLOR=1
The description for tree
's -C
flag says:
Turn colorization on always.
But it doesn't. It just works for that particular invocation of tree
and that's it. But setting that environment variable did it for me. It sets the environment variable for the current terminal session.
If you want this environment variable to persist through multiple terminal sessions of the current user, you should edit the ~/.bashrc
or ~/.bash_profile
shell script of the current user. Open the file and append the code to the end of the file.
If on the other hand, you want this environment variable to persist through multiple terminal sessions for all users, you should edit the /etc/environment
file. Open the file and append CLICOLOR=1
to the end of the file. [1][2]
[1] The suggested solutions assume that the user is using a Linux distribution and the Bourne-Again Shell (bash). Other platforms have their ways of setting persistent environment variables for individual users and system wide. See Windows and MacOS:
[2] Remember that these changes require some sort of reload.
Nothing of the other answers helped me. For what ever reason I don't know, on a Debian platform I had a ~/.dircolors
file just containing
~$ cat .dircolors
OTHER_WRITABLE 00;33;40 # dir that is other-writable (o+w) and not sticky
That is nothing, no colors. It seems a runaway application has set its color instead of appending it to ~/.dircolors
. Finding this, it just helped me to delete ~/.dircolors
and login to a new shell. This answer to Who is setting my LS_COLORS? points me into the right direction. Getting the default color scheme now you can customize it.
© 2022 - 2024 — McMap. All rights reserved.
tree -C
. Andman tree
. – Wismarman tree
says: which is colorized ala dircolors if theLS_COLORS
environment variable is set and output is to tty – Wismaralias tree="tree -C"
or setLS_COLORS
as you want. – Wearifulexport CLICOLOR=1
and it will use default colors ifLS_COLORS
is unset. – ProminentLS_COLORS
? I set it likeLS_COLORS=red
but it doesn't work in this way. – Pironi