有趣的linux命令总结

linux命令可以简化我们工作中的许多任务。关于Linux这个主题已经考虑很久了,也还是在不断的完善中,在自己的实验和各种资料的整理中,认为还是一些不错的命令。

自己也会在后续不断完善,大家有比较好的命令可以分享一下。

  • 1.查看自己常用的linux命令

history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10
这行脚本能输出你最常用的十条命令,可以看出自己平时最常用的一些命令。看看时间都去哪了?

-bash-4.1$ history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10

    193 ll

    123 cd

     79 sqlplus

     75 su

     71 ps

     64 crs_stat

     45 ksh

     32 vi

     26 less

     22 ls

hp-ux运行,可能有一些出入,调整为如下:

history | awk '{print $3}' | sort | uniq -c | sort -rn | head –10

 

  • 2. python -m SimpleHTTPServer

这条命令的优点就是简单,不用考虑太多的细节,直接运行即可启动一个简单的web服务器。通过http://localhost:8000即可访问。命令运行情况如下,通过浏览器查看的截图如下:

-bash-4.1$ python -m SimpleHTTPServer 

Serving HTTP on 0.0.0.0 port 8000 ...

192.168.3.1 - - [19/May/2014 07:24:38] "GET / HTTP/1.1" 200 -

192.168.3.1 - - [19/May/2014 07:24:39] code 404, message File not found

192.168.3.1 - - [19/May/2014 07:24:39] "GET /favicon.ico HTTP/1.1" 404 -

192.168.3.1 - - [19/May/2014 07:25:10] "GET /tmp/ HTTP/1.1" 200 -

ND97)%GWF{K(OG2TQ})2APA

其实临时需要分享一些文档,可以很方便,当然还有其它更好的用途。

 

  • 3.!!

下面这条命令有些神奇的味道,已经不能再简单了,但是看似简单,但是使用时需要谨慎,自己做学习之用还是比较好的,这也是这个命令的两面性了,如果它做的是一个rm操作,还是需要自己反复校验才可以。要不可能带来灾难。

!!

意思是重复执行上一条命令,示例如下,它会列出上一条命令,并且执行

 

[ora11g@rac1 test]$ ls -lrt *.log

-rw-r--r-- 1 ora11g dba 0 May  2 17:44 f.log

-rw-r--r-- 1 ora11g dba 0 May  2 17:44 d.log

-rw-r--r-- 1 ora11g dba 0 May  2 17:44 b.log

[ora11g@rac1 test]$ !!
ls -lrt
total 4
-rw-r--r-- 1 ora11g dba  0 May  2 17:44 f.log
-rw-r--r-- 1 ora11g dba  0 May  2 17:44 e.lst
-rw-r--r-- 1 ora11g dba  0 May  2 17:44 d.log
-rw-r--r-- 1 ora11g dba  0 May  2 17:44 c.lst
-rw-r--r-- 1 ora11g dba  0 May  2 17:44 b.log
-rw-r--r-- 1 ora11g dba  0 May  2 17:45 a.lst.bak
-rw-r--r-- 1 ora11g dba 15 May 19 07:39 a.lst

 

  • 4.  ^log^lst

将上一条命令中的 log替换为 lst,并执行

[ora11g@rac1 test]$ ^log^lst

ls -lrt *.lst

-rw-r--r-- 1 ora11g dba 0 May  2 17:44 e.lst

-rw-r--r-- 1 ora11g dba 0 May  2 17:44 c.lst

-rw-r--r-- 1 ora11g dba 0 May  2 17:44 a.lst

 

  • 5. cp filename{,.bak}

快速备份或复制文件。

[ora11g@rac1 test]$ cp test.dmp{,.backup}

[ora11g@rac1 test]$ ll

total 56

-rw-r--r-- 1 ora11g dba   293 Apr 13 01:36 showevent.sh

drwxr-xr-x 2 ora11g dba  4096 May  2 17:45 test

-rw-r--r-- 1 ora11g dba 16384 Jan 31 17:16 test2.dmp

-rw-r--r-- 1 ora11g dba 16384 Jan 31 17:15 test.dmp

-rw-r--r-- 1 ora11g dba 16384 May 19 07:32 test.dmp.backup

  • 6. cal

cal命令可能是快被大家遗忘的一个命令了,其实还是蛮实用的。比如查看2008年的年历,可以很清晰地显示出来,如果想显示2008年5月份的日历,也很方便的。

cal 2008

cal 5 2008

8JLMFM1H@(9[P%2}1E5]T2J

 

  • 7. wc

可能大家日常使用wc -l比较多,其实还可以统计有多少单词,多少字符。

[ora11g@rac1 test]$ cat a.lst
this is a test
[ora11g@rac1 test]$ wc -l a.lst
1 a.lst
[ora11g@rac1 test]$ wc -c a.lst
15 a.lst
[ora11g@rac1 test]$ wc -w a.lst
4 a.lst

 

  • 8. tac

这条命令如果仔细观察是cat的反转。功能也是如此。

-bash-4.1$ cat a.txt

This price is $5.00

This price is $6.00

this is good

-bash-4.1$ tac a.txt

this is good

This price is $6.00

This price is $5.00

 

  • 9. nl

如果输出的时候需要显示行号,nl也是一个不错的选择。效果和cat -n等价

-bash-4.1$ cat a.txt

This price is $5.00

This price is $6.00

this is good

-bash-4.1$ nl a.txt

     1  This price is $5.00

     2  This price is $6.00

     3  this is good

-bash-4.1$ cat -n a.txt

     1  This price is $5.00

     2  This price is $6.00

     3  this is good

 

  • 10. 显示当前目录的文件大小情况

-bash-4.1$ du -h --max-depth=1

4.0K    ./Pictures

4.0K    ./Templates

2.4G    ./test

372K    ./.gconf

60K     ./.ssh

12M     ./.mozilla

12K     ./.dbus

16K     ./.gnupg

 

  • 11. look

这个命令从我第一次用,就一下子有种相见恨晚的感觉,开始学英语的时候如果早知道这个命令就好了。

-bash-4.1$ look beautif

beautification

beautifications

beautified

beautifier

beautifiers

beautifies

beautiful

beautifully

beautifulness

beautify

beautifying

 

  • 12. factor

可以显示为对应的质数的乘积

-bash-4.1$ factor 2 12

2: 2

12: 2 2 3

-bash-4.1$ factor 2 15

2: 2

15: 3 5

 

  • 13. expr

shell在处理数字的时候毕竟还是要略逊一筹,这个命令也提供了一些便捷的处理方式,不过需要注意格式。

-bash-4.1$ expr 12/3

12/3

-bash-4.1$ expr  12 / 3

4

 

  • 14. 行列转换

shuf这个命令用起来真是舒服,很简单就实现了行列转换。

[ora11g@rac1 test]$ ls

a.lst  a.lst.bak  b.log  c.lst  d.log  e.lst  f.log

[ora11g@rac1 test]$ ls|shuf

a.lst

c.lst

b.log

d.log

f.log

e.lst

a.lst.bak

 

  • 15. last

如果想查看最近登录的用户情况。

-bash-4.1$ last|less

grid     pts/2        192.168.3.1      Mon May 19 07:23   still logged in  
grid     pts/0        192.168.3.1      Mon May 19 03:18   still logged in  
grid     pts/1        192.168.3.1      Sun May 18 17:13 - 07:28  (14:15)   
grid     pts/0        192.168.3.1      Sat May 17 08:32 - 17:48 (1+09:16)  
reboot   system boot  2.6.32-71.el6.x8 Sat May 17 08:31 - 07:43 (1+23:12)  
grid     pts/1        192.168.3.1      Sat May 17 02:58 - crash  (05:32)   
grid     pts/0        192.168.3.1      Sat May 17 01:56 - crash  (06:35)

 

  • 16. 对应的外网ip.

如果想查看对应的外网ip,一个命令就可以搞定。

[ora11g@rac1 test]$  curl ifconfig.me
117.79.232.14
[ora11g@rac1 test]$

请使用浏览器的分享功能分享到微信等