10個(gè) Linux 命令,讓你的操作更有效率
Linux是最適合開發(fā)的操作系統(tǒng),它是把所有的操作權(quán)都交給了用戶,有什么操作,就會(huì)呈現(xiàn)出什么樣的格局。開放、自由、誠實(shí),就是它最大的魅力。
而且越來越多的企業(yè)開始選擇Linux作為服務(wù)器,所以對(duì)于我們正在學(xué)習(xí)的小伙伴來說,了解Linux是極為重要的一章課題。
今天幫大家收集了10個(gè)Linux的命令,希望小伙伴們能夠本文中更深刻地理解 Linux 的相關(guān)特性:
01 pgrep
pgrep名字前有個(gè)p,我們可以猜到這和進(jìn)程相關(guān),又是grep,當(dāng)然這是進(jìn)程相關(guān)的grep命令。不過,這個(gè)命令主要是用來列舉進(jìn)程ID的。如:
- $ pgrep -u hchen2244122444
這個(gè)命令相當(dāng)于:
- ps -ef | egrep '^hchen' | awk '{print $2}'
02 pstree
這個(gè)命令可以以樹形的方式列出進(jìn)程。如下所示:
- [hchen@RHELSVR5 ~]$ pstree
- init-+-acpid
- |-auditd-+-python
- | `-{auditd}
- |-automount---4*[{automount}]
- |-backup.sh---sleep
- |-dbus-daemon
- |-events/0
- |-events/1
- |-hald---hald-runner---hald-addon-acpi
- |-httpd---10*[httpd]
- |-irqbalance
- |-khelper
- |-klogd
- |-ksoftirqd/0
- |-ksoftirqd/1
- |-kthread-+-aio/0
- | |-aio/1
- | |-ata/0
- | |-ata/1
- | |-ata_aux
- | |-cqueue/0
- | |-cqueue/1
- | |-kacpid
- | |-kauditd
- | |-kblockd/0
- | |-kblockd/1
- | |-kedac
- | |-khubd
- | |-6*[kjournald]
- | |-kmirrord
- | |-kpsmoused
- | |-kseriod
- | |-kswapd0
- | |-2*[pdflush]
- | |-scsi_eh_0
- | |-scsi_eh_1
- | |-xenbus
- | `-xenwatch
- |-migration/0
- |-migration/1
- |-6*[mingetty]
- |-3*[multilog]
- |-mysqld_safe---mysqld---9*[{mysqld}]
- |-smartd
- |-sshd---sshd---sshd---bash---pstree
- |-svscanboot---svscan-+-3*[supervise---run]
- | |-supervise---qmail-send-+-qmail-clean
- | | |-qmail-lspawn
- | | `-qmail-rspawn
- | `-2*[supervise---tcpserver]
- |-syslogd
- |-udevd
- |-watchdog/0
- |-watchdog/1
- -xinetd
03 bc
這個(gè)命令主要是做一個(gè)精度比較高的數(shù)學(xué)運(yùn)算的。比如開平方根等。下面是一個(gè)我們利用bc命令寫的一個(gè)腳本(文件名:sqrt)
- #!/bin/bash
- if [ $# -ne 1 ]
- then
- echo 'Usage: sqrt number'
- exit 1else
- echo -e "sqrt($1)\nquit\n" | bc -q -i
- fi
于是,我們可以這樣使用這個(gè)腳本進(jìn)行平方根運(yùn)算:
- [hchen@RHELSVR5]$ ./sqrt 36
- 6
- [hchen@RHELSVR5]$ ./sqrt 2.0000
- 1.4142
- [hchen@RHELSVR5]$ ./sqrt 10.0000
- 3.1622
04 split
如果你有一個(gè)很大的文件,你想把其分割成一些小的文件,那么這個(gè)命令就是干這件事的了。
- [hchen@RHELSVR5 applebak]# ls -l largefile.tar.gz
- -rw-r--r-- 1 hchen hchen 436774774 04-17 02:00 largefile.tar.gz
- [hchen@RHELSVR5 applebak]# split -b 50m largefile.tar.gz LF_
- [hchen@RHELSVR5]# ls -l LF_*-rw-r--r-- 1 hchen hchen 52428800 05-10 18:34 LF_aa
- -rw-r--r-- 1 hchen hchen 52428800 05-10 18:34 LF_ab
- -rw-r--r-- 1 hchen hchen 52428800 05-10 18:34 LF_ac
- -rw-r--r-- 1 hchen hchen 52428800 05-10 18:34 LF_ad
- -rw-r--r-- 1 hchen hchen 52428800 05-10 18:34 LF_ae
- -rw-r--r-- 1 hchen hchen 52428800 05-10 18:35 LF_af
- -rw-r--r-- 1 hchen hchen 52428800 05-10 18:35 LF_ag
- -rw-r--r-- 1 hchen hchen 52428800 05-10 18:35 LF_ah
- -rw-r--r-- 1 hchen hchen 17344374 05-10 18:35 LF_ai
文件合并只需要使用簡(jiǎn)單的合并就行了,如:
- [hchen@RHELSVR5]# cat LF_* >largefile.tar.gz
05 nl
nl命令其它和cat命令很像,只不過它會(huì)打上行號(hào)。如下所示:
- [hchen@RHELSVR5 include]# nl stdio.h | head -n 10
- 1 /* Define ISO C stdio on top of C++ iostreams.
- 2 Copyright (C) 1991,1994-2004,2005,2006 Free Software Foundation, Inc.
- 3 This file is part of the GNU C Library.
- 4 The GNU C Library is free software; you can redistribute it and/or
- 5 modify it under the terms of the GNU Lesser General Public
- 6 License as published by the Free Software Foundation; either
- 7 version 2.1 of the License, or (at your option) any later version.
- 8 The GNU C Library is distributed in the hope that it will be useful,
06 mkfifo
熟悉Unix的人都應(yīng)該知道這個(gè)是一個(gè)創(chuàng)建有名管道的系統(tǒng)調(diào)用或命令。
平時(shí),我們?cè)诿钚猩鲜褂秘Q線“|”把命令串起來是使用無命管道。
而我們使用mkfifo則使用的是有名管道。下面是示例:
下面是創(chuàng)建一個(gè)有名管道:
- [hchen@RHELSVR5 ~]# mkfifo /tmp/hchenpipe
- [hchen@RHELSVR5 ~]# ls -l /tmp
- prw-rw-r-- 1 hchen hchen 0 05-10 18:58 hchenpipe
然后,我們?cè)谝粋€(gè)shell中運(yùn)行如下命令,這個(gè)命令不會(huì)返回,除非有人從這個(gè)有名管道中把信息讀走。
- [hchen@RHELSVR5 ~]# ls -al > /tmp/hchenpipe
我們?cè)诹硪粋€(gè)命令窗口中讀取這個(gè)管道中的信息:(其會(huì)導(dǎo)致上一個(gè)命令返回)
- [hchen@RHELSVR5 ~]# head /tmp/hchenpipe
- drwx------ 8 hchen hchen 4096 05-10 18:27 .
- drwxr-xr-x 7 root root 4096 03-05 00:06 ..
- drwxr-xr-x 3 hchen hchen 4096 03-01 18:13 backup
- -rw------- 1 hchen hchen 721 05-05 22:12 .bash_history
- -rw-r--r-- 1 hchen hchen 24 02-28 22:20 .bash_logout
- -rw-r--r-- 1 hchen hchen 176 02-28 22:20 .bash_profile
- -rw-r--r-- 1 hchen hchen 124 02-28 22:20 .bashrc
- -rw-r--r-- 1 root root 14002 03-07 00:29 index.htm
- -rw-r--r-- 1 hchen hchen 31465 03-01 23:48 index.php
07 ldd
這個(gè)命令可以知道你的一個(gè)可執(zhí)行文件所使用了動(dòng)態(tài)鏈接庫。如:
- [hchen@RHELSVR5 ~]# ldd /usr/bin/java
- linux-gate.so.1 => (0x00cd9000)
- libgij.so.7rh => /usr/lib/libgij.so.7rh (0x00ed3000)
- libgcj.so.7rh => /usr/lib/libgcj.so.7rh (0x00ed6000)
- libpthread.so.0 => /lib/i686/nosegneg/libpthread.so.0 (0x00110000)
- librt.so.1 => /lib/i686/nosegneg/librt.so.1 (0x009c8000)
- libdl.so.2 => /lib/libdl.so.2 (0x008b5000)
- libz.so.1 => /usr/lib/libz.so.1 (0x00bee000)
- libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00aa7000)
- libc.so.6 => /lib/i686/nosegneg/libc.so.6 (0x0022f000)
- libm.so.6 => /lib/i686/nosegneg/libm.so.6 (0x00127000)
- /lib/ld-linux.so.2 (0x00214000)
08 col
這個(gè)命令可以讓你把man文件轉(zhuǎn)成純文本文件。如下示例:
- # PAGER=cat# man less | col -b > less.txt
09 xmlwf
這個(gè)命令可以讓你檢查一下一個(gè)XML文檔是否是所有的tag都是正常的。如:
- [hchen@RHELSVR5 ~]# curl 'https://coolshell.cn/?feed=rss2' > cocre.xml
- % Total % Received % Xferd Average Speed Time Time Time Current
- Dload Upload Total Spent Left Speed
- 100 64882 0 64882 0 0 86455 0 --:--:-- --:--:-- --:--:-- 2073k
- [hchen@RHELSVR5 ~]# xmlwf cocre.xml
- [hchen@RHELSVR5 ~]# perl -i -pe 's@<link>@<br>@g' cocre.xml
- [hchen@RHELSVR5 ~]# xmlwf cocre.xmlcocre.xml:13:23: mismatched tag
10 lsof
可以列出打開了的文件。
- [root@RHELSVR5 ~]# lsof | grep TCPhttpd 548 apache 4u IPv6 14300967 TCP *:http (LISTEN)
- httpd 548 apache 6u IPv6 14300972 TCP *:https (LISTEN)
- httpd 561 apache 4u IPv6 14300967 TCP *:http (LISTEN)
- httpd 561 apache 6u IPv6 14300972 TCP *:https (LISTEN)
- sshd 1764 root 3u IPv6 4993 TCP *:ssh (LISTEN)
- tcpserver 8965 root 3u IPv4 153795500 TCP *:pop3 (LISTEN)
- mysqld 10202 mysql 10u IPv4 73819697 TCP *:mysql (LISTEN)
- sshd 10735 root 3u IPv6 160731956 TCP 210.51.0.232:ssh->123.117.239.68:31810 (ESTABLISHED)
- sshd 10767 hchen 3u IPv6 160731956 TCP 210.51.0.232:ssh->123.117.239.68:31810 (ESTABLISHED)
- vsftpd 11095 root 3u IPv4 152157957 TCP *:ftp (LISTEN)