4 useful tips for the Linux mkdir, tar, and kill commands

  

We have been doing 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'm here to introduce four tips that can help you in every aspect. let's start!

4 useful Linux tips

1. Suppose you want to create a directory tree that looks like the long/complex below. What is the most effective way to achieve this?

Similar to the directory tree structure 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

The above can be achieved 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 with the tree command. If it is not installed you can use apt or yum to install the ‘tree’ package.

$ tree tecmint


Check directory structure

We can create arbitrarily complex directory tree structures in the above way. Note that this is just a normal command, but use ‘{}& rsquo; to create a hierarchical directory. It is very useful if you use it in a shell script when needed.

2. Create a file (eg test) on the desktop (/home/$USER/Desktop) and fill in the following.

ABCDEFGHIJKLMNOPQRSTUVWXYZ

What would an ordinary user do in this situation?

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

$ touch /home/$USER/Desktop/test

b. He will edit with a text Open the file, this may be nano, vim or other editor.

$ nano /home/$USER/Desktop/test

c. Then he will type the above into the file, save and exit.

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

What does an experienced Linux user do? He will type 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 if the file and content were successfully created.

$ cat /home/avi/Desktop/test


Checking the contents of the file

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

In this case we usually do two things.

a. Copy/move the tarball to the target location and extract 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. Unzip the tar package.

# tar -jxvf firefox-37.0.2.tar.bz2

We can also use another method.

We can also extract and copy/move the decompressed files to the desired destination 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 way it takes two steps to complete the task. Professional people can do this in one step:

$ tar -jxvf firefox-37.0.2.tar.bz2 -C /opt/

-C option to extract files to the specified directory (here /opt/).

This is not a question about options (-C), but a matter of habit. Develop the habit of using the -C option tar command. This will make your work easier. From now on, don't move the archive or copy/move the unzipped files. Save the tarball in the Downloads folder and unzip it wherever you want.

4. Regular way How do we kill a process?

The most common method, we first list all processes with the ps -A command, and then pipe into 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 PIDs, then you can use these PIDs in the following command With the help of kill apache2.

# kill 1006 2702 2703 2704 2705 2706 2707

Then cross check to see if there are processes/services with ‘apache2’ in the name running, as follows:

$ ps -A |
  Grep -i apache2

In fact, we can use tools like pgrep and pkill to implement in a more understandable way. You can use pgrep to find information about 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 by running the following command Process name and pid.

$ pgrep -l apache2

Output sample

15396 apache215400 apache215401 apache215402 apache215403 apache215404 apache215405 apache2

It is very simple to kill the process with pkill. You only need to enter the name of the resource you want to kill. I wrote a blog post about pkill, which 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 doesn't output anything and returns to the window means that no process with apache2 in its name is running.

Copyright © Windows knowledge All Rights Reserved