How to format the output of tree in linux so the colour scheme still applies?
Asked Answered
A

4

19

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.

Argumentation answered 25/1, 2017 at 11:1 Comment(6)
tree -C. And man tree.Wismar
Thanks @Wismar . Is there a way to set colourisation as default for tree without using flags?Argumentation
man tree says: which is colorized ala dircolors if the LS_COLORS environment variable is set and output is to ttyWismar
alias tree="tree -C" or set LS_COLORS as you want.Weariful
Just add this export CLICOLOR=1 and it will use default colors if LS_COLORS is unset.Prominent
But how should we set LS_COLORS? I set it like LS_COLORS=red but it doesn't work in this way.Pironi
B
27
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.

Bentinck answered 8/10, 2019 at 14:23 Comment(0)
O
3

I usually use tree with Level 2, so this my favorite alias:

tre='tree -CL 2'

enter image description here

Orford answered 17/9, 2020 at 17:0 Comment(0)
R
0

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:

Windows Environment Variables MacOS Environment Variables

[2] Remember that these changes require some sort of reload.

Room answered 4/8 at 11:13 Comment(0)
M
0

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.

Monostome answered 15/8 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.