Merge pull request #2996 from ictlyh/master

[Translated] tech/20150629 4 Useful Tips on mkdir, tar and kill Comma…
This commit is contained in:
ictlyh 2015-06-30 11:58:12 +08:00
commit 834daa18df
2 changed files with 206 additions and 207 deletions

View File

@ -1,207 +0,0 @@
ictlyh translating
4 Useful Tips on mkdir, tar and kill Commands in Linux
================================================================================
We keep on accomplishing a task conventionally until we come to know that it can be done in a much better way the other way. In continuation of our [Linux Tips and Trick Series][1], I am here with the below four tips that will going to help you in lots of ways. Here we go!
![Linux Useful Tips](http://www.tecmint.com/wp-content/uploads/2015/06/Linux-Useful-Tips.jpg)
4 Linux Useful Tips and Hacks
### 1. You are supposed to create a long/complex directory tree similar to given below. What is the most effective way to achieve this? ###
Directory tree structure to achieve as suggested below.
$ cd /home/$USER/Desktop
$ mkdir tecmint
$ mkdir tecmint/etc
$ mkdir tecmint/lib
$ mkdir tecmint/usr
$ mkdir tecmint/bin
$ mkdir tecmint/tmp
$ mkdir tecmint/opt
$ mkdir tecmint/var
$ mkdir tecmint/etc/x1
$ mkdir tecmint/usr/x2
$ mkdir tecmint/usr/x3
$ mkdir tecmint/tmp/Y1
$ mkdir tecmint/tmp/Y2
$ mkdir tecmint/tmp/Y3
$ mkdir tecmint/tmp/Y3/z
The above scenario can simply be achieved by running the below 1-liner command.
$ mkdir -p /home/$USER/Desktop/tecmint/{etc/x1,lib,usr/{x2,x3},bin,tmp/{Y1,Y2,Y3/z},opt,var}
To verify you may use tree command. If not installed you may apt or yum the package tree.
$ tree tecmint
![Check Directory Structure](http://www.tecmint.com/wp-content/uploads/2015/06/Check-Directory-Structure.png)
Check Directory Structure
We can create directory tree structure of any complexity using the above way. Notice it is nothing other than a normal command but its using `{}` to create hierarchy of directories. This may prove very helpful if used from inside of a shell script when required and in general.
### 2. Create a file (say test) on your Desktop (/home/$USER/Desktop) and populate it with the below contents. ###
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
Y
Z
What a normal user would do in this scenario?
a. He will create the file first, preferably using [touch command][2], as:
$ touch /home/$USER/Desktop/test
b. He will use a text editor to open the file, which may be nano, vim, or any other editor.
$ nano /home/$USER/Desktop/test
c. He will then place the above text into this file, save and exit.
So regardless of time taken by him/her, he need at-least 3 steps to execute the above scenario.
What a smart experienced Linux-er will do? He will just type the below text in one-go on terminal and all done. He need not do each action separately.
cat << EOF > /home/$USER/Desktop/test
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
Y
Z
EOF
You may use cat command to check if the file and its content were created successfully or not.
$ cat /home/avi/Desktop/test
![Check File Content](http://www.tecmint.com/wp-content/uploads/2015/06/Check-File-Content.gif)
### 3. We deal with archives (specially TAR balls) very often on Linux. In many cases we have to use that TAR ball on some location other than Downloads folder. What we do in this scenario? ###
We normally do two things in this scenario.
a. Copy/Move the tar ball and extract it at destination, as:
$ cp firefox-37.0.2.tar.bz2 /opt/
or
$ mv firefox-37.0.2.tar.bz2 /opt/
b. cd to /opt/ directory.
$ cd /opt/
c. Extract the Tarball.
# tar -jxvf firefox-37.0.2.tar.bz2
We can do this the other way around.
We will extract the Tarball where it is and Copy/Move the extracted archive to the required destination as:
$ tar -jxvf firefox-37.0.2.tar.bz2
$ cp -R firefox/ /opt/
or
$ mv firefox/ /opt/
In either case the work is taking two or steps to complete. The professional can complete this task in one step as:
$ tar -jxvf firefox-37.0.2.tar.bz2 -C /opt/
The option -C makes tar extract the archive in the specified folder (here /opt/).
No it is not about an option (-C) but it is about habits. Make a habit of using option -C with tar. It will ease your life. From now dont move the archive or copy/move the extracted file, just leave the TAR-ball in the Downloads folder and extract it anywhere you want.
### 4. How we kill a process in a traditional way? ###
In most general way, we first list all the process using command `ps -A` and pipeline it with grep to find a process/service (say apache2), simply as:
$ ps -A | grep -i apache2
#### Sample Output ####
1006 ? 00:00:00 apache2
2702 ? 00:00:00 apache2
2703 ? 00:00:00 apache2
2704 ? 00:00:00 apache2
2705 ? 00:00:00 apache2
2706 ? 00:00:00 apache2
2707 ? 00:00:00 apache2
The above output shows all currently running apache2 processes with their PIDs, you can then use these PIDs to kill apache2 with the help of following command.
# kill 1006 2702 2703 2704 2705 2706 2707
and then cross check if any process/service with the name apache2 is running or not, as:
$ ps -A | grep -i apache2
However we can do it in a more understandable format using utilities like pgrep and pkill. You may find relevant information about a process just by using pgrep. Say you have to find the process information for apache2, you may simply do:
$ pgrep apache2
#### Sample Output ####
15396
15400
15401
15402
15403
15404
15405
You may also list process name against pid by running.
$ pgrep -l apache2
#### Sample Output ####
15396 apache2
15400 apache2
15401 apache2
15402 apache2
15403 apache2
15404 apache2
15405 apache2
To kill a process using pkill is very simple. You just type the name of resource to kill and you are done. I have written a post on pkill which you may like to refer here : [http://www.tecmint.com/how-to-kill-a-process-in-linux/][3].
To kill a process (say apache2) using pkill, all you need to do is:
# pkill apache2
You may verify if apache2 has been killed or not by running the below command.
$ pgrep -l apache2
It returns the prompt and prints nothing means there is no process running by the name of apache2.
Thats all for now, from me. All the above discussed point are not enough but will surely help. We not only mean to produce tutorials to make you learn something new every-time but also want to show How to be more productive in the same frame. Provide us with your valuable feedback in the comments below. Keep connected. Keep Commenting.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/mkdir-tar-and-kill-commands-in-linux/
作者:[Avishek Kumar][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/avishek/
[1]:http://www.tecmint.com/tag/linux-tricks/
[2]:http://www.tecmint.com/8-pratical-examples-of-linux-touch-command/
[3]:http://www.tecmint.com/how-to-kill-a-process-in-linux/

View File

@ -0,0 +1,206 @@
Linux mkdir、tar 和 kill 命令的 4 个有用小技巧
================================================================================
我们一直以常规的方式完成一个任务,直到我们知道有更好的处理方法。作为 [Linux 技巧和绝招系列][1] 的后续,我会在这里介绍能在各个方面给你帮助的 4 个小技巧。开始吧!
![有用的 Linux 小技巧](http://www.tecmint.com/wp-content/uploads/2015/06/Linux-Useful-Tips.jpg)
4 个有用的 Linux 小技巧
### 1. 假设你要创建一个类似于下面很长的/复杂的目录树。实现这最有效的方法是什么呢? ###
类似下面要实现的目录树结构。
$ cd /home/$USER/Desktop
$ mkdir tecmint
$ mkdir tecmint/etc
$ mkdir tecmint/lib
$ mkdir tecmint/usr
$ mkdir tecmint/bin
$ mkdir tecmint/tmp
$ mkdir tecmint/opt
$ mkdir tecmint/var
$ mkdir tecmint/etc/x1
$ mkdir tecmint/usr/x2
$ mkdir tecmint/usr/x3
$ mkdir tecmint/tmp/Y1
$ mkdir tecmint/tmp/Y2
$ mkdir tecmint/tmp/Y3
$ mkdir tecmint/tmp/Y3/z
上述情况可以简单地通过运行下面一行命令来实现。
$ mkdir -p /home/$USER/Desktop/tecmint/{etc/x1,lib,usr/{x2,x3},bin,tmp/{Y1,Y2,Y3/z},opt,var}
你可以用 tree 命令验证。如果没有安装你可以使用 apt 或 yum 安装 tree 软件包。
$ tree tecmint
![检查目录结构](http://www.tecmint.com/wp-content/uploads/2015/06/Check-Directory-Structure.png)
检查目录结构
我们可以用上面的方式创建任意复制的目录树结构。注意这仅仅是一个普通的命令,但是用 {} 来创建层级目录。需要的时候如果在 shell 脚本中使用是非常有用的。
### 2. 在桌面(/home/$USER/Desktop创建一个文件例如 test并填入以下内容。 ###
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
Y
Z
这种情况一个普通用户会怎么做呢?
a. 他首先会创建文件,最好使用 [touch 命令][2],例如:
$ touch /home/$USER/Desktop/test
b. 他会用一个文本编辑器打开文件,这可能是 nano、vim 或其它编辑器。
$ nano /home/$USER/Desktop/test
c. 然后他会将上面的内容输入到文件中,保存并退出。
忽略他/她使用的时间,他至少需要 3 步来执行上面的情况。
一个经验丰富的 Linux 用户会怎么做呢?他会在终端中输入下面的文本然后就完成所有任务。他不需要单独执行每一步。
cat << EOF > /home/$USER/Desktop/test
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
Y
Z
EOF
你可以用 cat 命令检查是否成功创建了文件和内容。
$ cat /home/avi/Desktop/test
![检查文件内容](http://www.tecmint.com/wp-content/uploads/2015/06/Check-File-Content.gif)
### 3. 我们经常在 Linux 中处理归档文件(尤其是 TAR 包)。很多情况下我们会在某些位置,而不是在 Downloads 目录中使用 TAR 包。这种情况下我们怎么做呢? ###
在这种情况下我们通常会做两件事。
a. 复制/移动 tar 包到目标位置并解压,例如:
$ cp firefox-37.0.2.tar.bz2 /opt/
$ mv firefox-37.0.2.tar.bz2 /opt/
b. cd 到 /opt/ 目录。
$ cd /opt/
c. 解压 tar 包。
# tar -jxvf firefox-37.0.2.tar.bz2
我们也可以采用另外一种方式。
我们可以在 Tar 包所在位置解压并复制/移动解压后的文件到所需的目标位置,例如:
$ tar -jxvf firefox-37.0.2.tar.bz2
$ cp -R firefox/ /opt/
$ mv firefox/ /opt/
不管哪种方式都需要两步才能完成任务。专业的人可以只用一步就完成这个任务:
$ tar -jxvf firefox-37.0.2.tar.bz2 -C /opt/
-C 选项提取文件到指定目录(这里是 /opt/)。
这并不是关于选项(-C的问题而是习惯的问题。养成使用带 -C 选项 tar 命令的习惯。这会使你的工作更加轻松。从现在开始不要再移动归档文件或复制/移动解压后的文件了,在 Downloads 文件夹保存 tar 包并解压到你想要的任何地方吧。
### 4. 常规方式我们怎样杀掉一个进程? ###
最普遍的方法,我们首先用 `ps -A` 命令列出所有进程,然后通过管道输入到 grep 来查找进程/服务(假如 apache2如下
$ ps -A | grep -i apache2
#### 输出样例 ####
1006 ? 00:00:00 apache2
2702 ? 00:00:00 apache2
2703 ? 00:00:00 apache2
2704 ? 00:00:00 apache2
2705 ? 00:00:00 apache2
2706 ? 00:00:00 apache2
2707 ? 00:00:00 apache2
上面的输出显示了所有正在运行 apache2 的进程以及它们的 PID然后你可以使用这些 PID 在下面命令的帮助下杀掉 apache2。
# kill 1006 2702 2703 2704 2705 2706 2707
然后交叉检查是否还有名称中包含 apache2 的进程/服务在运行,如下:
$ ps -A | grep -i apache2
实际上我们可以使用类似 pgrep 和 pkill 的工具以一种更容易理解的方式实现。你可以使用 pgrep 找到和一个进程相关的信息。假如你要找和 apache2 相关的进程信息,你只需要运行:
$ pgrep apache2
#### 输出样例 ####
15396
15400
15401
15402
15403
15404
15405
你也可以通过运行下面命令列出进程名称以及 pid。
$ pgrep -l apache2
#### 输出样例 ####
15396 apache2
15400 apache2
15401 apache2
15402 apache2
15403 apache2
15404 apache2
15405 apache2
用 pkill 杀掉进程非常简单。你只需要输入想要杀死的资源名称。我写了一篇关于 pkill 的博文,你可以参考: [http://www.tecmint.com/how-to-kill-a-process-in-linux/][3]。
用 pkill 杀死一个进程(例如 apache2你只需要输入以下命令
# pkill apache2
你可以通过运行下面命令验证是否杀掉了 apache2。
$ pgrep -l apache2
它没有输出任何东西并返回到窗口意味着没有名称中包含 apache2 的进程在运行。
这就是我要说的所有东西。上面讨论的点肯定远远不够,但也肯定对你有所帮助。我们不仅仅是介绍教程使你学到一些新的东西,更重要的是想告诉你 ‘在同样的情况下如何变得更有效率’。在下面的评论框中告诉我们你的反馈吧。保持联系,继续评论。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/mkdir-tar-and-kill-commands-in-linux/
作者:[Avishek Kumar][a]
译者:[ictlyh](https://github.com/ictlyh)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/avishek/
[1]:http://www.tecmint.com/tag/linux-tricks/
[2]:http://www.tecmint.com/8-pratical-examples-of-linux-touch-command/
[3]:http://www.tecmint.com/how-to-kill-a-process-in-linux/