Four tips for Linux mkdir, tar, and kill commands
We have been doing a task in a regular way until we know there are better solutions. As a follow-up of the Linux tips and tricks series, I will introduce four tips that can help you in various aspects. Start!
Four useful Linux tips
1. Suppose you want to create a very long/complex directory tree similar to the following. What is the most effective way to achieve this?
Similar to the directory tree to be implemented 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
In the above case, you can simply run the following command line.
$ mkdir -p /home/$USER/Desktop/tecmint/{etc/x1,lib,usr/{x2,x3},bin,tmp/{Y1,Y2,Y3/z},opt,var}
You can use the tree Command for verification. If not, you can use apt or yum to install the 'tree' software package.
$ tree tecmint
Check the directory structure
We can use the above method to create any complex directory tree structure. Note that this is just a common command, but you can use '{}' to create a hierarchical directory. If necessary, it is very useful in shell scripts.
2. Create a file (such as test) on the Desktop (/home/$ USER/Desktop) and enter the following content.
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
Y
Z
In this case, what would a common user do?
A. He will first create a file. It is best to use the touch command, for example:
$ touch /home/$USER/Desktop/test
B. He will open the file in a text editor, which may be nano, vim or other editors.
$ nano /home/$USER/Desktop/test
C. Then he will input the above content to the file, save and exit.
Ignore the time used by him/her. It takes at least three steps for him/her to execute the above situation.
What can an experienced Linux User do? He will input the following text in the terminal to complete all the tasks. He does not need to perform each step separately.
cat << EOF >/home/$USER/Desktop/test
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
Y
Z
EOF
You can run the 'cat' command to check whether the file and content have been successfully created.
$ cat /home/avi/Desktop/test
Check file content
3. We often process archive files (especially TAR packages) in Linux ). In many cases, we use TAR packages in some locations instead of Downloads directories. What should we do in this situation?
In this case, we usually do two things.
A. Copy/move the tar package to the target location and decompress it. For example:
$ cp firefox-37.0.2.tar.bz2 /opt/
Or
$ mv firefox-37.0.2.tar.bz2 /opt/
B. cd to the/opt/directory.
$ cd /opt/
C. decompress the tar package.
# tar -jxvf firefox-37.0.2.tar.bz2
We can also adopt another method.
You can also extract and copy/move the extracted files to the desired target location in the location of the Tar package, for example:
$ tar -jxvf firefox-37.0.2.tar.bz2
$ cp -R firefox/ /opt/
Or
$ mv firefox/ /opt/
Either method requires two steps to complete the task. Professional personnel can complete this task in only one step:
$ tar -jxvf firefox-37.0.2.tar.bz2 -C /opt/
-C option to extract the file to the specified directory (here is/opt /).
This is not about option (-C), but a habit. Develop the habit of using the tar command with the-C option. This makes your work easier. Do not move the archive file or copy/move the decompressed file from now on. Save the tar package in the Downloads folder and decompress it wherever you want.
4. How do we kill a process in the conventional way?
The most common method is to useps -A
Command to list all processes, and then enter grep in the pipeline to find the process/service (for example, apache2), as shown below:
$ ps -A | grep -i apache2
Output example
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 processes running apache2 and their PIDs. Then you can use these PIDs to kill apache2 with the help of the following commands.
# kill 1006 2702 2703 2704 2705 2706 2707
Then, cross-check whether there are any processes/services with names containing 'apache2' running, as shown below:
$ ps -A | grep -i apache2
In fact, we can use tools like pgrep and pkill to achieve this in a more understandable way. You can use pgrep to find information related to a process. If you are looking for process information related to apache2, you only need to run:
$ pgrep apache2
Output example
15396
15400
15401
15402
15403
15404
15405
You can also run the following command to list the process name and pid.
$ pgrep -l apache2
Output example
15396 apache2
15400 apache2
15401 apache2
15402 apache2
15403 apache2
15404 apache2
15405 apache2
Killing a process with pkill is very simple. You only need to enter the name of the resource to be killed. I wrote a blog post about pkill. For more information, see http://www.tecmint.com/how-to-kill-a-process-in-linux /.
To kill a process with pkill (for example, apache2), you only need to enter the following command:
# pkill apache2
You can run the following command to verify whether apache2 has been killed.
$ pgrep -l apache2
It does not output anything and returns to the window, which means that the process with no name including apache2 is running.
This is what I want to talk about. The points discussed above are certainly far from enough, but they are certainly helpful to you. We will not only introduce the tutorials to help you learn new things, but also want to tell you how to make things more efficient in the same situation '. Let us know your feedback in the comment box below. Keep in touch and continue comments.
Via: http://www.tecmint.com/mkdir-tar-and-kill-commands-in-linux/
Author: Avishek Kumar Translator: ictlyh Proofreader: wxy
This article was originally translated by LCTT and launched with the Linux honor in China
This article permanently updates the link address: