Tuesday, March 23, 2010

Less known Linux commands.

Completing a task with the simplest command.

#ac prints out a report of connect time (in hours) based on the logins/logouts in the current wtmp file. A total is also printed out

$ac -p
root                                532.96
admin                               63.53
total     536.96

# mcookie generates a 128-bit random hexadecimal number
#The "random" number generated is actually the output of the MD5 message digest fed with various piece of random information: the current time, the process id, the parent process id ..
$mcookie
9612f5db846dd4d0655572b33ea1cb6f

#pidof -- find the process ID of a running program.
##match program name only, it can't match argument of the program
$ pidof /sbin/mingetty
3773 3770 3769 3768 3767 3766

# procinfo :display memory, disk, IRQ & DMA module and device info #$procinfo -D refresh values every few seconds just like top.
$procinfo
Memory:      Total        Used        Free      Shared     Buffers
Mem:       3897500      684240     3213260           0      269308
Swap:       779112           0      779112

Bootup: Wed Feb 24 00:20:02 2010    Load average: 0.11 0.07 0.02 3/223 5641

user  :       0:54:16.96   0.2%  page in :     103469  disk 1:    10272r 1857163w
nice  :       0:00:28.10   0.0%  page out:   12920948
system:       3:13:02.34   0.6%  page act:     126534
IOwait:       0:11:44.80   0.0%  page dea:          0
hw irq:       0:00:08.20   0.0%  page flt:   84232828
sw irq:       1:30:52.17   0.3%  swap in :          0
idle  :  22d  9:49:49.93  98.9%  swap out:          0
uptime:  22d 15:38:40.56         context : 1476505392

irq  0: 489275054 timer                 irq  9:         0 acpi         
irq  1:         8 i8042                 irq 12:       105 i8042        
irq  3:         1                       irq 14:  17590508 ide0         
irq  4:         1                       irq169:   7179622 ioc0, vmxnet ether
irq  6:         5                       irq177:   3543610 vmxnet ether 
irq  7:         0 parport0              irq185:     42843 vmxnet ether 
irq  8:         2 rtc                   irq193:  15376791 vmxnet ether 

#readlink - display destination of a symbolic link
$ls -l test2
lrwxrwxrwx 1 root wheel 5 2010-03-19 10:09 test2 -> test1
$readlink test2
test1
#Who links me? find source softlink with "lname" argument
$ touch /var/tmp/test1
$ ln -s /var/tmp/test1 /tmp/test1
$ find /tmp -lname /var/tmp/test1 -ls
11    0 lrwxrwxrwx   1 root wheel          14 Jun 30 11:07 /tmp/test1 -> /var/tmp/test1

#Who links me? find source hardlink with "samefile" argument
#hardlinks have same inode number(inode=13316 in this example), so it can also be achived with inum argument as well.
$touch /tmp/test1/test-hl
$ln  /tmp/test1/test-hl /tmp/test-hl
$find /tmp/ -samefile /tmp/test1/test-hl -exec ls -il {} \;
13316 -rw-r--r-- 2 root  wheel 0 2010-06-30 11:15 /tmp/test1/test-hl
13316 -rw-r--r-- 2 root wheel 0 2010-06-30 11:15 /tmp/test-hl


# seq - print a sequence of numbers
# default increment is 1
# It was used in "for loop", but now brace expansion {1..3} is better choice.

$seq
1
2
3
$seq -s " "  3
1 2 3

#convert vertical lines to horizontal lines

$ seq 4 | paste -s
1       2       3       4

$ seq 4 | xargs -n 2
1 2
3 4

#convert horizontal lines to vertical lines
$echo "123456"| fold -w 2
12
34
56

#stat - display file or file system status
##Links:2 means the file has two copies(hard link files)
$stat test1
File: `test1'
Size: 3               Blocks: 8          IO Block: 4096   regular file
Device: fd04h/64772d    Inode: 767061      Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2010-03-18 16:21:01.000000000 +1100
Modify: 2010-03-18 16:21:00.000000000 +1100
Change: 2010-03-18 16:21:00.000000000 +1100

##display parent file system info.It can be used to find parent partition by matching inodes Total/Free of df -i
$stat -f test1
File: "test1"
ID: 0        Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 2633708    Free: 2371194    Available: 2237409
Inodes: Total: 1338240    Free: 1335916

#tac - concatenate and print files in reverse
$cat t1
12
$ tac t1
21

#usleep - sleep for the specified number of microseconds
usleep 300

#watch - execute a program periodically, showing output fullscreen
#monitor eth0 output, refresh every 2 seconds
watch -n 2 ifconfig eth0

#Zdump prints the current time in each zone name named on the command line.
$date
Thu Mar 18 05:51:55 GMT 2010
$zdump GMT
GMT  Thu Mar 18 05:51:57 2010 GMT
(alternative command: $export TZ=GMT;date)

#zgrep - search possibly compressed files (.Z,gz,bz2)for a regular expression
#other z commands zcat zmore ...
$zgrep .*  test1.gz
test1.gz:test message

#shell tips
# - is equivalent to $OLDPWD;When a is used as the operand,this shall be equivalent to the command: cd "$OLDPWD" && pwd ; (reference man ksh or man bash)

$cd /tmp
$cd /var/tmp
$cd -
/tmp

# "$_" .. expands to to the last argument to the previous command.. (reference Special Parameters in man ksh/bash)
# ( bash only "!$" "!!:$" designates the last argument of the preceding command. This may be shortened to "!$")
$ ping -c 1 172.16.1.1
PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data.
64 bytes from 172.16.1.1: icmp_seq=1 ttl=64 time=0.396 ms
$ telnet $_ 80
Trying 172.16.1.1...
Connected to 172.16.1.1.
Escape character is '^]'.
^]
telnet> quit
Connection closed.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.