LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2012, 07:25 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
ls and LS_COLORS


Hi:

Is LS_COLORS the only environment variable affecting the behavior of the ls command?
 
Old 03-05-2012, 08:14 AM   #2
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
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.....
 
Old 03-05-2012, 08:21 AM   #3
lisle2011
Member
 
Registered: Mar 2011
Location: Surrey B.C. Canada (Metro Vancouver)
Distribution: Slackware 2.6.33.4-smp
Posts: 183
Blog Entries: 1

Rep: Reputation: 25
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.
 
Old 03-05-2012, 09:06 AM   #4
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
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.

Last edited by stf92; 03-05-2012 at 09:14 AM.
 
Old 03-05-2012, 09:40 AM   #5
lisle2011
Member
 
Registered: Mar 2011
Location: Surrey B.C. Canada (Metro Vancouver)
Distribution: Slackware 2.6.33.4-smp
Posts: 183
Blog Entries: 1

Rep: Reputation: 25
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.
 
Old 03-05-2012, 09:51 AM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
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.
 
Old 03-07-2012, 12:50 PM   #7
lisle2011
Member
 
Registered: Mar 2011
Location: Surrey B.C. Canada (Metro Vancouver)
Distribution: Slackware 2.6.33.4-smp
Posts: 183
Blog Entries: 1

Rep: Reputation: 25
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.

Last edited by lisle2011; 03-07-2012 at 12:51 PM. Reason: letter replacement
 
Old 03-07-2012, 12:54 PM   #8
lisle2011
Member
 
Registered: Mar 2011
Location: Surrey B.C. Canada (Metro Vancouver)
Distribution: Slackware 2.6.33.4-smp
Posts: 183
Blog Entries: 1

Rep: Reputation: 25
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.
 
Old 03-07-2012, 03:19 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 03-07-2012, 04:15 PM   #10
lisle2011
Member
 
Registered: Mar 2011
Location: Surrey B.C. Canada (Metro Vancouver)
Distribution: Slackware 2.6.33.4-smp
Posts: 183
Blog Entries: 1

Rep: Reputation: 25
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.
 
Old 03-07-2012, 07:08 PM   #11
lisle2011
Member
 
Registered: Mar 2011
Location: Surrey B.C. Canada (Metro Vancouver)
Distribution: Slackware 2.6.33.4-smp
Posts: 183
Blog Entries: 1

Rep: Reputation: 25
ls and LS_COLORS

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Filename abbreviations in printenv LS_COLORS erdfields Linux - Newbie 3 11-23-2009 12:46 PM
zsh, dircolors, and LS_COLORS MikeDawg Slackware 1 12-08-2007 04:36 AM
login shell, 'ls', and LS_COLORS soldan Linux - General 5 03-21-2007 06:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration