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

鲜为人知的Linux命令续

2013-11-24 15:47 267 查看


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> + .

上面的组合键事实上不是一个命令,而是传递最后一个命令参数到提示符后的快捷键,以输入命令的倒序方式传递命令。按住 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(netdev),111(bluetooth),117(scanner)



22. ^foo^bar 命令

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

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

01.
avi@localhost:~/Desktop$
lls -l


02.
bash
:
lls:
command
not
found


03.


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


05.


06.
ls
-l


07.
total
7489440


08.


09.
drwxr-xr-x
2 avi  avi       36864 Nov 13  2012 101MSDCF


10.
-rw-r--r--
1 avi  avi      206833 Nov  5 15:27 1.jpg


11.
-rw-r--r--
1 avi  avi      158951 Nov  5 15:27 2.jpg


12.
-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‘上。

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

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


02.


03.
Linux


04.
GNU


05.
Debian


06.
Fedora


07.
kali


08.
ubuntu


09.
git


10.
Linus


11.
Torvalds


12.


13.


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


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


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


24. at 命令

‘at‘命令与cron
命令相似也可用于安排一个任务或者在某个特定时刻执行命令。

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

1.
avi@localhost:~/Desktop$
echo
"ls
-l > /dev/pts/0"
|
at 14:012




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

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


示例输出

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

1.
-rw-r--r--
1 avi  avi      220492 Nov  1 13:49 Screenshot-1.png


2.
-rw-r--r--
1 root root        358 Oct 17 10:11 sources.list


3.
-rw-r--r--
1 avi  avi  4695982080 Oct 10 20:29 squeeze.iso


4.
..


5.
..


6.
-rw-r--r--
1 avi  avi       90624 Nov  5 12:59 Untitled 1.doc


7.
-rw-r--r--
1 avi  avi       96206 Nov  5 12:56 Untitled 1.odt


8.
-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。

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

1.
avi@localhost:~/Desktop$
tty


2.


3.
/dev/pts/0


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


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

下面的命令以人类可读的形式输出当前目录的子目录的大小。

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

1.
avi@localhost:/home/avi/Desktop
#
du -h --max-depth=1


2.


3.
38M
./
test


4.
1.1G
./shivji


5.
42M
./drupal


6.
6.9G
./101MSDCF


7.
16G
.


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


26. expr 命令

‘expr‘不是那么鲜为人知的命令。这个命令在终端中计算简单的算数时非常有用。

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

01.
avi@localhost:/home/avi/Desktop
#
expr 2 + 3


02.
5


03.


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


05.
3


06.


07.
avi@localhost:/home/avi/Desktop
#
expr 12 / 3


08.
4


09.


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


11.
18



27. look 命令

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

avi@localhost:/home/avi/Documents# look car

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

01.
Cara


02.
Cara's


03.


04.
...


05.
carps


06.
carpus


07.
carpus's


08.
carrel


09.
carrel's


10.
carrels


11.
carriage


12.
carriage's


13.
carriages


14.
carriageway


15.
carriageway's


16.
carried


17.
carrier


18.
carrier's


19.
carriers


20.
carries


21.


22.
...


23.
caryatids


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


28. yes 命令

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

这个命令会持续地输出给定的字符串,直到由你的中断命令打断。

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

01.
avi@localhost:~/Desktop$
yes
"Tecmint
is one of the best site dedicated to Linux, how to"


02.


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


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


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


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


07.


08.


09.
...


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


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


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



29. factor 命令

factor实际是一个源于数学的命令。这个命令会输出所有给定数字的因数。

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

1.
avi@localhost:~/Desktop$
factor 22


2.
22:
2 11


3.


4.
avi@localhost:~/Desktop$
factor 21


5.
21:
3 7


6.


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


8.
11:
11



30. ping -i 60 -a IP_address

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

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

一旦服务器连通就有一个声音如何(译注:下面命令是等60秒PING一次)?

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

1.
avi@localhost:~/Desktop$
ping
-i
60 -a www.google.com


2.


3.
PING
www.google.com (74.125.200.103) 56(84) bytes of data.


4.
64
bytes from www.google.com (74.125.200.103): icmp_req=1 ttl=44
time
=105
ms


5.
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
命令检查内容。

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

1.
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)。

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

1.
<code>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.

via: http://www.tecmint.com/10-lesser-known-linux-commands-part-2/

译者:Luoxcat 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出

本文地址:http://linux.cn/thread/11931/1/1/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: