LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ls and LS_COLORS (https://www.linuxquestions.org/questions/linux-newbie-8/ls-and-ls_colors-932805/)

stf92 03-05-2012 07:25 AM

ls and LS_COLORS
 
Hi:

Is LS_COLORS the only environment variable affecting the behavior of the ls command?

Satyaveer Arya 03-05-2012 08:14 AM

On FreeBSD, ls shows colors if the CLICOLOR environment variable is set or if -G is passed on the command line. The actual colors are configured through the LSCOLORS environment variable. For example to show directories in light blue, use :-

Code:

export LSCOLORS=Exfxcxdxbxegedabagacad
Here e means dark blue, E means light blue, x means default, f in the third position makes symbolic links magenta, and so on.

With GNU ls, e.g. on Linux, ls shows colors if --color is passed on the command line. The actual colors are configured through the LS_COLORS environment variable, which can be set with the dircolors command.....

lisle2011 03-05-2012 08:21 AM

LS_COLORS and command ls and DIR_COLORS
 
From:
man ls
Using color to distinguish file types is disabled both by default and with --color=never.
With --color=auto, ls emits color codes only when standard output is connected to a terminal.
The LS_COLORS environment variable can change the settings. Use the dircolors command to set
it.

from:
info coreutils 'ls invocation'
(1) If you use a non-POSIX locale (e.g., by setting `LC_ALL' to `en_US'), then `ls' may produce output that is sorted differently than you're accustomed to. In that case, set the `LC_ALL' environment variable to `C'.

LS_COLORS; if enabled will be an environment variable.

At the prompt type `set`

the screen will fill with information and if set,LS_COLORS will be shown and any parameters will be obvious. The colors and other variables and information concerning colors will be found in /etc/DIR_COLORS if the file exists.

stf92 03-05-2012 09:06 AM

Then, lisle2011, according to your post, LS_COLORS and LC_ALL are the only two possible environment varibles than can affect ls. My question is in connection with the following: suppose I have

alias v='/bin/ls $LS_OPTIONS --format=long -h'

where I have
$ echo $LS_OPTIONS
$ -F -b -T 0 --color=auto

Now, I can issue

$ v -a

and the -a option will be added to those options already contained in v. v is a comfortable alias for my personal use. But once in a while I want to have the same kind of output given by v but without the -h option. Is it possible to temporarily dissable the -h option?

I think it's impossible except for the following workaround:
to have a script with a very short name that sets LS_OPTION to one of two predetermined sets of options.

EDIT:

A solution: v --block-size=1. --block-size=1 will override -h in v. The only drawback is that "--block-size=1" is 14 characters long, 14 characters to type.

lisle2011 03-05-2012 09:40 AM

LS_COLORS and alias changes
 
I have not tried it, so all I can say is that once an option is included in an alias you are stuck with it. This may not be true. But one also cannot invent options for command line programs. It seems a lot of work to make an alias then not want to have all the options included. My only suggestion would be a second alias that did not include the -h option.

ls -alh would work for me anyway. Or conversely ls -al.

stf92 03-05-2012 09:51 AM

In some OSs options to some commands can be both asserted or negated. For example, MS-DOS, DIR command. Here you can set a collection of switches (options) as default and, when issuing the command, negate one or more of the options in the collection.

lisle2011 03-07-2012 12:50 PM

ls and LS_COLORS
 
LC_ALL overides every other environment variable starting with LS_. Read about LS_ and LS_ALL yourself in your documentation. LC_ALL is not specific to ls but will affect how the listing is shown depending on locale.

To solve your problem you should turn your alias into a script and put that script in your PATH. Exclude the -h from the script but put some scripting in so that it works like this:

scriptname
alone will not use the human readable option

scriptname -h will use the human readable option.

You can unalias v and name your script v so the result is:

v - gives you a listing without human readable option

v -h will give you what you have now.

This based on the assumption that you can write a script. It needs to be only very simple. If you cannot manage it I'll help you.

Negating an option using ls is not possible except as I have illustrated above.

lisle2011 03-07-2012 12:54 PM

ls and ls options
 
I re-read one of your previous responses and you mentioned 14 letters being too many to type. If you wish to learn to use bash and the command line typing is a part of it. So just get used to having to type.

Tinkster 03-07-2012 03:19 PM

Ummm ... unless you've already gone through all the letters of the alphabet for
aliases, why don't you create alias 'w' w/o the -h?



Cheers,
Tink

lisle2011 03-07-2012 04:15 PM

ls and LS_COLORS
 
here is my solution for you, you can edit this file how you like.

touch v
chmod 0744 v

vim v

Paste this code into v

Code:

#!/bin/bash
# You do not need to put $LS_OPTIONS in the command string as it is already an environmental variable that has been set.
# For just one command line option you need only use $1

echo '/bin/ls -l $1'
/bin/ls -l $1

I only put the echo in so you could visualize. You can comment it out. If you want more options add more variables $2, $3, $4, etc. If only one option is on the command line the others will be ignored.

I hope this resolves your problem.

lisle2011 03-07-2012 07:08 PM

ls and LS_COLORS
 
PS I neglected to mention that $LS_OPTIONS does not get expanded in your alias.


All times are GMT -5. The time now is 02:30 AM.