Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Jul 27, 2011

BASH Shell change the color of shell prompt under Linux or UNIX


You can change the color of your shell prompt to impress your friend or to make your own life quite easy while working at command prompt.
In the Linux default shell is BASH.
Your current prompt setting is stored in PS1 shell variable. There are other variables too, like PS2, PS3 and PS4.
Bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters.

Task: Display current BASH prompt (PS1)

Use echo command to display current BASH prompt:
$ echo $PS1



Output:
[\\u@\h \\W]\\$

By default the command prompt is set to: [\u@\h \W]\$. Backslash-escaped special characters are decoded as follows:
  • \u: Display the current username
  • \h: Display the hostname
  • \W: Print the current working directory

Task: Modify current BASH prompt

Use export command to setup a new shell prompt:


$ export PS1="[\\u@\\H \\W \\@]\\$"


Where,
  • \H: Display FQDN hostname
  • \@: Display current time in 12-hour am/pm format

Task: Add colors to the prompt

To add colors to the shell prompt use the following export command syntax:
'\e[x;ym $PS1 \e[m'

Where,
  • \e[ Start color scheme
  • x;y Color pair to use (x;y)
  • $PS1 is your shell prompt
  • \e[m Stop color scheme
To set a red color prompt, type the command:

$ export PS1="\e[0;31m[\u@\h \W]\$ \e[m "

List of Color code

Color Code
Black 0;30
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33
Replace digit 0 with 1 to get light color version.

Task: How to make the prompt setting permanent

Your new shell prompt setting is temporary i.e. when you logout setting will be lost. To have it set everytime you login to your workstation add above export command to your .bash_profile file or .bashrc file.
$ cd
$ vi .bash_profile
OR
$ vi .bashrc
Append export line:
export PS1="\e[0;31m[\u@\h \W]\$ \e[m"

Save and close the file.

tput command

You can also use tput command. For example display RED prompt use tput as follows:
export PS1="\[$(tput setaf 1)\]\u@\h:\w $ \[$(tput sgr0)\]"

handy tput commands

  • tput bold - Bold effect
  • tput rev - Display inverse colors
  • tput sgr0 - Reset everything
  • tput setaf {CODE}- Set foreground color, see color {CODE} below
  • tput setab {CODE}- Set background color, see color {CODE} below

Colors {code} code for tput command

Color {code} Color
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White


Read the man page of bash and tput command for more information.

Jun 22, 2011

Converting Unix to Windows and back


 In vim, use :set ff=unix to convert to Unix; use :set ff=dos to convert to Windows.

Remove ^M characters at end of lines in vi


:%s/{Ctrl+V}{Ctrl+M}//{Enter}

May 19, 2011

Recursively download entire websites


wget -r -p -U Mozilla http://www.mywebsite/thedownloadurl.html

The most important command line options are --limit-rate= and --wait=. You should add --wait=20 to pause 20 seconds between retrievals, this makes sure you are not manually added to a blacklist. --limit-rate defaults to bytes, add K to set KB/s. Example:

wget --wait=20 --limit-rate=20K -r -p -U Mozilla http://www.mywebsite/thedownloadurl.html

Use --no-parent

--no-parent is a very handy option that guarantees wget will not download anything from the folders beneath the folder you want to acquire. Use this to make sure wget does not fetch more than it needs to if just just want to download the files in a folder.

May 18, 2011

Find file in folders recursively

Case sensitive
find . -name "index.*" -type f -print

Case insensitive :
find . -iname "index.*" -type f -print

Apr 12, 2010

Trapping SIGNAL in Bash

#!/bin/bash
# bash trap command
trap bashtrap INT
# bash clear screen command
clear;
# bash trap function is executed when CTRL-C is pressed:
# bash prints message => Executing bash trap subrutine !
bashtrap()
{
echo "CTRL+C Detected !...executing bash trap !"
}
# for loop from 1/10 to 10/10
for a in `seq 1 10`; do
echo "$a/10 to Exit."
sleep 1;
done
echo "Exit Bash Trap Example!!!"

Backing up folder in Bash

#!/bin/bash
OF=myhome_directory_$(date +%Y%m%d).tar.gz
tar -czf $OF /home/linuxconfig

Dec 16, 2009

Summing intergers in a file

Method : 1

awk '{s+=$0}END{print s}' file

Method : 2

echo $(tr "\n" "+" < file)0|bc

Replacing newline with comma


cat 'FILENAME'|awk -F'\n' '{for(i=1;i<=NF;i++) printf "%s,", $i}'

Dec 3, 2009

Using xargs for rm & cp


Remove all files with .bak extension -

find . -name "*.bak" -type f -print | xargs /bin/rm -f

Remove all files with .bak extension who were last modified before today -

find . -mtime +0 -name "*.bak" -print | xargs rm

Copy all .html file to /tmp/movedhtml -

find . -name "*.html" -exec cp -p "{}" /tmp/movedhtml \;

Nov 12, 2009

Kill all same name spawn process in one go


Syntax :


ps -aef | grep -v grep | grep PROC_NAME | awk {'print $2'} | xargs kill -9


E.g :

ps -aef | grep -v grep | grep test.pl | awk {'print $2'} | xargs kill -9

JOIN FILES

SYNTAX :

join -t
'DELIMITER' -1 FILE_1_join_column -2
FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE

join -t
'\t' -1 1 -2 2 a.txt b.txt > a_b.outDELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE

join -t
'\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.outDELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE

join -t
'\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.outDELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE

join -t$'\t' -1 1 -2 2 a.txt b.txt > a_b.out
\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.out\t' -1 1 -2 2 a.txt b.txt > a_b.outDELIMITER' -1 FILE_1_join_column -2 FILE_2_join_column FILE_1 FILE_2 > OUTPUT_FILE

join -t$'\t' -1 1 -2 2 a.txt b.txt > a_b.out

Aug 11, 2009

Bach shell SPLIT


x='A:B:C';
echo ${x%:*};
output - A:B

echo ${x#*:};
output - B:C

echo $x;
A:B:C

x=${x% *};
echo ${x% *};
output - A

Jun 12, 2009

Command line Pagination


head -$m $filename | tail -$n

Sort file by column


SYNTAX :


SORT -t 'DELIMITER' -k`COLUMN NO` `IN FILENAME` > `OUT FILENAME`


E.g :


sort -t',' -k3 input.txt > output.txt

Splitting file


This
would split the file "newfile.txt" into three separate files called newaa, newab and newac each file the size of 22 :

split -b 22 newfile.txt new

This
would split the file "newfile.txt" into files beginning with the name "new" each containing 300 lines of text each :

split -l 300 file.txt new

Cron


Entry - Description Equivalent To
:

@reboot - Run once, at startup. None
@yearly - Run once a year 0 0 1 1 *
@annually - (same as @yearly) 0 0 1 1 *
@monthly - Run once a month 0 0 1 * *
@weekly - Run once a week 0 0 * * 0
@daily - Run once a day 0 0 * * *
@midnight - (same as @daily) 0 0 * * *
@hourly - Run once an hour 0 * * * *

*/120 07-22 * * * Once every 2 hours but only between 7 am and 10 pm

Counting Matching Records using AWK


Syntax :

awk -F\| '{if($
`COLUMN-NO` == `MATCH-STRING`) {print $3; } }' < `IN-FILE.txt`|awk '{sum = sum + $1} END {print sum;}'

awk -F\| '{if($
4 == 4177380) {print $3; } }' < input.txt|awk '{sum = sum + $1} END {print sum;}'

Finding Number Of Days in a Month


Syntax :
cal
`MONTH` `YEAR` | egrep -v '[A-Za-z]' | wc -w

E.g :
cal 2 2008 | egrep -v '[A-Za-z]' | wc -w


Syntax : cal
`MONTH` `date +%Y` | egrep -v '[A-Za-z]' | wc -w

E.g :
cal 1 `date +%Y` | egrep -v '[A-Za-z]' | wc -w

Jun 11, 2009

Extracting Rondom Lines from file


SYNTAX :


awk 'BEGIN {srand()} {printf "%05.0f %s \n",rand()*99999, $0; }'
`IN-FILE`| sort -n | head -`NO-OF-LINES` | sed 's/^[0-9]* //' > `OUT-FILE`

E.g :

awk 'BEGIN {srand()} {printf "%05.0f %s \n",rand()*99999, $0; }' datafile.in | sort -n | head -100 | sed 's/^[0-9]* //' >
datafile.out