Four tips for Linux mkdir, tar, and kill commands

Source: Internet
Author: User
Tags touch command pkill

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.

  1. $ cd /home/$USER/Desktop
  2. $ mkdir tecmint
  3. $ mkdir tecmint/etc
  4. $ mkdir tecmint/lib
  5. $ mkdir tecmint/usr
  6. $ mkdir tecmint/bin
  7. $ mkdir tecmint/tmp
  8. $ mkdir tecmint/opt
  9. $ mkdir tecmint/var
  10. $ mkdir tecmint/etc/x1
  11. $ mkdir tecmint/usr/x2
  12. $ mkdir tecmint/usr/x3
  13. $ mkdir tecmint/tmp/Y1
  14. $ mkdir tecmint/tmp/Y2
  15. $ mkdir tecmint/tmp/Y3
  16. $ mkdir tecmint/tmp/Y3/z

In the above case, you can simply run the following command line.

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

  1. $ 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.
  1. ABC
  2. DEF
  3. GHI
  4. JKL
  5. MNO
  6. PQR
  7. STU
  8. VWX
  9. Y
  10. 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:

  1. $ touch /home/$USER/Desktop/test

B. He will open the file in a text editor, which may be nano, vim or other editors.

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

  1. cat << EOF >/home/$USER/Desktop/test
  2. ABC
  3. DEF
  4. GHI
  5. JKL
  6. MNO
  7. PQR
  8. STU
  9. VWX
  10. Y
  11. Z
  12. EOF

You can run the 'cat' command to check whether the file and content have been successfully created.

  1. $ 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:

  1. $ cp firefox-37.0.2.tar.bz2 /opt/
  2. Or
  3. $ mv firefox-37.0.2.tar.bz2 /opt/

B. cd to the/opt/directory.

  1. $ cd /opt/

C. decompress the tar package.

  1. # 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:

  1. $ tar -jxvf firefox-37.0.2.tar.bz2
  2. $ cp -R firefox/ /opt/
  3. Or
  4. $ mv firefox/ /opt/

Either method requires two steps to complete the task. Professional personnel can complete this task in only one step:

  1. $ 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 -ACommand to list all processes, and then enter grep in the pipeline to find the process/service (for example, apache2), as shown below:

  1. $ ps -A | grep -i apache2

Output example

  1. 1006?00:00:00 apache2
  2. 2702?00:00:00 apache2
  3. 2703?00:00:00 apache2
  4. 2704?00:00:00 apache2
  5. 2705?00:00:00 apache2
  6. 2706?00:00:00 apache2
  7. 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.

  1. # 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:

  1. $ 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:

  1. $ pgrep apache2

Output example

  1. 15396
  2. 15400
  3. 15401
  4. 15402
  5. 15403
  6. 15404
  7. 15405

You can also run the following command to list the process name and pid.

  1. $ pgrep -l apache2

Output example

  1. 15396 apache2
  2. 15400 apache2
  3. 15401 apache2
  4. 15402 apache2
  5. 15403 apache2
  6. 15404 apache2
  7. 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:

  1. # pkill apache2

You can run the following command to verify whether apache2 has been killed.

  1. $ 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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.