Wednesday, November 17, 2010

Changing Directory Listing (ls) Color in Linux and Mac OS X

Saving it for future purposes in case the original article gets lost.

Original Post: http://wuhrr.wordpress.com/2009/04/25/changing-directory-listing-ls-color-in-linux-and-mac-os-x/#comment-3413
 

Problem
I practically “live” on the Linux command line and one thing that has been bugging me for so long that now I decided to do something about it. On my company’s Linux systems the ls command produces color output, which makes it easy to distinguish different file types. However, the directories are dark blue which makes it nearly impossible to see against the black background.
My Solution
To fix the problem, my research took me to the LS_COLORS environment variable and a command named dircolors. The solution is to set the LS_COLORS environment variable to control how the ls command chooses its color. I can set the LS_COLORS variable manually, but there is a better way which employes the dircolors command.
The dircolors command output commands to set the LS_COLORS environment. It also output the colors in human-readable format, allowing easy modifications.
To use the dircolors, the first step is to save the current settings into a file. From the terminal, I issued one of the following commands:
dircolors -p > ~/dircolors.txt
The next step is to edit the file ~/dircolors.txt. This file’s format is easy to understand and self-documented; I had no problem finding the file that begins with “DIR” and change the color to my taste.
Next, I try out the new color scheme:
eval $( dircolors -b ~/dircolors.txt ); ls # bash shell syntax
    eval `dircolors -c ~/dircolors.txt`; ls # C shell syntax
After finding the color scheme I liked, I saved it to my start up file:
dircolors -b ~/dircolors.txt >> ~/.profile # bash shell
    dircolors -c ~/dircolors.txt >> ~/.cshrc   # C shell
From this point on, I no longer have to put up with hard-to-see colors.
Solution for BSD Systems
On BSD systems, which includes the Mac OS X, the variable in question is LSCOLOR (note the lack of the underscore character). The format of this variable is different from the Linux’s LS_COLORS. The default is exfxcxdxbxegedabagacad. The value of this variable consists of pairs of characters; the first character is the code for foreground color, and the second is for background. Please consult the man page for ls on BSD for more information.
The Interactive Solution
While I like to do things the hard way to learn more about the inner-working of the OS, there is a more interactive way: point your browser here to an interactive online web application which assist you in setting the colors. After finding your desired color scheme, you still have to cut and paste it to your start up file.
Additional References
Google is your friend: do a search for LSCOLORS or LS_COLORS will result in more information that you ever care to read.

Thursday, August 12, 2010

Number of distinct users on linux/unix server

Today I was wondering if there was a way to figure out how many users were using a particular linux server. A simple thought that first came to my mind was couting the number of users in the output of 'ps -ef' command.

A simple 'wc -l' would give the number of lines in the output of 'ps -ef'.

The next step would be to just get the first column to get rid of the duplicate usernames.
'ps -ef | cut -d' ' f1' gave me the required output. Now the next step was to get only the unique entries. And the 'uniq' built in utility came to rescue.


The final command : 'ps -ef | cut -d' ' -f1 | sort | uniq | wc -l'

08/14/2010: I just realised that sort has an inbuilt option of unique
Instead of redirecting the out of sort to unique, one can use 'sort -u'

08/14/2010: Also instead of using 'ps -ef' command, one can use 'w'
Note: This command takes into account of the header row too. So the count would be count-- :)
08/14/2010: grep has an inbuilt option of '-v'which allows for invert matching. So the previous problem noted can be removed by adding 'grep -v UID'



The revised final command : 'w| cut -d' ' -f1 | sort -u | grep -v USER | wc -l'

08/14/2010: One final issue that I still see is the first line is taken into account here apart from the header.
 17:54:01 up 39 days, 10:20, 359 users,  load average: 0.59, 0.28, 0.15
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT