LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   *BSD (https://www.linuxquestions.org/questions/%2Absd-17/)
-   -   Trying to get colored output with ls on openBSD 6.4 (https://www.linuxquestions.org/questions/%2Absd-17/trying-to-get-colored-output-with-ls-on-openbsd-6-4-a-4175644018/)

That Random Guy 12-10-2018 01:15 PM

Trying to get colored output with ls on openBSD 6.4
 
Hello,

I have openBSD 6.4 installed on a VM which I've been using in an attempt to learn how to get file shares running on openBSD.

Quite frankly, I've taken for granted how colors pertaining to file type when ls is invoked or having aliases for lengthy commands is done in Linux.

In an attempt to get the same thing on openBSD, I went ahead and installed bash on my system.

I also changed my default shell to bash. I can only confirm this as whenever I logon now, I get bash instead openBSD's default.

When I echo $SHELL, I get the following path: /usr/local/bin/bash

When I echo $BASH_VERSION, I get 4.4.23(1)-Release

Under my home directory, I find the following files & folders:
Code:

.Xdefaults
.bash_history
.config
.cshrc
.cvsrc
.login
.mailrc
.profile
.ssh
.viminfo
mbox

Aside from the one's listed in blue, all other objects are files.

I've tried to see if there was a bashrc file anywhere but I don't know if that's the right file I should be editing now. I read somewhere that I could get what I'm asking for from other files, such as .bash_profile or even .profile.

I honestly don't know where to look at this point as I'm pretty new to BSD in general and I've never had to do this before on a Linux host.

Could I get some assistance?

TIA

EDIT:
I'm trying to get colors to function under the terminal (by that, I mean I want colored output). I already see color in instances where man is invoked and a man page is displayed. What I would like next is to see the colors I normally see when I invoke ls. On all Linux machines I've used in the past, I was able to run ls and the color scheme would be generated automatically. I don't know how to enable that same feature with ls on openBSD. I'm looking to achieve that, and my guess is to edit some config file, but I don't know if that's true.

EDIT #2:
This is all being done on an openBSD guest machine run via virtualbox. I am not trying to get this on a Linux machine 'cause I don't have to.

Didier Spaier 12-10-2018 05:49 PM

The files you mention (.bash_profile, .profile, you can add .bsh_aliases and .bashrc) are optional and generally written by the user.
Try this command to know more:
man bash
Aldo, you could read http://tldp.org/LDP/Bash-Beginners-G...ers-Guide.html (old, but mostly still valid).

ehartman 12-10-2018 08:11 PM

Quote:

Originally Posted by That Random Guy (Post 5935580)
I've tried to see if there was a bashrc file anywhere but I don't know if that's the right file I should be editing now.

The environment variable for enabling colors is LS_OPTIONS, with LS_COLORS giving what colors to use for which files.
They are normally set by a program 'dircolors', which in _MY_ system is invoked by a script in the /etc/profile.d directory (all of the .sh scripts in that directory are executed by /etc/profile), but you can create your own /etc/profile.d/dircolors.sh (or any other name with the .sh extension) to set those variables AND create aliases for 'ls' with the $LS_OPTIONS added/
Again, mine is:
Code:

/bin/ls --color=auto -T 0
but I changed it from the dircolors default one.

ondoho 12-11-2018 01:57 AM

That Random Guy, what is the problem?
i cannot make out a clear question in your op.

cynwulf 12-11-2018 04:22 AM

Quote:

Originally Posted by That Random Guy (Post 5935580)
Quite frankly, I've taken for granted how colors pertaining to file type when ls is invoked or having aliases for lengthy commands is done in Linux.

As I understand it, it's GNU coreutils ls(1) which has the this colorisation feature. As far as I know, it has nothing to do with the shell and is not configured in ~/.profile or anything similar. OpenBSD's ls(1) does not have this.

Install the coreutils package and you will be able to use the GNU ls ( gls(1) ) which supports this feature. Refer to the man page.

You can then alias it as your ls(1) command.

Quote:

Originally Posted by That Random Guy (Post 5935580)
I also changed my default shell to bash. I can only confirm this as whenever I logon now, I get bash instead openBSD's default.

When I echo $SHELL, I get the following path: /usr/local/bin/bash

When I echo $BASH_VERSION, I get 4.4.23(1)-Release

Be aware that changing your default shell to a shell which is installed from ports rather than the ksh(1) shell in base, could result in some breakage if the shell is removed. It's not a huge problem for a user account shell, but certainly do not change root's shell.

nodir 12-11-2018 04:55 AM

Quote:

I read somewhere that I could get what I'm asking for from other files, such as .bash_profile or even .profile.
Here is a list and an explanation about the files which configure the way bash acts:
http://mywiki.wooledge.org/DotFiles?...=%28profile%29
Quite some linux distributions come with a lot of configurations out of box, debian even autocreates .profile and .bashrc (also very complex ones) in the users home directory.
I sometimes copy them to distros or OS'es where i ain't got them.

In general i think you are asking about what ehartman said.

As cynwulf said you can't expect a different flavor of commands, though with the same name, to always work the exact same way (in this case the command "ls"). Always make sure the according manpage confirms what you know from Gnu/Linux.

Though it really ain't that clear what exactly your problem is (as ondoho said too).

That Random Guy 12-11-2018 08:17 AM

LS colors
 
Quote:

Originally Posted by ondoho (Post 5935742)
That Random Guy, what is the problem?
i cannot make out a clear question in your op.

I'm trying to get colored output for the ls command on openBSD as it is enabled by default on almost every Linux distro I can think of.

I initially also needed help with aliases but after playing with the .profile file, I was able to get that going.

I edited my OP so that others understand better as well. It was kind of vague.

That Random Guy 12-11-2018 08:29 AM

Yep
 
Quote:

Originally Posted by Didier Spaier (Post 5935653)
The files you mention (.bash_profile, .profile, you can add .bsh_aliases and .bashrc) are optional and generally written by the user.
Try this command to know more:
man bash
Aldo, you could read http://tldp.org/LDP/Bash-Beginners-G...ers-Guide.html (old, but mostly still valid).

I already looked through the man page for bash and its related pages as well. Didn't see anything there that would help--and if there was, it wasn't clear enough.

That Random Guy 12-11-2018 08:31 AM

Yikes!
 
Quote:

Originally Posted by cynwulf (Post 5935780)
As I understand it, it's GNU coreutils ls(1) which has the this colorisation feature. As far as I know, it has nothing to do with the shell and is not configured in ~/.profile or anything similar. OpenBSD's ls(1) does not have this.

Install the coreutils package and you will be able to use the GNU ls ( gls(1) ) which supports this feature. Refer to the man page.

You can then alias it as your ls(1) command.


Be aware that changing your default shell to a shell which is installed from ports rather than the ksh(1) shell in base, could result in some breakage if the shell is removed. It's not a huge problem for a user account shell, but certainly do not change root's shell.

I better change that now then. Thanks for the tip! :)

cynwulf 12-11-2018 08:51 AM

Quote:

Originally Posted by That Random Guy (Post 5935853)
I'm trying to get colored output for the ls command on openBSD as it is enabled by default on almost every Linux distro I can think of.

As OpenBSD is not GNU or Linux and OpenBSD's ls(1) command is not the same as the GNU ls(1) command, you will not get the colourisation.

As I've said above, you will need to install coreutils from ports and use the GNU gls(1) command to get the colorised output (just tested and works).

That Random Guy 12-11-2018 09:55 AM

Yep
 
Quote:

Originally Posted by cynwulf (Post 5935868)
As OpenBSD is not GNU or Linux and OpenBSD's ls(1) command is not the same as the GNU ls(1) command, you will not get the colourisation.

As I've said above, you will need to install coreutils from ports and use the GNU gls(1) command to get the colorised output (just tested and works).

Yep, and I understood that. I already had coreutils installed but wasn't aware of the differences between the distinct ls utils.

I was able to get an alias going for gls, but it doesn't seem to be working (i.e. colored output; the command does run). I'm thinking there's something else I haven't configured right, which would explain why even colorls wasn't showing colors when invoked.

I'll have to investigate some more. :study:

cynwulf 12-11-2018 10:12 AM

It worked here. Can you post the exact alias you used?

That Random Guy 12-11-2018 11:51 AM

!
 
Quote:

Originally Posted by cynwulf (Post 5935895)
It worked here. Can you post the exact alias you used?

I will list the contents of my .profile file below:

Code:

PATH=$HOME/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin:/usr/games:.

alias ls='gls'
alias la='ls -la'
alias cls='clear'

I can confirm that the aliases are being applied because I have run cls and it has worked. I have also run ls with the --color=auto option, which I think would otherwise fail with the BSD port.

According to the man page, it's assumed to leave color on always by default. So, I tried without the color option and also setting it to always.

Incidentally, I'm running this machine as a guest on Virtualbox. I don't know if that would have anything to do with it, though.

ehartman 12-11-2018 04:44 PM

Quote:

Originally Posted by That Random Guy (Post 5935939)
alias ls='gls'

That should be
Code:

alias ls='gls --color=auto'
and the LS_COLORS environment variable should be set in your system.

color=auto means: use color on an interactive terminal but NOT when ls is used within a script, crontab etc as the colors may disturb further processing (like in a ls|grep pipe).
See the man page of the GNU version of ls about using colors.
Note that displaying colors is DISabled by default so you do need to use the --colors option (with a value other then 'never').

That Random Guy 12-11-2018 07:11 PM

Got it
 
Quote:

Originally Posted by ehartman (Post 5936045)
That should be
Code:

alias ls='gls --color=auto'
and the LS_COLORS environment variable should be set in your system.

color=auto means: use color on an interactive terminal but NOT when ls is used within a script, crontab etc as the colors may disturb further processing (like in a ls|grep pipe).
See the man page of the GNU version of ls about using colors.
Note that displaying colors is DISabled by default so you do need to use the --colors option (with a value other then 'never').

I forgot to read that part in the man pageóbut after some trifle and a few Google searches, I was able to get it working.

It turns out my TERM variable wasn't one that supported colorization as prescribed in the dircolors man page. After changing that, and running gdircolors -b (and editing my profile), I've now received colored output when invoking ls.

What I need to figure out now is how to make the TERM change permanent. So far, I've only been able to do that via export TERM=vt100.

In any case, I'm marking this thread as solved.


All times are GMT -5. The time now is 11:38 AM.