Archive for the ‘Linux’ Category

In linux, the way to print a colored text in the terminal using a C program or any scripting is to exploit the terminal command according to the need.


#include <stdio.h>

#define red   "\033[0;31m"        /* 0 -> normal ;  31 -> red */
#define cyan  "\033[1;36m"        /* 1 -> bold ;  36 -> cyan */
#define green "\033[4;32m"        /* 4 -> underline ;  32 -> green */
#define blue  "\033[9;34m"        /* 9 -> strike ;  34 -> blue */

#define black  "\033[0;30m"
#define brown  "\033[0;33m"
#define magenta  "\033[0;35m"
#define gray  "\033[0;37m"

#define none   "\033[0m"        /* to flush the previous property */

int
main(void)
{
 printf("%sHello, %sworld!%s\n", red, blue, none);
 printf("%sHello%s, %sworld!\n", green, none, cyan);
 printf("%s", none);

 return 0;
}

The output of the above code looks like this in bash terminal:

The terminal command “\ 033[0m”, which is called here as ‘none’ is required to flush the previous setting (for example, strike or underline) otherwise this property will be applied to the remaining text, no matter whatever color we set.

Advertisements

.bashrc customizations

Posted: October 23, 2009 in Linux
Tags: , , ,

Any system file in Linux that ends with ‘rc‘ ( rc stands for run command ) will have startup instructions for the associated application.
.bashrc, .vimrc, .mailrc, .chsrc etc are few examples.

Here are some useful customizations in .bashrc file. Once .bashrc is edited, to see its effect, it is needed to run the command :

$source ~/.bashrc

1. grep ( global regular expression print )

alias grep='grep -i -n -R --color'

This alias is useful in displaying the grep result with more information.

2. to increase the history file size, add these four lines.

shopt -s histappend
shopt -s cmdhist
export HISTSIZE=100000
export HISTFILESIZE=100000

This will maintain enough information that are required in most cases.

3. cd followed by ls

Let us define a new function cdl

cdl()
{ command cd "$@" && ls; }

4. to find the size of current folder  du -sh

alias sd='du -sh'

5. to find the size of all files inside the current folder du -sh *

alias sall='du -sh *'

6. df -h is useful to find out how much free space is available in various partitions of the hard disk.