11 个很少人知道但很有用的 Linux 命令

仅仅用于软件推荐,不适合发求软件或软件使用问题方面的贴子
回复
levee
帖子: 3030
注册时间: 2009-10-03 23:31

11 个很少人知道但很有用的 Linux 命令

#1

帖子 levee » 2013-10-29 17:23

英文原文:11 Lesser Known Useful Linux Commands

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

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



1. sudo !!命令

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

代码: 全选

$ apt-get update

E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied) 
E: Unable to lock directory /var/lib/apt/lists/ 
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied) 
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

代码: 全选

$ sudo !!

sudo apt-get update 
[sudo] password for server: 
…
..
Fetched 474 kB in 16s (28.0 kB/s) 
Reading package lists... Done 
server@localhost:~$
2. python命令

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

代码: 全选

# python -m SimpleHTTPServer
25080743_TM5j.png
3. mtr命令

我们大多数都熟悉pingtraceroute。那对于把两个命令的功能合二为一的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
25080746_rD1T.png
4. Ctrl+x+e命令

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

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

5. nl命令

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

代码: 全选

# cat one.txt 

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

代码: 全选

# nl one.txt 

1 fedora 
2 debian 
3 arch 
4 slack 
5 suse
6. shuf命令

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

代码: 全选

# ls 

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

代码: 全选

#  ls | shuf (shuffle Input)

Music 
Documents 
Templates 
Pictures 
Public 
Desktop 
Downloads 
Videos

代码: 全选

#  ls | shuf -n1 (pick on random selection)

Public

代码: 全选

# ls | shuf -n1 

Videos

代码: 全选

# ls | shuf -n1 

Templates

代码: 全选

# ls | shuf -n1 

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

7. ss命令

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

代码: 全选

# ss 

State      Recv-Q Send-Q      Local Address:Port          Peer Address:Port   
ESTAB      0      0           192.168.1.198:41250        *.*.*.*:http    
CLOSE-WAIT 1      0               127.0.0.1:8000             127.0.0.1:41393   
ESTAB      0      0           192.168.1.198:36239        *.*.*.*:http    
ESTAB      310    0               127.0.0.1:8000             127.0.0.1:41384   
ESTAB      0      0           192.168.1.198:41002       *.*.*.*:http    
ESTAB      0      0               127.0.0.1:41384            127.0.0.1:8000
8. last命令

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

代码: 全选

#  last 
server   pts/0        :0               Tue Oct 22 12:03   still logged in   
server   tty8         :0               Tue Oct 22 12:02   still logged in   
…
...
(unknown tty8         :0               Tue Oct 22 12:02 - 12:02  (00:00)    
server   pts/0        :0               Tue Oct 22 10:33 - 12:02  (01:29)    
server   tty7         :0               Tue Oct 22 10:05 - 12:02  (01:56)    
(unknown tty7         :0               Tue Oct 22 10:04 - 10:05  (00:00)    
reboot   system boot  3.2.0-4-686-pae  Tue Oct 22 10:04 - 12:44  (02:39)    

wtmp begins Fri Oct  4 14:43:17 2007
9. curl ifconfig.me

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

代码: 全选

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

10. tree命令

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

代码: 全选

# tree
. 
|-- Desktop 
|-- Documents 
|   `-- 37.odt 
|-- Downloads 
|   |-- attachments.zip 

|   |-- ttf-indic-fonts_0.5.11_all.deb 
|   |-- ttf-indic-fonts_1.1_all.deb 
|   `-- wheezy-nv-install.sh 
|-- Music 
|-- Pictures 
|   |-- Screenshot from 2013-10-22 12:03:49.png 
|   `-- Screenshot from 2013-10-22 12:12:38.png 
|-- Public 
|-- Templates 
`-- Videos 

10 directories, 23 files
11. pstree

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

代码: 全选

# pstree 
init─┬─NetworkManager───{NetworkManager} 
     ├─accounts-daemon───{accounts-daemon} 
     ├─acpi_fakekeyd 
     ├─acpid 
     ├─apache2───10*[apache2] 
     ├─at-spi-bus-laun───2*[{at-spi-bus-laun}] 
     ├─atd 
     ├─avahi-daemon───avahi-daemon 
     ├─bluetoothd 
     ├─colord───{colord} 
     ├─colord-sane───2*[{colord-sane}] 
     ├─console-kit-dae───64*[{console-kit-dae}] 
     ├─cron 
     ├─cupsd 
     ├─2*[dbus-daemon] 
     ├─dbus-launch 
     ├─dconf-service───2*[{dconf-service}] 
     ├─dovecot─┬─anvil 
     │         ├─config 
     │         └─log 
     ├─exim4 
     ├─gconfd-2 
     ├─gdm3─┬─gdm-simple-slav─┬─Xorg 
     │      │                 ├─gdm-session-wor─┬─x-session-manag─┬─evolution-a+ 
     │      │                 │                 │                 ├─gdu-notific+ 
     │      │                 │                 │                 ├─gnome-scree+ 
     │      │                 │                 │                 ├─gnome-setti+ 
     │      │                 │                 │                 ├─gnome-shell+++ 
     │      │                 │                 │                 ├─nm-applet──+++ 
     │      │                 │                 │                 ├─ssh-agent 
     │      │                 │                 │                 ├─tracker-min+ 
     │      │                 │                 │                 ├─tracker-sto+ 
     │      │                 │                 │                 └─3*[{x-sessi+ 
     │      │                 │                 └─2*[{gdm-session-wor}] 
     │      │                 └─{gdm-simple-slav} 
     │      └─{gdm3} 
     ├─6*[getty] 
     ├─gnome-keyring-d───9*[{gnome-keyring-d}] 
     ├─gnome-shell-cal───2*[{gnome-shell-cal}] 
     ├─goa-daemon───{goa-daemon} 
     ├─gsd-printer───{gsd-printer} 
     ├─gvfs-afc-volume───{gvfs-afc-volume}
目前为止就这么多。在下篇文章中,我将涉及一些其他很少有人知道的有趣的Linux命令。到那时连接 Tecmint保持收看。喜欢和分享将有助于我们传播。

来源:oschina
头像
Methuselar
帖子: 122
注册时间: 2009-06-04 12:06
联系:

Re: 11 个很少人知道但很有用的 Linux 命令

#2

帖子 Methuselar » 2013-10-29 17:33

阿三阿阿三...
Mea Culpa!
头像
tang.zhe
帖子: 1505
注册时间: 2010-07-15 13:31

Re: 11 个很少人知道但很有用的 Linux 命令

#3

帖子 tang.zhe » 2013-10-29 18:14

:em11
xyq164288
帖子: 80
注册时间: 2009-01-25 15:13

Re: 11 个很少人知道但很有用的 Linux 命令

#4

帖子 xyq164288 » 2014-03-07 17:01

:em11 学习了
头像
月下叹逍遥
论坛版主
帖子: 33994
注册时间: 2010-10-07 14:23
系统: Archdows10
来自: 某系某星某洲某国某省某市
联系:

Re: 11 个很少人知道但很有用的 Linux 命令

#5

帖子 月下叹逍遥 » 2014-03-07 17:16

Ctrl+x+e,这是要变成章鱼的节奏吗?
浮生七十今三十,从此凄惶未可知
晨★梦一柯南
帖子: 335
注册时间: 2010-12-28 17:03

Re: 11 个很少人知道但很有用的 Linux 命令

#6

帖子 晨★梦一柯南 » 2014-03-07 17:27

:em11 有些常用, 有些就忘了...
头像
susbarbatus
帖子: 2966
注册时间: 2010-04-10 16:14
系统: Arch Linux

Re: 11 个很少人知道但很有用的 Linux 命令

#7

帖子 susbarbatus » 2014-03-07 18:23

python 不能算吧……
沉迷将棋中……
头像
highwind
帖子: 1362
注册时间: 2008-09-05 23:31
系统: LinuxMint17

Re: 11 个很少人知道但很有用的 Linux 命令

#8

帖子 highwind » 2014-03-08 4:45

!!普通用户也是那个功能。
nl这个很好,记下了。 :em02
头像
ceclinux
帖子: 308
注册时间: 2013-01-17 2:42
系统: Ubuntu 12.04LTS

Re: 11 个很少人知道但很有用的 Linux 命令

#9

帖子 ceclinux » 2014-03-08 14:16

sudo !!很好。。。
Ubuntu 12.04 LTS
laptop:Acer 4750G CPU:I5-2410
内存:2+2G 硬盘:500+750
http://www.ceclinux.org
头像
highwind
帖子: 1362
注册时间: 2008-09-05 23:31
系统: LinuxMint17

Re: 11 个很少人知道但很有用的 Linux 命令

#10

帖子 highwind » 2014-03-12 5:19

说几个好用的快捷键吧:
Ctrl + R – Allows you to type a part of the command you're looking for and finds it

Ctrl + A – Return to the start of the command you're typing
Ctrl + E – Go to the end of the command you're typing
Ctrl + U – Cut everything before the cursor to a special clipboard, erases the whole line
Ctrl + K – Cut everything after the cursor to a special clipboard
Ctrl + Y – Paste from the special clipboard that Ctrl + U and Ctrl + K save their data to
Ctrl + T – Swap the two characters before the cursor (you can actually use this to transport a character from the left to the right, try it!)
Ctrl + W – Delete the word / argument left of the cursor in the current line
回复