Merge pull request #2573 from martin2011qi/master

translated
This commit is contained in:
joeren 2015-04-02 08:20:17 +08:00
commit cc8c74bd3a
2 changed files with 266 additions and 268 deletions

View File

@ -1,268 +0,0 @@
translating by martin.
11 Linux Terminal Commands That Will Rock Your World
================================================================================
I have been using Linux for about 10 years and what I am going to show you in this article is a list of Linux commands, tools and clever little tricks that I wish somebody had shown me from the outset instead of stumbling upon them as I went along.
![Linux Keyboard Shortcuts.](http://f.tqn.com/y/linux/1/L/m/J/1/keyboardshortcuts.png)
Linux Keyboard Shortcuts.
### 1. Useful Command Line Keyboard Shortcuts ###
The following keyboard shortcuts are incredibly useful and will save you loads of time:
- CTRL + U - Cuts text up until the cursor.
- CTRL + K - Cuts text from the cursor until the end of the line
- CTRL + Y - Pastes text
- CTRL + E - Move cursor to end of line
- CTRL + A - Move cursor to the beginning of the line
- ALT + F - Jump forward to next space
- ALT + B - Skip back to previous space
- ALT + Backspace - Delete previous word
- CTRL + W - Cut word behind cursor
- Shift + Insert - Pastes text into terminal
Just so that the commands above make sense look at the next line of text.
sudo apt-get intall programname
As you can see I have a spelling error and for the command to work I would need to change "intall" to "install".
Imagine the cursor is at the end of the line. There are various ways to get back to the word install to change it.
I could press ALT + B twice which would put the cursor in the following position (denoted by the ^ symbol):
sudo apt-get^intall programname
Now you could press the cursor key and insert the ''s' into install.
Another useful command is "shift + insert" especially If you need to copy text from a browser into the terminal.
![](http://f.tqn.com/y/linux/1/L/n/J/1/sudotricks2.png)
### 2. SUDO !! ###
You are going to really thank me for the next command if you don't already know it because until you know this exists you curse yourself every time you enter a command and the words "permission denied" appear.
- sudo !!
How do you use sudo !!? Simply. Imagine you have entered the following command:
apt-get install ranger
The words "Permission denied" will appear unless you are logged in with elevated privileges.
sudo !! runs the previous command as sudo. So the previous command now becomes:
sudo apt-get install ranger
If you don't know what sudo is [start here][1].
![Pause Terminal Applications.](http://f.tqn.com/y/linux/1/L/o/J/1/pauseapps.png)
Pause Terminal Applications.
### 3. Pausing Commands And Running Commands In The Background ###
I have already written a guide showing how to run terminal commands in the background.
- CTRL + Z - Pauses an application
- fg - Returns you to the application
So what is this tip about?
Imagine you have opened a file in nano as follows:
sudo nano abc.txt
Halfway through typing text into the file you realise that you quickly want to type another command into the terminal but you can't because you opened nano in foreground mode.
You may think your only option is to save the file, exit nano, run the command and then re-open nano.
All you have to do is press CTRL + Z and the foreground application will pause and you will be returned to the command line. You can then run any command you like and when you have finished return to your previously paused session by entering "fg" into the terminal window and pressing return.
An interesting thing to try out is to open a file in nano, enter some text and pause the session. Now open another file in nano, enter some text and pause the session. If you now enter "fg" you return to the second file you opened in nano. If you exit nano and enter "fg" again you return to the first file you opened within nano.
![nohup.](http://f.tqn.com/y/linux/1/L/p/J/1/nohup3.png)
nohup.
### 4. Use nohup To Run Commands After You Log Out Of An SSH Session ###
The [nohup command][2] is really useful if you use the ssh command to log onto other machines.
So what does nohup do?
Imagine you are logged on to another computer remotely using ssh and you want to run a command that takes a long time and then exit the ssh session but leave the command running even though you are no longer connected then nohup lets you do just that.
For instance I use my [Raspberry PI][3] to download distributions for review purposes.
I never have my Raspberry PI connected to a display nor do I have a keyboard and mouse connected to it.
I always connect to the Raspberry PI via [ssh][4] from a laptop. If I started downloading a large file on the Raspberry PI without using the nohup command then I would have to wait for the download to finish before logging off the ssh session and before shutting down the laptop. If I did this then I may as well have not used the Raspberry PI to download the file at all.
To use nohup all I have to type is nohup followed by the command as follows:
nohup wget http://mirror.is.co.za/mirrors/linuxmint.com/iso//stable/17.1/linuxmint-17.1-cinnamon-64bit.iso &
![Schedule tasks with at.](http://f.tqn.com/y/linux/1/L/q/J/1/at.png)
Schedule tasks with at.
### 5. Running A Linux Command 'AT' A Specific Time ###
The 'nohup' command is good if you are connected to an SSH server and you want the command to remain running after logging out of the SSH session.
Imagine you want to run that same command at a specific point in time.
The 'at' command allows you to do just that. 'at' can be used as follows.
at 10:38 PM Fri
at> cowsay 'hello'
at> CTRL + D
The above command will run the program [cowsay][5] at 10:38 PM on Friday evening.
The syntax is 'at' followed by the date and time to run.
When the at> prompt appears enter the command you want to run at the specified time.
The CTRL + D returns you to the cursor.
There are lots of different date and time formats and it is worth checking the man pages for more ways to use 'at'.
![](http://f.tqn.com/y/linux/1/L/l/J/1/manmost.png)
### 6. Man Pages ###
Man pages give you an outline of what commands are supposed to do and the switches that can be used with them.
The man pages are kind of dull on their own. (I guess they weren't designed to excite us).
You can however do things to make your usage of man more appealing.
export PAGER=most
You will need to install 'most; for this to work but when you do it makes your man pages more colourful.
You can limit the width of the man page to a certain number of columns using the following command:
export MANWIDTH=80
Finally, if you have a browser available you can open any man page in the default browser by using the -H switch as follows:
man -H <command>
Note this only works if you have a default browser set up within the $BROWSER environment variable.
![View Processes With htop.](http://f.tqn.com/y/linux/1/L/r/J/1/nohup2.png)
View Processes With htop.
### 7. Use htop To View And Manage Processes ###
Which command do you currently use to find out which processes are running on your computer? My bet is that you are using '[ps][6]' and that you are using various switches to get the output you desire.
Install '[htop][7]'. It is definitely a tool you will wish that you installed earlier.
htop provides a list of all running processes in the terminal much like the file manager in Windows.
You can use a mixture of function keys to change the sort order and the columns that are displayed. You can also kill processes from within htop.
To run htop simply type the following into the terminal window:
htop
![Command Line File Manager - Ranger.](http://f.tqn.com/y/linux/1/L/s/J/1/ranger.png)
Command Line File Manager - Ranger.
### 8. Navigate The File System Using ranger ###
If htop is immensely useful for controlling the processes running via the command line then [ranger][8] is immensely useful for navigating the file system using the command line.
You will probably need to install ranger to be able to use it but once installed you can run it simply by typing the following into the terminal:
ranger
The command line window will be much like any other file manager but it works left to right rather than top to bottom meaning that if you use the left arrow key you work your way up the folder structure and the right arrow key works down the folder structure.
It is worth reading the man pages before using ranger so that you can get used to all keyboard switches that are available.
![Cancel Linux Shutdown.](http://f.tqn.com/y/linux/1/L/t/J/1/shutdown.png)
Cancel Linux Shutdown.
### 9. Cancel A Shutdown ###
So you started the [shutdown][9] either via the command line or from the GUI and you realised that you really didn't want to do that.
shutdown -c
Note that if the shutdown has already started then it may be too late to stop the shutdown.
Another command to try is as follows:
- [pkill][10] shutdown
![Kill Hung Processes With XKill.](http://f.tqn.com/y/linux/1/L/u/J/1/killhungprocesses.png)
Kill Hung Processes With XKill.
### 10. Killing Hung Processes The Easy Way ###
Imagine you are running an application and for whatever reason it hangs.
You could use 'ps -ef' to find the process and then kill the process or you could use 'htop'.
There is a quicker and easier command that you will love called [xkill][11].
Simply type the following into a terminal and then click on the window of the application you want to kill.
xkill
What happens though if the whole system is hanging?
Hold down the 'alt' and 'sysrq' keys on your keyboard and whilst they are held down type the following slowly:
- [REISUB][12]
This will restart your computer without having to hold in the power button.
![youtube-dl.](http://f.tqn.com/y/linux/1/L/v/J/1/youtubedl2.png)
youtube-dl.
### 11. Download Youtube Videos ###
Generally speaking most of us are quite happy for Youtube to host the videos and we watch them by streaming them through our chosen media player.
If you know you are going to be offline for a while (i.e. due to a plane journey or travelling between the south of Scotland and the north of England) then you may wish to download a few videos onto a pen drive and watch them at your leisure.
All you have to do is install youtube-dl from your package manager.
You can use youtube-dl as follows:
youtube-dl url-to-video
You can get the url to any video on Youtube by clicking the share link on the video's page. Simply copy the link and paste it into the command line (using the shift + insert shortcut).
### Summary ###
I hope that you found this list useful and that you are thinking "i didn't know you could do that" for at least 1 of the 11 items listed.
--------------------------------------------------------------------------------
via: http://linux.about.com/od/commands/tp/11-Linux-Terminal-Commands-That-Will-Rock-Your-World.htm
作者:[Gary Newell][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://linux.about.com/bio/Gary-Newell-132058.htm
[1]:http://linux.about.com/cs/linux101/g/sudo.htm
[2]:http://linux.about.com/library/cmd/blcmdl1_nohup.htm
[3]:http://linux.about.com/od/mobiledevicesother/a/Raspberry-Pi-Computer-Running-Linux.htm
[4]:http://linux.about.com/od/commands/l/blcmdl1_ssh.htm
[5]:http://linux.about.com/cs/linux101/g/cowsay.htm
[6]:http://linux.about.com/od/commands/l/blcmdl1_ps.htm
[7]:http://www.linux.com/community/blogs/133-general-linux/745323-5-commands-to-check-memory-usage-on-linux
[8]:http://ranger.nongnu.org/
[9]:http://linux.about.com/od/commands/l/blcmdl8_shutdow.htm
[10]:http://linux.about.com/library/cmd/blcmdl1_pkill.htm
[11]:http://linux.about.com/od/funnymanpages/a/funman_xkill.htm
[12]:http://blog.kember.net/articles/reisub-the-gentle-linux-restart/

View File

@ -0,0 +1,266 @@
11个Linux终端命令让你的世界摇滚起来
================================================================================
我已经用了十年的Linux了通过今天这篇文章我将向大家展示一系列的我希望一开始就有人教导而不是曾在我成长道路上绊住我的Linux命令、工具和花招。
![Linux Keyboard Shortcuts.](http://f.tqn.com/y/linux/1/L/m/J/1/keyboardshortcuts.png)
Linux的快捷键。
### 1. 命令行日常系快捷键 ###
如下的快捷方式非常有用,能够极大的提升你的工作效率:
- CTRL + U - 剪切光标前的内容
- CTRL + K - 剪切光标至行末的内容
- CTRL + Y - 粘贴
- CTRL + E - 移动光标到行末
- CTRL + A - 移动光标到行首
- ALT + F - 跳向下一个空格
- ALT + B - 跳回上一个空格
- ALT + Backspace - 删除前一个字
- CTRL + W - 剪切光标后一个字
- Shift + Insert - 向终端内粘贴文本
那么为了让上诉内容更易理解来看下面的这行命令。
sudo apt-get intall programname
如你所见命令中存在拼写错误为了正常执行需要把“intall”替换成“install”。
想象现在光标正在行末我们有很多的方法将她退回单词install并替换它。
我可以按两次ALT+B这样光标就会在如下的位置这里用^代替光标的位置)。
sudo apt-get^intall programname
现在你可以按两下方向键并将“s”插入到install中去了。
如果你想将浏览器中的文本复制到终端,可以使用快捷键"shift + insert"。
![](http://f.tqn.com/y/linux/1/L/n/J/1/sudotricks2.png)
### 2. SUDO !! ###
这个命令如果你还不知道我觉得你应该好好感谢我因为如果你不知道那每次你在输入长串命令后看到“permission denied”后一定会痛恼不堪。
- sudo !!
如何使用sudo !!?很简单。试想你刚输入了如下命令:
apt-get install ranger
一定会出现"Permission denied"除非你的登录了足够高权限的账户。
sudo !!就会用sudo的形式运行上一条命令。所以上一条命令可以看成是这样
sudo apt-get install ranger
如果你不知道什么是sudo[戳这里][1]。
![Pause Terminal Applications.](http://f.tqn.com/y/linux/1/L/o/J/1/pauseapps.png)
暂停终端运行的应用程序。
### 3. 暂停并在后台运行命令 ###
我曾经写过一篇如何在终端后台运行命令的指南。
- CTRL + Z - 暂停应用程序
- fg - 重新将程序唤到前台
如何使用这个技巧呢?
试想你正用nano编辑一个文件
sudo nano abc.txt
文件编辑到一半你意识到你需要马上在终端输入些命令但是nano在前台运行让你不能输入。
你可能觉得唯一的方法就是保存文件推出nano运行命令以后在重新打开nano。
其实你只要按CTRL + Z前台的命令就会暂停画面就切回到命令行了。然后你就能运行你想要运行命令等命令运行完后在终端窗口输入“fg”就可以回到先前暂停的任务。
有一个尝试非常有趣就是用nano打开文件输入一些东西然后暂停会话。再用nano打开另一个文件输入一些什么后再暂停会话。如果你输入“fg”你将回到第二个用nano打开的文件。只有退出nano再输入“fg”你才会回到第一个用nano打开的文件。
![nohup.](http://f.tqn.com/y/linux/1/L/p/J/1/nohup3.png)
nohup.
### 4. 使用nohup在登出SSH会话后仍运行命令 ###
如果你用ssh登录别的机器时[nohup命令]真的非常有用。
那么怎么使用nohup呢
想象一下你使用ssh远程登录到另一台电脑上你运行了一条非常耗时的命令然后退出了ssh会话不过命令仍在执行。而nohup可以将这一场景变成现实。
举个例子以测试为目的我用[树莓派][3]来下载发行版。
我绝对不会给我的树莓派外接显示器、键盘或鼠标。
一般我总是用[SSH] [4]从笔记本电脑连接到树莓派。如果我在不用nohup的情况下使用树莓派下载大型文件那我就必须等待到下载完成后才能登出ssh会话关掉笔记本。如果是这样那我为什么要使用树莓派下文件呢
使用nohup的方法也很简单只需如下例中在nohup后输入要执行的命令即可
nohup wget http://mirror.is.co.za/mirrors/linuxmint.com/iso//stable/17.1/linuxmint-17.1-cinnamon-64bit.iso &
![Schedule tasks with at.](http://f.tqn.com/y/linux/1/L/q/J/1/at.png)
At管理任务日程
### 5. 特定的时间运行Linux命令 ###
nohup命令在你用SSH连接到服务器并在上面保持执行SSH登出前任务的时候十分有用。
想一下如果你需要在特定的时间执行同一个命令,这种情况该怎么办呢?
命令at就能妥善解决这一情况。以下是at使用示例。
at 10:38 PM Fri
at> cowsay 'hello'
at> CTRL + D
上面的命令能在周五下午10时38分运行程序[cowsay] [5]。
使用的语法就是at后追加日期时间。
当at>提示符出现后就可以输入你想在那个时间运行的命令了。
CTRL + D返回终端。
还有许多日期和时间的格式都是值得的你好好翻一翻at的man手册来找到更多的使用方式。
![](http://f.tqn.com/y/linux/1/L/l/J/1/manmost.png)
### 6. Man手册 ###
Man手册会为你列出命令和参数的使用大纲教你如何使用她们。
Man手册看起开沉闷呆板。我思忖她们也不是被设计来娱乐我们的
不过这不代表你不能做些什么来使她们变得性感点。
export PAGER=most
你需要 most她会使你的你的man手册的色彩更加绚丽。
你可以用一下命令给man手册设定指定的行长
export MANWIDTH=80
最后,如果你有浏览器,你可以使用-H在默认浏览器中打开任意的man页。
man -H <command>
注意啦,以上的命令只有在你将默认的浏览器已经设置到环境变量$BROWSER中了之后才效果哟。
![View Processes With htop.](http://f.tqn.com/y/linux/1/L/r/J/1/nohup2.png)
使用htop查看进程。
### 7. 使用htop查看和管理进程 ###
你用哪个命令找出电脑上正在运行的进程的呢?我敢打赌是‘[ps][6]’并在其后加不同的参数来得到你所想要的不同输出。
安装‘[htop][7]’吧!绝对让你相见恨晚。
htop在终端中将进程以列表的方式呈现有点类似于Windows中的任务管理器。
你可以使用功能键的组合来切换排列的方式和展示出来的项。你也可以在htop中直接杀死进程。
在终端中简单的输入htop即可运行。
htop
![Command Line File Manager - Ranger.](http://f.tqn.com/y/linux/1/L/s/J/1/ranger.png)
命令行文件管理 - Ranger.
### 8. 使用ranger浏览文件系统 ###
如果说htop是命令行进程控制的好帮手那么[ranger][8]就是命令行浏览文件系统的好帮手。
你在用之前可能需要先安装,不过一旦安装了以后就可以在命令行输入以下命令启动她:
ranger
在命令行窗口中ranger和一些别的文件管理器很像但是她是左右结构的比起上下的来意味着你按左方向键你将前进到上一个文件夹结构而右方向键则会切换到下一个。
在使用前ranger的man手册还是值得一读的这样你就可以用快捷键操作ranger了。
![Cancel Linux Shutdown.](http://f.tqn.com/y/linux/1/L/t/J/1/shutdown.png)
Linux取消关机。
### 9. 取消关机 ###
无论是在命令行还是图形用户界面[关机][9]后发现自己不是真的想要关机。
shutdown -c
需要注意的是,如果关机已经开始则有可能来不及停止关机。
以下是另一个可以尝试命令:
- [pkill][10] shutdown
![Kill Hung Processes With XKill.](http://f.tqn.com/y/linux/1/L/u/J/1/killhungprocesses.png)
使用XKill杀死挂起进程。
### 10. 杀死挂起进程的简单方法 ###
想象一下,你正在运行的应用程序不明原因的僵死了。
你可以使用ps -ef来找到该进程后杀掉或者使用htop
有一个更快、更容易的命令叫做[xkill][11]。
简单的在终端中输入以下命令并在窗口中点击你想杀死的应用程序。
xkill
那如果整个系统挂掉了怎么办呢?
按住键盘上的altsysrq同时输入
- [REISUB][12]
这样不按电源键你的计算机也能重启了。
![youtube-dl.](http://f.tqn.com/y/linux/1/L/v/J/1/youtubedl2.png)
youtube-dl.
### 11. 下载Youtube视频 ###
一般来说我们大多数人都喜欢看Youtube的视频也会通过钟爱的播放器播放Youtube的流。
如果你需要离线一段时间(比如:从苏格兰南部坐飞机到英格兰南部旅游的这段时间)那么你可能希望下载一些视频到存储设备中,到闲暇时观看。
你所要做的就是从包管理器中安装youtube-dl。
你可以用以下命令使用youtube-dl
youtube-dl url-to-video
你能在Youtubu视频页面点击分享链接得到视频的url。只要简单的复制链接在粘帖到命令行就行了要用shift + insert快捷键哟
### 总结 ###
希望你在这篇文章中得到帮助并且在这11条中找到至少一条让你惊叹“原来可以这样”的技巧。
--------------------------------------------------------------------------------
via: http://linux.about.com/od/commands/tp/11-Linux-Terminal-Commands-That-Will-Rock-Your-World.htm
作者:[Gary Newell][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://linux.about.com/bio/Gary-Newell-132058.htm
[1]:http://linux.about.com/cs/linux101/g/sudo.htm
[2]:http://linux.about.com/library/cmd/blcmdl1_nohup.htm
[3]:http://linux.about.com/od/mobiledevicesother/a/Raspberry-Pi-Computer-Running-Linux.htm
[4]:http://linux.about.com/od/commands/l/blcmdl1_ssh.htm
[5]:http://linux.about.com/cs/linux101/g/cowsay.htm
[6]:http://linux.about.com/od/commands/l/blcmdl1_ps.htm
[7]:http://www.linux.com/community/blogs/133-general-linux/745323-5-commands-to-check-memory-usage-on-linux
[8]:http://ranger.nongnu.org/
[9]:http://linux.about.com/od/commands/l/blcmdl8_shutdow.htm
[10]:http://linux.about.com/library/cmd/blcmdl1_pkill.htm
[11]:http://linux.about.com/od/funnymanpages/a/funman_xkill.htm
[12]:http://blog.kember.net/articles/reisub-the-gentle-linux-restart/