您的位置:首页 > 运维架构 > Linux

鲜为人知而又实用的 Linux 命令大全

2016-07-27 16:52 627 查看

鲜为人知而又实用的 Linux 命令大全

Linux命令行吸引了大多数Linux爱好者。一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务。Linux命令和它们的转换对于Linux用户、Shell脚本程序员和管理员来说是最有价值的宝藏。有些Linux命令很少人知道,但不管你是新手还是高级用户,它们都非常方便有用。

这篇文章的目的是介绍一些少有人知的Linux命令,它们一定会高效地帮你管理你的桌面/服务器。


1. sudo !!命令

没有特定输入sudo命令而运行,将给出没有权限的错误。那么,你不需要重写整个命令,仅仅输入'!!'就可以抓取最后的命令。

·········10········20········30········40········50········60········

1.
$
apt-get update


2.


3.
E:
Could not
open
lock
file
/var/lib/apt/lists/lock
-
open
(13:
Permission denied)


4.
E:
Unable to lockdirectory /var/lib/apt/lists/


5.
E:
Could not
open
lock
file
/var/lib/dpkg/lock
-
open
(13:
Permission denied)


6.
E:
Unable to lockthe administration directory (/var/lib/dpkg/), are you root?


-

·········10········20········30········40········50········60········

01.
$
sudo
!!


02.


03.
sudo
apt-get
update


04.
[
sudo
]
password
for
server:


05.


06.
..


07.
Fetched
474 kB
in
16s
(28.0 kB/s)


08.
Reading
package lists... Done


09.
server@localhost:~$



2. python命令

下面的命令生产一个通过HTTP显示文件夹结构树的简单网页,可以通过浏览器在端口8000访问,直到发出中断信号。

# python -m SimpleHTTPServer




3. mtr命令

我们大多数都熟悉ping和traceroute。那对于把两个命令的功能合二为一的mtr命令呢。如果mtr没在你的机子上安装,apt或者yum需要的包。

$sudo apt-get install mtr (On Debian based Systems)

# yum install mtr (On Red Hat based Systems)

现在运行mtr命令,开始查看mtr运行的主机和google.com直接的网络连接。

# mtr google.com




4. Ctrl+x+e命令

这个命令对于管理员和开发者非常有用。为了使每天的任务自动化,管理员需要通过输入vi、vim、nano等打开编辑器。

仅仅从命令行快速的敲击“Ctrl-x-e”,就可以在编辑器中开始工作了。


5. nl命令

“nl命令”添加文件的行数。一个叫做'one.txt'的文件,其每行的内容是(Fedora、Debian、Arch、Slack和Suse),给每行添加行号。首先使用cat命令显示“one.txt”的文件内容。

·········10········20········30········40········50········60········

1.
#
cat one.txt


2.


3.
fedora


4.
debian


5.
arch


6.
slack


7.
suse


现在运行“nl命令”,以添加行号的方式来显示。

·········10········20········30········40········50········60········

1.
#
nl one.txt


2.


3.
1
fedora


4.
2
debian


5.
3
arch


6.
4
slack


7.
5
suse



6. shuf命令

“Shut”命令随机从一个文件或文件夹中选择行/文件/文件夹。首先使用ls命令来显示文件夹的内容。

·········10········20········30········40········50········60········

01.
#
ls


02.
Desktop
Documents  Downloads  Music  Pictures  Public  Templates  Videos


03.


04.
#
ls | shuf (shuffle Input)


05.
Music


06.
Documents


07.
Templates


08.
Pictures


09.
Public


10.
Desktop


11.
Downloads


12.
Videos


13.


14.
#
ls | shuf -n1 # 随机选择一个


15.
Public


16.


17.
#
ls | shuf -n1


18.
Videos


19.


20.
#
ls | shuf -n1


21.
Templates


22.


23.
#
ls | shuf -n1


24.
Downloads


注意:你可以把‘ n1’替换成‘ n2’来输出两个随机选择或者使用 n3、 n4等数字输出其他任意的随机选择。


7. ss命令

“ss”表示socket统计。这个命令调查socket,显示类似netstat命令的信息。它可以比其他工具显示更多的TCP和状态信息。

·········10········20········30········40········50········60········

01.
#
ss


02.


03.
State
Recv-Q Send-Q      Local Address:Port          Peer Address:Port


04.
ESTAB
0      0           192.168.1.198:41250        *.*.*.*:http


05.
CLOSE-WAIT
1      0               127.0.0.1:8000             127.0.0.1:41393


06.
ESTAB
0      0           192.168.1.198:36239        *.*.*.*:http


07.
ESTAB
310    0               127.0.0.1:8000             127.0.0.1:41384


08.
ESTAB
0      0           192.168.1.198:41002       *.*.*.*:http


09.
ESTAB
0      0               127.0.0.1:41384            127.0.0.1:8000



8. last命令

“last”命令显示的是上次登录用户的历史信息。这个命令通过搜索文件“/var/log/wtmp”,显示logged-in和logged-out及其tty‘s的用户列表。

·········10········20········30········40········50········60········

01.
#
last


02.
server
pts/0        :0               Tue Oct 22 12:03   still logged
in


03.
server
tty8         :0               Tue Oct 22 12:02   still logged
in


04.


05.
...


06.
(unknown
tty8         :0               Tue Oct 22 12:02 - 12:02  (00:00)


07.
server
pts/0        :0               Tue Oct 22 10:33 - 12:02  (01:29)


08.
server
tty7         :0               Tue Oct 22 10:05 - 12:02  (01:56)


09.
(unknown
tty7         :0               Tue Oct 22 10:04 - 10:05  (00:00)


10.
reboot
system boot  3.2.0-4-686-pae  Tue Oct 22 10:04 - 12:44  (02:39)


11.


12.
wtmp
begins Fri Oct  4 14:43:17 2007



9. curl ifconfig.me

那么如何得到你的外部IP地址呢?使用google?那么这个命令就在你的终端输出你的外部IP地址。

# curl ifconfig.me

注意:你可能没有按照curl包,你需要 apt/yum来按照包。


10. tree命令

以树式的格式得到当前文件夹的结构。

·········10········20········30········40········50········60········

01.
#
tree


02.
.


03.
|--
Desktop


04.
|--
Documents


05.
|
`-- 37.odt


06.
|--
Downloads


07.
|
|-- attachments.zip


08.
|
|-- ttf-indic-fonts_0.5.11_all.deb


09.
|
|-- ttf-indic-fonts_1.1_all.deb


10.
|
`-- wheezy-nv-
install
.sh


11.
|--
Music


12.
|--
Pictures


13.
|
|-- Screenshot from 2013-10-22 12:03:49.png


14.
|
`-- Screenshot from 2013-10-22 12:12:38.png


15.
|--
Public


16.
|--
Templates


17.
`--
Videos


18.


19.
10
directories, 23 files



11. pstree

这个命令显示当前运行的所有进程及其相关的子进程,输出的是类似‘tree’命令的树状格式。

·········10········20········30········40········50········60········

01.
#
pstree


02.
init─┬─NetworkManager───{NetworkManager}


03.
├─accounts-daemon───{accounts-daemon}


04.
├─acpi_fakekeyd


05.
├─acpid


06.
├─apache2───10*[apache2]


07.
├─at-spi-bus-laun───2*[{at-spi-bus-laun}]


08.
├─atd


09.
├─avahi-daemon───avahi-daemon


10.
├─bluetoothd


11.
├─colord───{colord}


12.
├─colord-sane───2*[{colord-sane}]


13.
├─console-kit-dae───64*[{console-kit-dae}]


14.
├─
cron


15.
├─cupsd


16.
├─2*[dbus-daemon]


17.
├─dbus-launch


18.
├─dconf-service───2*[{dconf-service}]


19.
├─dovecot─┬─anvil


20.
│
├─config


21.
│
└─log


22.
├─exim4


23.
├─gconfd-2


24.
├─gdm3─┬─gdm-simple-slav─┬─Xorg


25.
│
│                 ├─gdm-session-wor─┬─x-session-manag─┬─evolution-a+


26.
│
│                 │                 │                 ├─gdu-notific+


27.
│
│                 │                 │                 ├─gnome-scree+


28.
│
│                 │                 │                 ├─gnome-setti+


29.
│
│                 │                 │                 ├─gnome-shell+++


30.
│
│                 │                 │                 ├─nm-applet──+++


31.
│
│                 │                 │                 ├─
ssh
-agent


32.
│
│                 │                 │                 ├─tracker-min+


33.
│
│                 │                 │                 ├─tracker-sto+


34.
│
│                 │                 │                 └─3*[{x-sessi+


35.
│
│                 │                 └─2*[{gdm-session-wor}]


36.
│
│                 └─{gdm-simple-slav}


37.
│
└─{gdm3}


38.
├─6*[getty]


39.
├─gnome-keyring-d───9*[{gnome-keyring-d}]


40.
├─gnome-shell-
cal
───2*[{gnome-shell-
cal
}]


41.
├─goa-daemon───{goa-daemon}


42.
├─gsd-printer───{gsd-printer}


43.
├─gvfs-afc-volume───{gvfs-afc-volume}


目前为止就这么多。在下篇文章中,我将涉及一些其他很少有人知道的有趣的Linux命令。到那时连接 Tecmint保持收看。喜欢和分享将有助于我们传播。


12. <空格> 命令

你在终端上键入的每个命令都会记录到history,也能用history命令重新调用。

如何骗过history 命令呢?呵呵,你可以轻而易举地做到。在终端,只需要在键入命令之前输入一个或多个空格,这样你的命令就不会被记录了。

让我们体验一下吧,先在终端尝试五个常见的 Linux 命令并在命令之前留个空(例如** ls, pwd, uname, echo “hi”** 和 who),然后检查这些命令是不是记录在历史中。
avi@localhost:~$ ls
avi@localhost:~$ pwd
avi@localhost:~$ uname
avi@localhost:~$ echo “hi”
avi@localhost:~$ who


现在运行
history
命令来查看上面已执行的命令是否已经被记录了.
avi@localhost:~$history
40 cd /dev/
41 ls
42 dd if=/dev/cdrom1 of=/home/avi/Desktop/squeeze.iso
43 ping www.google.com
44 su


你看到没有最后执行的命令没有被记录。我们也可以用另一个命令
cat | bash
欺骗history,结果跟上面一样。


13. stat 命令

Linux中的stat命令用来显示文件或文件系统的状态信息。当用文件名作为参数时,stat将会展示文件的全部信息。状态信息包括文件 大小、块、权限、访问时间、修改时间、状态改变时间等。
avi@localhost:~$stat 34.odt
File: `34.odt'
Size: 28822 Blocks: 64 IO Block: 4096 regular file
Device: 801h/2049d Inode: 5030293 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ avi) Gid: ( 1000/ avi)
Access: 2013-10-14 00:17:40.000000000 +0530
Modify: 2013-10-01 15:20:17.000000000 +0530
Change: 2013-10-01 15:20:17.000000000 +0530


14. + . 和 + .

上面的组合键事实上不是一个命令,而是传递最后一个命令参数到提示符后的快捷键,以输入命令的倒序方式传递命令。按住 Alt或Esc再按一下 “.”。


15. pv 命令

在电影里尤其是好莱坞电影你可能已经看见过模拟文本了,像是在实时输入文字,你可以用pv命令仿照任何类型模拟风的文本输出,包括流水线输出。pv可能没有在你的系统上安装,你需要用apt或yum获取安装包,然后安装pv到你的机器。

root@localhost:# echo "Tecmint [dot] com is the world's best website for qualitative Linux article" | pv -qL 20

输出样式
Tecmint [dot] com is the world''s best website for qualitative Linux article


16. mount | colum -t

上面的命令用一个很不错的格式与规范列出了所有挂载文件系统。
avi@localhost:~$mount | column -t


输出样式
/dev/sda1on  / type  ext3 (rw,errors=remount-ro)
tmpfson  /lib/init/rw  type  tmpfs(rw,nosuid,mode=0755)
proc on  /proc type  proc (rw,noexec,nosuid,nodev)
sysfson  /sys  type  sysfs(rw,noexec,nosuid,nodev)
udev on  /dev  type  tmpfs(rw,mode=0755)
tmpfson  /dev/shm  type  tmpfs(rw,nosuid,nodev)
devpts   on  /dev/pts  type  devpts   (rw,noexec,nosuid,gid=5,mode=620)
fusectl  on  /sys/fs/fuse/connections  type  fusectl  (rw)
binfmt_misc  on  /proc/sys/fs/binfmt_misc  type  binfmt_misc  (rw,noexec,nosuid,nodev)
nfsd on  /proc/fs/nfsd type  nfsd (rw)


17. Ctr+l 命令

在进行下一步之前,我先问一下,你是如何清理你的终端?呵呵,你会在提示符后键入 “clear”。好的。用上面的命令执行清理终端都将成为过去。你只需要按下Ctr+l,看看它如何立即清理你的终端。


18. curl 命令

在命令行下如何检查你的未读邮件?这个命令对于工作在没有图形界面的服务器的人佷有用。它会在运行期间再次要求输入密码,你不需要在上面一行硬编码你的密码,否则会有其它安全风险。

avi@localhost:~$curl -u avishek1210@gmail.com --silent "https://mail.google.com/mail/feed/atom" | perl -ne 'print \t if //; print "$2\n" if /<(title|name)>(.*)<\/\1>/;'

输出样式
Enter host password for user 'avishek1210@gmail.com':
Gmail - Inbox for avishek1210@gmail.com
People offering cars in Delhi - Oct 26
Quikr Alerts
another dependency question
Chris Bannister
Ralf Mardorf
Reco
Brian
François Patte
Curt
Siard
berenger.morel
Hi Avishek - Download your Free MBA Brochure Now...
Diya
★Top Best Sellers Of The Week, Take Your Pick★
Timesdeal
aptitude misconfigure?
Glenn English
Choosing Debian version or derivative to run Wine when resource poor
Chris Bannister
Zenaan Harkness
Curt
Tom H
Richard Owlett
Ralf Mardorf
Rob Owens


19.screen 命令

screen命令能断开一个会话下的一个长时间运行的进程并能再次连接,如有需要,也提供了灵活的命令选项

要运行一个长时间的进程,我们通常执行
avi@localhost:~$./long-unix-script.sh


缺乏灵活性,需要用户持续当前的会话,但是如果我们执行上面的命令是:
avi@localhost:~$screen ./long-unix-script.sh


它能在不同会话间断开或重连。当一个命令正在执行时按“Ctrl + A”然后再按“d”来断开。

重新连接运行:
avi@localhost:~$screen -r 4980.pts-0.localhost


注解:在这里,这个命令的稍后的部分是screen id,你能用‘screen -ls’命令查看。欲了解更多关于screen命令和它们的用法,请阅读我们的一些帮助文章:10
个screen命令的示例。


20. file

No!写上面的命令不是笔误. ‘file’是一个能提供关于文件类型信息的命令。
avi@localhost:~$file 34.odt
34.odt: OpenDocument Text


21. id

上面的命令会打印真正的和有效的用户和组的id。
avi@localhost:~$id


输出样式

uid=1000(avi) gid=1000(avi)

groups=1000(avi),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),109(ne


22. ^foo^bar 命令

在一个实例中运行修改后的最后一个命令。假设我需要运行一个命令‘ls -l‘来详细列出‘Desktop’目录下的内容。意外地,你打了‘lls -l‘。所以你需要重新打整个命令或者使用导航键编辑前面的命令。当你的命令很长时这个很痛苦。
avi@localhost:~/Desktop$lls -l
bash: lls: command not found

avi@localhost:~/Desktop$^lls^ls

ls -l
total 7489440

drwxr-xr-x 2 avi avi 36864 Nov 13 2012 101MSDCF
-rw-r--r-- 1 avi avi 206833 Nov 5 15:27 1.jpg
-rw-r--r-- 1 avi avi 158951 Nov 5 15:27 2.jpg
-rw-r--r-- 1 avi avi 90624 Nov 5 12:59 Untitled 1.doc


注意:在上面的替换中我们使用“typo(被替换的)original_command(原始命令)”。警告!这个命令可能会非常危险!如果你有意或者无意地打错了系统命令或者任何像rm
-rf那样的风险命令的话!


23. > file.txt 命令

这个命令会刷新文件的内容而不需删除然后创建相同的文件。当我们需要反复输出,或者在相同的文件上记录日志时,这个命令就非常有用。

我有一个有很多文字的‘test.txt’文件在我的‘Desktop‘上。
avi@localhost:~/Desktop$cat test.txt

Linux
GNU
Debian
Fedora
kali
ubuntu
git
Linus
Torvalds

avi@localhost:~/Desktop$> test.txt
avi@localhost:~/Desktop$cat test.txt


注意:再说一次,这个命令可能很危险!永远不要尝试刷新系统文件或者某篇日志文件的内容。如果你这么做了,你可能会遭遇严重的问题!


24. at 命令

‘at‘命令与cron
命令相似也可用于安排一个任务或者在某个特定时刻执行命令。
avi@localhost:~/Desktop$echo "ls -l > /dev/pts/0" | at 14:012



avi@localhost:~/Desktop$echo "ls -l > /dev/pts/0" | at 2:12 PM


示例输出
-rw-r--r-- 1 avi  avi      220492 Nov  1 13:49 Screenshot-1.png
-rw-r--r-- 1 root root        358 Oct 17 10:11 sources.list
-rw-r--r-- 1 avi  avi  4695982080 Oct 10 20:29 squeeze.iso
..
..
-rw-r--r-- 1 avi  avi       90624 Nov  5 12:59 Untitled 1.doc
-rw-r--r-- 1 avi  avi       96206 Nov  5 12:56 Untitled 1.odt
-rw-r--r-- 1 avi  avi        9405 Nov 12 23:22 Untitled.png


注意:echo “ls -l”的意思是,将这串命令(这里是 ls -l)输出在标准终端上。你可以用你需要或者选择的命令替代‘ls -l‘。

> :重定向输出

/dev/pts/0: 这是输出设备和/或文件,输出到指定的地方,这里输出在终端(/dev/pts/0)。

就我而言,此时我的tty在/dev/pts/0。你可以用tty命令检查你的tty。
avi@localhost:~/Desktop$tty

/dev/pts/0


注意: ‘at‘会在系统时间匹配到特定时间时会尽快执行。


25. du -h –max-depth=1 命令

下面的命令以人类可读的形式输出当前目录的子目录的大小。
avi@localhost:/home/avi/Desktop# du -h --max-depth=1

38M     ./test
1.1G    ./shivji
42M     ./drupal
6.9G    ./101MSDCF
16G .


注意:上面的命令在检查系统使用率是非常有用。


26. expr 命令

‘expr‘不是那么鲜为人知的命令。这个命令在终端中计算简单的算数时非常有用。
avi@localhost:/home/avi/Desktop# expr 2 + 3
5

avi@localhost:/home/avi/Desktop# expr 6 – 3
3

avi@localhost:/home/avi/Desktop# expr 12 / 3
4

avi@localhost:/home/avi/Desktop# expr 2 \* 9
18


27. look 命令

在终端上从英文字典上查单词以防混淆。比如说,我记不清了是该拼成carrier还是carieer。

avi@localhost:/home/avi/Documents# look car
Cara
Cara's
…
...
carps
carpus
carpus's
carrel
carrel's
carrels
carriage
carriage's
carriages
carriageway
carriageway's
carried
carrier
carrier's
carriers
carries
…
...
caryatids


上面的命令会显示字典上所有以'car'开头的单词。我得到了我想找的。


28. yes 命令

另外一个命令在通常基础上并不会经常使用,但是在脚本语言和系统管理时非常有用。

这个命令会持续地输出给定的字符串,直到由你的中断命令打断。
avi@localhost:~/Desktop$yes "Tecmint is one of the best site dedicated to Linux, how to"

Tecmint is one of the best site dedicated to Linux, how to
Tecmint is one of the best site dedicated to Linux, how to
Tecmint is one of the best site dedicated to Linux, how to
Tecmint is one of the best site dedicated to Linux, how to


...
Tecmint is one of the best site dedicated to Linux, how to
Tecmint is one of the best site dedicated to Linux, how to
Tecmint is one of the best site dedicated to Linux, how to


29. factor 命令

factor实际是一个源于数学的命令。这个命令会输出所有给定数字的因数。
avi@localhost:~/Desktop$factor 22
22: 2 11

avi@localhost:~/Desktop$factor 21
21: 3 7

avi@localhost:~/Desktop$factor 11
11: 11


30. ping -i 60 -a IP_address

我们都用ping命令检测服务器是否连通。我通常ping google,来检测我是否连接到了因特网。

当你等待或者持续盯着你的终端等待命令的回应或者等待服务器的连接时,有时是很气人的。

一旦服务器连通就有一个声音如何(译注:下面命令是等60秒PING一次)?
avi@localhost:~/Desktop$ping -i 60 -a www.google.com

PING www.google.com (74.125.200.103) 56(84) bytes of data.
64 bytes from www.google.com (74.125.200.103): icmp_req=1 ttl=44 time=105 ms
64 bytes from 74.125.200.103: icmp_req=2 ttl=44 time=281 ms


注意,当你发现命令不会返回声音时。请确保你的系统不是静音的,声音已经在‘sound preferences(声音选项)‘ 中启用并确保勾选了‘Enable window and window sound‘。


31. tac 命令

这个命令很有趣,他会以倒序输出文本文件的内容。也就是从最后一行到第一行。

在home目录下,我的Documents目录下有一个35.txt文件。用cat
命令检查内容。
avi@localhost:~/Documents$cat 35.txt


示例输出

Linux is built with certain powerful tools, which are unavailable in windows.
One of such important tool is Shell Scripting. Windows however comes with such a tool but as usual it is much weak as compared to it's Linux Counterpart.
Shell scripting/programming makes it possible to execute command(s), piped to get desired output in order to automate day-to-day usages.

现在用tac命令反转文件内容(译注:当然,我们知道cat反转过来就是tac)。
<code style="padding:0px; margin:0px">avi@localhost:~/Documents$tac 35.txt </code>


示例输出

Shell scripting/programming makes it possible to execute command(s), piped to get desired output in order to automate day-to-day usages.
One of such important tool is Shell Scripting. Windows however comes with such a tool but as usual it is much weak as compared to it's Linux Counterpart.
Linux is built with certain powerful tools, which are unavailable in windows.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: