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, I am here with the below four tips that will going to help you in lots of ways. Here we go!

Linux Useful Tips
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
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, 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

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 don’t 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 PID’s, you can then use these PID’s 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 : https://www.tecmint.com/how-to-kill-a-process-in-linux/.

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.

That’s 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.

Avishek
A Passionate GNU/Linux Enthusiast and Software Developer with over a decade in the field of Linux and Open Source technologies.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

18 thoughts on “4 Useful Tips on mkdir, tar and kill Commands in Linux”

    • @sampath, I would have loved do send you every important commands on your email id but practically i am too busy and have no time. You may use the search bar at the top of Tecmint.com to search posts already published. We are adding new posts on daily basis. Keep learning. keep connected. If you have any query related to Linux/FOSS, ask us at linuxsay.com

      Reply
  1. I really, really like your Linux tricks and shortcuts, I have been a Linux enthusiast for a while by now and I am making a lot of progress since I started swimming in this ocean, thank you very much but I have a question regarding your free Ebooks and that question is: Does everyone have to belong to a company or corporation in order to get any of the books you offer? I am actually studying and practicing on my very own and although I have a job, my job does not involve any computer work since I work in the food service industry, so I have no employer to mention in order to obtain one of your free ebooks. How does it work?

    Reply
    • @Jose,
      Not like that, but you should fill all the information to get the free ebooks and this is one time registration only..

      Reply
  2. Try:

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

    Tar can detect the file format and tar xf FILENAME is all you need to unpack a file ;)

    Reply
    • @ Tomas

      $ tar cf will extract but wont be verbose. To produce a verbose output i used (-v). Here all i needed was to tell people to use option (-C). Please go through the post, once again!

      Reply
  3. Brace expansion is very usefull, here is another usefull trick for you. This will create a backupfile with the “.bak” extension.(there should be no spaces in between..)
    cp somefile{,.bak}

    Reply
    • Thanks Krishna for your Feedback. Happy to know, it was helpful for you.
      Keep connected for more such posts.

      Reply
    • Agree @Mohamed Magdy.
      I wrote the post from the point of view of doing it quickly and efficiently and add it to habit.

      Reply
  4. Hi, most part of day I usually use ‘pgrep …’ then ‘kill -9 ..’, so, about pkill…I need to start to use it.

    Very useful this article, thanks brow!

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.