Chinaunix

标题: 10个你闻所未闻的Linux命令 [打印本页]

作者: itwaka    时间: 2011-07-06 11:01
标题: 10个你闻所未闻的Linux命令
http://www.itwaka.com/

这也许需要几年甚至是几十年,才能真正掌握Linux shell 命令。这里有10个你从来没有听说或使用过的命令。他们在没有特定的顺序。我最喜欢的是mkfifo。

    pgrep, 替代:

    # ps -ef | egrep '^root ' | awk '{print $2}'
    1
    2
    3
    4
    5
    20
    21
    38
    39
    ...
    你还可以这样:
    # pgrep -u root
    1
    2
    3
    4
    5
    20
    21
    38
    39
    ...
    pstree, 在tree 格式中列出进程,当有webSphere或重任务应用时非常有用。

    # pstree
    init-+-acpid
    |-atd
    |-crond
    |-cups-config-dae
    |-cupsd
    |-dbus-daemon-1
    |-dhclient
    |-events/0-+-aio/0
    | |-kacpid
    | |-kauditd
    | |-kblockd/0
    | |-khelper
    | |-kmirrord
    | `-2*[pdflush]
    |-gpm
    |-hald
    |-khubd
    |-2*[kjournald]
    |-klogd
    |-kseriod
    |-ksoftirqd/0
    |-kswapd0
    |-login---bash
    |-5*[mingetty]
    |-portmap
    |-rpc.idmapd
    |-rpc.statd
    |-2*[sendmail]
    |-smartd
    |-sshd---sshd---bash---pstree
    |-syslogd
    |-udevd
    |-vsftpd
    |-xfs
    `-xinetd
    bc 是个任意精度计算器语言,它可以Shell脚本执行平方根操作,expr 不支持平方根。

    # ./sqrt
    Usage: sqrt number
    # ./sqrt 64
    8
    # ./sqrt 132112
    363
    # ./sqrt 1321121321
    36347
    Here is the script:
    # cat sqrt
    #!/bin/bash
    if [ $# -ne 1 ]
    then
    echo 'Usage: sqrt number'
    exit 1
    else
    echo -e "sqrt($1)\nquit\n" | bc -q -i
    fi
    split, 你需要将大的文件分解称若干小部分?split是你的命令,下面是将250MB文件分解为2M的块儿,所有开始于LF_前缀。

    # ls -lh largefile
    -rw-r--r-- 1 root root 251M Feb 19 10:27 largefile
    # split -b 2m largefile LF_
    # ls -lh LF_* | head -n 5
    -rw-r--r-- 1 root root 2.0M Feb 19 10:29 LF_aa
    -rw-r--r-- 1 root root 2.0M Feb 19 10:29 LF_ab
    -rw-r--r-- 1 root root 2.0M Feb 19 10:29 LF_ac
    -rw-r--r-- 1 root root 2.0M Feb 19 10:29 LF_ad
    -rw-r--r-- 1 root root 2.0M Feb 19 10:29 LF_ae
    # ls -lh LF_* | wc -l
    126
    nl 数字线,在没发现nl之前,一直用脚本来实现。

    # head wireless.h
    /*
    * This file define a set of standard wireless extensions
    *
    * Version : 20 17.2.06
    *
    * Authors : Jean Tourrilhes - HPL
    * Copyright (c) 1997-2006 Jean Tourrilhes, All Rights Reserved.
    */#ifndef _LINUX_WIRELESS_H
    # nl wireless.h | head
    1 /*
    2 * This file define a set of standard wireless extensions
    3 *
    4 * Version : 20 17.2.06
    5 *
    6 * Authors : Jean Tourrilhes - HPL
    7 * Copyright (c) 1997-2006 Jean Tourrilhes, All Rights Reserved.
    8 */9 #ifndef _LINUX_WIRELESS_H
    mkfifo 是最酷的一个,你一定知道如何创建管道 输送grep的结果到LESS,可能甚至perl。但是你知道如何使2个命令通过1个命名管道沟通?看下图,创建管道,开始写到它。

mkfifo pipe; tail file > pipe

然后读取它:

cat pipe

    ldd, 想知道java链接到哪个Linux线程库?

    # ldd /usr/java/jre1.5.0_11/bin/java
    libpthread.so.0 => /lib/tls/libpthread.so.0 (0x00bd4000)
    libdl.so.2 => /lib/libdl.so.2 (0x00b87000)
    libc.so.6 => /lib/tls/libc.so.6 (0x00a5a000)
    /lib/ld-linux.so.2 (0x00a3c000)
    col, 想保存帮助页面为纯文本?

    # PAGER=cat
    # man less | col -b > less.txt
    xmlwf, 需要知道一个XML文档或许是配置文件是否合理?

    # curl -s 'http://bashcurescancer.com' > bcc.html
    # xmlwf bcc.html
    # perl -i -pe 's@<br/>@<br>@g' bcc.html
    # xmlwf bcc.html
    bcc.html:104:2: mismatched tag
    lsof 列出打开文件,你可以用它做很多很cool的事情,比如查找哪个接口是开放的?

    # lsof | grep TCP
    portmap 2587 rpc 4u IPv4 5544 TCP *:sunrpc (LISTEN)
    rpc.statd 2606 root 6u IPv4 5585 TCP *:668 (LISTEN)
    sshd 2788 root 3u IPv6 5991 TCP *:ssh (LISTEN)
    sendmail 2843 root 4u IPv4 6160 TCP badhd:smtp (LISTEN)
    vsftpd 9337 root 3u IPv4 34949 TCP *:ftp (LISTEN)
    cupsd 16459 root 0u IPv4 41061 TCP badhd:ipp (LISTEN)
    sshd 16892 root 3u IPv6 61003 TCP badhd.mshome.net:ssh->kontiki.mshome.net:4661 (ESTABLISHED)
    Note: OpenBSD 101 pointed out that “lsof -i TCP” a better way to obtain this same information. Thanks!Or find the number of open files a user has. Very important for running big applications like Oracle, DB2, or WebSphere:


    # lsof | grep ' root ' | awk '{print $NF}' | sort | uniq | wc -l
    179

    提示,匿名评论者指出应该用“sort -u”代替sort | uniq,本人忘记了-u flag,谢谢!

http://linux.itwaka.com/command/74735.html
作者: nagaregawa    时间: 2011-07-06 13:33
没有题目那么夸张吧!
作者: xiaohui1616    时间: 2011-07-06 18:11
呵呵。真得假的啊。不见得把。
作者: ajdan4q    时间: 2011-07-06 19:41
大部分都用过了。。
作者: ubuntuone    时间: 2011-07-07 11:41
这个可以搜藏下
作者: jerryjzm    时间: 2011-07-07 14:07
有的都用过。
有些在其他平台部通用

有mkfifo还有mknod
作者: linux4me    时间: 2011-07-13 20:23
好假啊,很多都有见到过,只是少用他们罢了
作者: godymoon    时间: 2011-07-13 21:14
lsof倒挺常用的

不过,很讨厌这种吹牛B的帖子
作者: ubuntuone    时间: 2011-07-13 23:21
恩  搜藏了  谢谢分享
作者: wsxedcer    时间: 2011-07-14 08:41
一个字,假
作者: Yei    时间: 2011-07-30 19:28
建议多去commandlinefu.com这样的网站看看,然后你的帖子标题上还可以多加几个0……
作者: unix5188    时间: 2011-07-31 15:20
基本上都使用过的,没有新鲜的东西啊。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2