4 Useful tips for Linux mkdir, tar, and kill commands

Source: Internet
Author: User
Tags bz2 touch command pkill

    • Software sharing

    • Forum

    • Submission

Popular Tags:

    • Safety

    • Activities

    • Docker

    • Books

    • Hackers

    • Nginx

    • Interview

    • Dns


Search Search


Linux. China-Open source community ? Technical Learning ? View Content

4 Useful tips for Linux mkdir, tar, and kill commands

2015-7-22 11:20| view: 3005| comments: 8| favorites: 8| share: 0

Original: http://www.tecmint.com/mkdir-tar-and-kill-commands-in-linux/ Avishek Kumar
translation: Lctt https://linux.cn/article-5863-1.html Translator: Ictlyh

We have always done a task in the usual way until we know there is a better way to handle it. As a follow-up to the Linux tips and tricks series, I'll show you 4 tips to help you in every way. Let's go!

4 Useful Linux Tips

1. Suppose you want to create a very long/complex directory tree similar to the one below. What is the most effective way to achieve this?

Similar to the following directory tree structure to implement.

$ 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$ Mkdi R tecmint/tmp/y1$ mkdir tecmint/tmp/y2$ mkdir tecmint/tmp/y3$ mkdir tecmint/tmp/y3/z

This can be done simply by running the following line of commands.

$ mkdir-p/home/$USER/desktop/tecmint/{etc/x1,lib,usr/{x2,x3},bin,tmp/{y1,y2,y3/z},opt,var}

You can verify it with the tree command. If not installed you can install the ' tree ' package using apt or yum.

$ Tree Tecmint

Check the directory structure

We can create any complex tree structure in the same way as above. Note that this is just a normal command, but use ' {} ' to create a hierarchical directory. It is useful if you want to use it in a shell script.

2. Create a file (for example, test) on the desktop (/home/$USER/desktop) and fill in the following content.
Abcdefghijklmnopqrstuvwxyz

What would an ordinary user do?

A. He will first create the file, preferably using the touch command, for example:

$ touch/home/$USER/desktop/test

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

$ nano/home/$USER/desktop/test

C. Then he will enter the above content into the file, save and exit.

Ignoring the time he/she uses, he needs at least 3 steps to perform the above situation.

What will a seasoned Linux user do? He will enter the following text in the terminal and complete all tasks. He does not need to perform each step alone.

Cat << EOF >/home/$USER/desktop/testabcdefghijklmnopqrstuvwxyzeof

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

$ cat/home/avi/desktop/test

Check file contents

3. We often process archive files (especially TAR packages) in Linux. In many cases we will use the TAR package in some locations rather than in the Downloads directory. What do we do in this situation?

In this case we usually do two things.

A. Copy/move the TAR package to the target location and unzip it, for example:

$ 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. Unzip the TAR package.

# TAR-JXVF FIREFOX-37.0.2.TAR.BZ2

We can also use a different approach.

We can also extract and copy/move the extracted files to the desired target location at 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 way, it takes two steps to complete the task. Professional people can accomplish this task in just one step:

$ TAR-JXVF firefox-37.0.2.tar.bz2-c/opt/

The-C option extracts the file to the specified directory (this is/opt/).

It's not about the option (-C), it's about getting used to the problem . Develop the habit of using the tar command with the-C option. This will make your job easier. From now on, don't move the archive or copy/move the extracted files, save the TAR package in the Downloads folder and unzip it anywhere you want.

4. How do we kill a process in the usual way?

The most common method, we first ps -A list all the processes with a command, and then input through the pipeline to grep to find the process/service (if apache2), as follows:

$ ps-a | Grep-i apache2

Output sample

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 the processes running apache2 and their PID, then you can use these PID to kill Apache2 with the help of the commands below.

# Kill 1006 2702 2703 2704 2705 2706 2707

Then cross-check if there are any processes/services that have names that contain ' apache2 ' running, as follows:

$ ps-a | Grep-i apache2

We can actually use tools like Pgrep and pkill to implement them in a way that's easier to understand. 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 sample

15396154001540115402154031540415405

You can also list the process name and PID by running the following command.

$ pgrep-l Apache2

Output sample

15396 apache215400 apache215401 apache215402 apache215403 apache215404 apache215405 apache2

Killing the process with Pkill is very simple. You only need to enter the name of the resource you want to kill. I wrote a post about Pkill, you can refer to: 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 verify that Apache2 is killed by running the following command.

$ pgrep-l Apache2

It does not output anything and returns to the window means that there is no name in the process that contains the apache2 in the run.

That's all I have to say. The points discussed above are certainly far from enough, but they certainly help you. We're not just introducing tutorials to get you to learn something new, but more importantly to tell you ' how to be more efficient in the same situation '.


4 Useful tips for Linux mkdir, tar, and kill commands

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.