Friday, August 13, 2010

pidgin log view

view pidgin logs for last 1 day

this is for the lazy users who want to check the last few chat messages
and respond

you need to enable logging.
go to your .purple directory usually in $HOME or give that path and find

this shows you the previous days chat messages till now.

find logs -mtime -2 -a -type f|xargs ls -tr|xargs elinks -dump -no-references|less

Thursday, August 5, 2010

System commands

System commands

Disk usage

df     report file system disk space blocks usage
df -i  disk space inodes usage
du    disk usage actual filesize
rm    remove file
mkdir  create directory
rmdir  remove empty directory

Process commands

ps        current processes
pmap  PID   memory map of process
kill  PID       kill process
kill  -TERM          default if no signal given (TERM=15)
pstree                     process tree
free        memory usage
ipcs        interprocess communication protocol
top         displays linux current tasks

Monday, July 19, 2010

Bash colours

Normal Colours
echo -e "\e[0;30m" Black
echo -e "\e[0;31m" Red
echo -e "\e[0;32m" Green
echo -e "\e[0;33m" Yellow
echo -e "\e[0;34m" Blue
echo -e "\e[0;35m" Purple
echo -e "\e[0;36m" Cyan
echo -e "\e[0;37m" White


Bold Colours

echo -e "\e[1;30m" Black
echo -e "\e[1;31m" Red
echo -e "\e[1;32m" Green
echo -e "\e[1;33m" Yellow
echo -e "\e[1;34m" Blue
echo -e "\e[1;35m" Purple
echo -e "\e[1;36m" Cyan
echo -e "\e[1;37m" White


Coloured with underline
echo -e "\e[4;30m"
echo -e "\e[4;31m"
echo -e "\e[4;32m" Green
echo -e "\e[4;33m"
echo -e "\e[4;34m" Blue
echo -e "\e[4;35m" Purple
echo -e "\e[4;36m" Cyan
echo -e "\e[4;37m" White

Background Colours
echo -e "\e[40m" Black
echo -e "\e[41m" Red
echo -e "\e[42m" Green
echo -e "\e[43m" Yellow
echo -e "\e[44m" Blue
echo -e "\e[45m" Purple
echo -e "\e[46m" Cyan
echo -e "\e[47m" White


Reset as of terminal colours
echo -e "\e[0m" Reset Text

The use of m indicates end of the codes

you can try out for combination
echo -e "\e[1;31m"Bold Red"\e[4;33m"Bold Yellow Underlined"\e[0;45m"Purple Background"\e[0m"

Monday, June 14, 2010

convert dos file to linux file

We often use the commands to convert the dos file to linux, using the commands

dos2unix - DOS/MAC to UNIX text file format converter
unix2dos - UNIX to DOS text file format converter


The conversion can be done by the commands tr and sed.

$ tr -d \\r < dos-file.txt > temp.txt && mv temp.txt dos-file.txt

here dos-file.txt will be converted to unix fileformat, temp.txt is the temporary file created and then renamed to dos-file.txt

$ sed -e 's/$/\r/g' unix-file.txt > temp.txt  && mv temp.txt unix-file.txt
here unix-file.txt will be converted to dos fileformat, temp.txt is the temporary file created and then renamed to unix-file.txt.

Thursday, June 3, 2010

find: files between dates

Finding the files in the date range

check man find for more

 find - search for files in a directory hierarchy
        -mmin n
              File's data was last modified n minutes ago.
       -mtime n
              File's  data  was  last modified n*24 hours ago. 

       -type c
              File is of type c:
              b      block (buffered) special
              c      character (unbuffered) special
              d      directory
              p      named pipe (FIFO)
              f      regular file
              l      symbolic link; this is never true if the -L option or the -follow  option
                     is  in effect, unless the symbolic link is broken.  If you want to search
                     for symbolic links when -L is in effect, use -xtype.
              s      socket
              D      door (Solaris)


       -name pattern
              Base of file name (the path with the leading directories removed) matches  shell
              pattern  pattern.   The  metacharacters  (`*', `?', and `[]') match a `.' at the
              start of the base name (this is a change in findutils-4.2.2; see  section  STAN‐
              DARDS  CONFORMANCE  below).   To  ignore a directory and the files under it, use
              -prune; see an example in the description of -path.  Braces are  not  recognised
              as  being special, despite the fact that some shells including Bash imbue braces
              with a special meaning in shell patterns.  The filename  matching  is  performed
              with  the  use of the fnmatch(3) library function.   Don't forget to enclose the
              pattern in quotes in order to protect it from expansion by the shell.




Listing the files with date

$ ls -l
-rw-r----- 1 syslog adm 1555 2010-06-03 07:54 messages
-rw-r----- 1 syslog adm 487017 2010-05-30 08:02 messages.1
-rw-r----- 1 syslog adm 646 2010-05-23 07:59 messages.2.gz
-rw-r----- 1 syslog adm 415 2010-05-16 07:47 messages.3.gz
-rw-r----- 1 syslog adm 55321 2010-05-09 07:49 messages.4.gz




-mtime 4 gives the file created exactly 4 days ago (4*24)

$ find . -mtime 4
./messages.1

$ date -d "4 days ago"
Sun May 30 16:15:14 IST 2010




-mtime 11 gives the file created exactly 11 days ago (11*24)

$ find . -mtime 11
./messages.2.gz

$ date -d "11 days ago"
Sun May 23 16:16:04 IST 2010




-mtime -11 gives the files created from now till 11 days

$ find . -mtime -11
./messages
./messages.1




-mtime +11 gives files created after the 11th day

$ find . -mtime +11
./messages.3.gz
./messages.4.gz




now when we want the files created from 11th day and older or rather above ten days then we can have

$ find .  -mtime +10
./messages.2.gz
./messages.3.gz
./messages.4.gz




if we want to have list of files created 10 days before whose name begins with messages

$find . -mtime +10 -a -name "messages*" -a -type f -exec ls -l {} \;
-rw-r----- 1 syslog adm 646 2010-05-23 07:59 ./messages.2.gz
-rw-r----- 1 syslog adm 415 2010-05-16 07:47 ./messages.3.gz
-rw-r----- 1 syslog adm 55321 2010-05-09 07:49 ./messages.4.gz

I would always suggest to have the -a -type f option so you are surely working on files only, as ls -l will in sub-directories.

Wednesday, June 2, 2010

sed: for the text between matching pattern

sed -ne '/IC/,/EI/p' country-codes.txt where IC is the first pattern and IE is the last for the file having country codes

file:country-codes.txt
GY,Guyana
HA,Haiti
HO,Honduras
HU,Hungary
IC,Iceland
IN,India
ID,Indonesia
IR,Iran
IZ,Iraq
EI,Ireland
IM,Isle of Man
IS,Israel
IT,Italy

command: sed -ne '/IC/,/EI/p' country-codes.txt
IC,Iceland
IN,India
ID,Indonesia
IR,Iran
IZ,Iraq
EI,Ireland

Friday, March 26, 2010

ls --color Decorating Directory Listing

The change the colours for your directory listing
Edit file: /etc/DIR_COLORS, copy the only changed ones in ~/.dircolors.

Next login, you get it.

file: /etc/DIR_COLORS
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
#NORMAL 00 # no color code at all
#FILE 00 # normal file, use no color at all
RESET 0 # reset to "normal" color
DIR 01;34 # directory
LINK 01;36 # symbolic link (If you set this to 'target' instead of a
# numerical value, the color is as for the file pointed to.)

Monday, March 1, 2010

FTP GET remote file timestamp

Check the remote server file time
login to ftp server
ftp servername
do
ls -ltr

will show list of files say we have
ftp> ls -ltr

227 Entering Passive Mode (10,146,172,50,66,132)
150 Here comes the directory listing.
-rw-r--r-- 1 50005 50005 5250 Mar 01 08:15 tech-talk
226 Directory send OK.


use modtime filename to check the file modified timestamp, ftp always shows in GMT time

ftp> modtime tech-talk

tech-talk 03/01/2010 08:15:00 GMT

Wednesday, February 17, 2010

ternary operator

$ test 1 -eq 1 && echo true || echo false
true

$ test 1 -eq 2 && echo true || echo false
false

&& -- operator will execute the second command only if the previous command ran successfully (exit status=0)

|| -- operator will execute the second command only if the previous command failed (exit status=1)

Tuesday, February 16, 2010

Total no. of ocurrence of word in file

We are searching the word phrase in the file rfc2119.txt

tr will split the space in newline and grep will search for the word in be beginning or end of the line and does not have any alphanumeric character around the word.


File: http://www.faqs.org/rfcs/rfc2119.txt
cat rfc2119.txt |tr '[[:space:]]' '\n' |grep -E "(^|\W)phrase(\W|$)" |wc -l

o/p: 5

Thursday, February 11, 2010

dc: base16 to base10

dc is a reverse-polish desk calculator

echo "2i 100 p"|dc
says: 100 is of base2 give o/p in base 10

echo "16i 100 p"|dc
says: 100 is of base16 give o/p in base 10

echo "16o 10i 100 p"|dc -- read 100 in base10 and o/p in base16
echo "2o 10i 100 p"|dc -- read 100 in base10 and o/p in base2

Friday, February 5, 2010

cp

cp * copies in ascii order
mkdir l; echo hello>b; echo ding> a
cp * will copy files a b to directory l,
if l is removed, a is copied to b

bash expansion

* -- is expanded in ascii
say you create files touch {e,b,a,t,f}
and you list it; it will list a b e f t
same thing happens if you do cp or mv

Monday, February 1, 2010

Linux Prompt

Avid Windows PROMPT C:\ lover on linux
try: export PS1="C:\$(pwd|tr '/' '\\\\\')> "

Thursday, January 28, 2010

Twitter style

Styling My Twitter संजीव लिनुक्स (San-Linux)

Perl UTF8 to DEC

This is reading xml loaded in @xmld, and returns the xml, with utf8 converted to dec.
For the systems which cannot store utf8 char sets.

foreach my $line (@xmld)
{
my $loopc=0;
while ($line=~/([\x{80}-\x{FFFF}])/ || $line=~/\d{3}\_=\_/){
$line=utf8todec($line);
$loopc++;
last if $loopc>4;
}

if ($line =~m/(\d{3,})\_\=\_/){
if (my @u_ar=($line=~m/\d{3,}\_=\_/g)){
foreach my $u_cs (@u_ar){
if (my $u_cs=~m/(\d{3,})\_\=\_/){
my $u_ch=$1;
$line=~s/$u_cs/&#$u_ch;/g;
}
}
}
}

if ($line ne "") {
if ( $jxmld !~/\s$/ && $line !~/.\s/ && $jxmld ne "" ) {
$jxmld .= " $line";
}else{
$jxmld .= $line;
}
}
}

sub utf8todec()
{
my $u_st=shift;
my @u_ar, $u_c1, $u_c2, $u_c3, $u_c4, $u_cs, $u_ch;

$u_st=~ s/([\x{80}-\x{FFFF}])/ord($1).'_=_'/gse;

if (@u_ar=($u_st=~m/\d{3}\_=\_\d{3}\_=\_\d{3}\_=\_/g)){
foreach $u_cs (@u_ar){
if ($u_cs=~m/(\d{3})\_\=\_(\d{3})\_\=\_(\d{3})\_\=\_/){
($u_c1, $u_c2, $u_c3)=($1,$2,$3);
if ($u_c1>=224&& $u_c1<=239){
$u_ch=($u_c1-224)*64*64+($u_c2-128)*64+($u_c3-128);
$u_st=~s/$u_cs/&#$u_ch;/g;
}
}
}
}

if (@u_ar=($u_st=~m/\d{3}\_=\_\d{3}\_=\_/g)){
foreach $u_cs (@u_ar){
if ($u_cs=~m/(\d{3})\_\=\_(\d{3})\_\=\_/){
($u_c1, $u_c2)=($1,$2);
if ($u_c1>=192&& $u_c1<=223){
$u_ch=($u_c1-192)*64+($u_c2-128);
$u_st=~s/$u_cs/&#$u_ch;/g;
}
}
}
}
return $u_st;
}

Tuesday, January 19, 2010

BASH

for loops: 1 to 10

for i in `seq 1 10`; do
echo $i;
done

for loops: 1 to 10

for i in `
echo {1..10}`; do
echo $i;
done

for loops: A to 10

for i in `echo {A..Z}`; do
echo $i;
done

Monday, January 18, 2010

File differences

diff -BNarq

The best use for file differences,
-b --ignore-space-change Ignore changes in the amount of white space.
-w --ignore-all-space Ignore all white space.
-B --ignore-blank-lines Ignore changes whose lines are all blank.
-a --text Treat all files as text.
-r --recursive Recursively compare any subdirectories found.
-N --new-file Treat absent files as empty.
-q --brief Output only whether files differ.


If the sources are checked out from svn,
I would remove all the .svn directories and do the diff, issuing
find . -iname ".svn" -exec rm -frv '{}' \;

SED Tips

Sed: Search text and display

sed -n '/404 Not Found/,/405 Method Not Allowed/p' rfc2616.txt

This searches the rfc2616.txt T, for the pattern 404 Not Found till 405 Method Not Allowed is found, then displays.

source: http://www.faqs.org/rfcs/rfc2616.txt

sed -n '/404 Not Found/,/405 Method Not Allowed/p' rfc2616.txt

10.4.5 404 Not Found ...........................................66
10.4.6 405 Method Not Allowed ..................................66
10.4.5 404 Not Found

The server has not found anything matching the Request-URI. No
indication is given of whether the condition is temporary or
permanent. The 410 (Gone) status code SHOULD be used if the server
knows, through some internally configurable mechanism, that an old
resource is permanently unavailable and has no forwarding address.
This status code is commonly used when the server does not wish to
reveal exactly why the request has been refused, or when no other
response is applicable.

10.4.6 405 Method Not Allowed