{"id":12396,"date":"2019-03-26T22:47:06","date_gmt":"2019-03-26T22:47:06","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12396"},"modified":"2019-03-26T22:47:06","modified_gmt":"2019-03-26T22:47:06","slug":"4-useful-tips-on-mkdir-tar-and-kill-commands-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/26\/4-useful-tips-on-mkdir-tar-and-kill-commands-in-linux\/","title":{"rendered":"4 Useful Tips on mkdir, tar and kill Commands in Linux"},"content":{"rendered":"<p>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\u00a0<a href=\"https:\/\/www.tecmint.com\/tag\/linux-tricks\/\" target=\"_blank\" rel=\"noopener\">Linux Tips and Trick Series<\/a>, I am here with the below four tips that will going to help you in lots of ways. Here we go!<\/p>\n<div id=\"attachment_14331\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/06\/Linux-Useful-Tips.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-14331\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/06\/Linux-Useful-Tips-620x297.jpg\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/06\/Linux-Useful-Tips-620x297.jpg 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/06\/Linux-Useful-Tips.jpg 720w\" alt=\"Linux Useful Tips\" width=\"620\" height=\"297\" aria-describedby=\"caption-attachment-14331\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-14331\" class=\"wp-caption-text\">4 Linux Useful Tips and Hacks<\/p>\n<\/div>\n<h6>1. You are supposed to create a long\/complex directory tree similar to given below. What is the most effective way to achieve this?<\/h6>\n<p>Directory tree structure to achieve as suggested below.<\/p>\n<pre>$ cd \/home\/$USER\/Desktop\r\n$ mkdir tecmint\r\n$ mkdir tecmint\/etc\r\n$ mkdir tecmint\/lib\r\n$ mkdir tecmint\/usr\r\n$ mkdir tecmint\/bin\r\n$ mkdir tecmint\/tmp\r\n$ mkdir tecmint\/opt\r\n$ mkdir tecmint\/var\r\n$ mkdir tecmint\/etc\/x1\r\n$ mkdir tecmint\/usr\/x2\r\n$ mkdir tecmint\/usr\/x3\r\n$ mkdir tecmint\/tmp\/Y1\r\n$ mkdir tecmint\/tmp\/Y2\r\n$ mkdir tecmint\/tmp\/Y3\r\n$ mkdir tecmint\/tmp\/Y3\/z\r\n<\/pre>\n<p>The above scenario can simply be achieved by running the below 1-liner command.<\/p>\n<pre>$ mkdir -p \/home\/$USER\/Desktop\/tecmint\/{etc\/x1,lib,usr\/{x2,x3},bin,tmp\/{Y1,Y2,Y3\/z},opt,var}\r\n<\/pre>\n<p>To verify you may use\u00a0<strong>tree<\/strong>\u00a0command. If not installed you may\u00a0<strong>apt<\/strong>\u00a0or\u00a0<strong>yum<\/strong>\u00a0the package \u2018<strong>tree<\/strong>\u2018.<\/p>\n<pre>$ tree tecmint\r\n<\/pre>\n<div id=\"attachment_14326\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/06\/Check-Directory-Structure.png\"><img decoding=\"async\" class=\"size-medium wp-image-14326\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/06\/Check-Directory-Structure.png\" sizes=\"(max-width: 803px) 100vw, 803px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/06\/Check-Directory-Structure.png 803w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/06\/Check-Directory-Structure-620x316.png 620w\" alt=\"Check Directory Structure\" aria-describedby=\"caption-attachment-14326\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-14326\" class=\"wp-caption-text\">Check Directory Structure<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<p>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\u00a0<code>{}<\/code>\u00a0to create hierarchy of directories. This may prove very helpful if used from inside of a shell script when required and in general.<\/p>\n<h6>2. Create a file (say test) on your Desktop (\/home\/$USER\/Desktop) and populate it with the below contents.<\/h6>\n<pre>ABC\r\nDEF\r\nGHI\r\nJKL\r\nMNO\r\nPQR\r\nSTU\r\nVWX\r\nY\r\nZ\r\n<\/pre>\n<p>What a normal user would do in this scenario?<\/p>\n<p><strong>a.<\/strong>\u00a0He will create the file first, preferably using\u00a0<a href=\"https:\/\/www.tecmint.com\/8-pratical-examples-of-linux-touch-command\/\" target=\"_blank\" rel=\"noopener\">touch command<\/a>, as:<\/p>\n<pre>$ touch \/home\/$USER\/Desktop\/test\r\n<\/pre>\n<p><strong>b.<\/strong>\u00a0He will use a text editor to open the file, which may be\u00a0<strong>nano<\/strong>,\u00a0<strong>vim<\/strong>, or any other editor.<\/p>\n<pre>$ nano \/home\/$USER\/Desktop\/test\r\n<\/pre>\n<p><strong>c.<\/strong>\u00a0He will then place the above text into this file, save and exit.<\/p>\n<p>So regardless of time taken by him\/her, he need at-least 3 steps to execute the above scenario.<\/p>\n<p>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.<\/p>\n<pre>cat &lt;&lt; EOF &gt; \/home\/$USER\/Desktop\/test\r\nABC\r\nDEF\r\nGHI\r\nJKL\r\nMNO\r\nPQR\r\nSTU\r\nVWX\r\nY\r\nZ\r\nEOF\r\n<\/pre>\n<p>You may use \u2018<strong>cat<\/strong>\u2018 command to check if the file and its content were created successfully or not.<\/p>\n<pre>$ cat \/home\/avi\/Desktop\/test\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/06\/Check-File-Content.gif\"><img decoding=\"async\" class=\"size-medium wp-image-14327\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/06\/Check-File-Content.gif\" alt=\"Check File Content\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<h6>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?<\/h6>\n<p>We normally do two things in this scenario.<\/p>\n<p><strong>a.<\/strong>\u00a0Copy\/Move the tar ball and extract it at destination, as:<\/p>\n<pre>$ cp firefox-37.0.2.tar.bz2 \/opt\/\r\nor\r\n$ mv firefox-37.0.2.tar.bz2 \/opt\/\r\n<\/pre>\n<p><strong>b.<\/strong>\u00a0cd to\u00a0<strong>\/opt\/<\/strong>\u00a0directory.<\/p>\n<pre>$ cd \/opt\/\r\n<\/pre>\n<p><strong>c.<\/strong>\u00a0Extract the Tarball.<\/p>\n<pre># tar -jxvf firefox-37.0.2.tar.bz2 \r\n<\/pre>\n<p>We can do this the other way around.<\/p>\n<p>We will extract the Tarball where it is and Copy\/Move the extracted archive to the required destination as:<\/p>\n<pre>$ tar -jxvf firefox-37.0.2.tar.bz2 \r\n$ cp -R firefox\/  \/opt\/\r\nor\r\n$ mv firefox\/ \/opt\/\r\n<\/pre>\n<p>In either case the work is taking two or steps to complete. The professional can complete this task in one step as:<\/p>\n<pre>$ tar -jxvf firefox-37.0.2.tar.bz2 -C \/opt\/\r\n<\/pre>\n<p>The option\u00a0<strong>-C<\/strong>\u00a0makes tar extract the archive in the specified folder (here\u00a0<strong>\/opt\/<\/strong>).<\/p>\n<p>No it is not about an option (<strong>-C<\/strong>) but it is about habits. Make a habit of using option\u00a0<strong>-C<\/strong>\u00a0with tar. It will ease your life. From now don\u2019t move the archive or copy\/move the extracted file, just leave the TAR-ball in the\u00a0<strong>Downloads<\/strong>folder and extract it anywhere you want.<\/p>\n<h6>4. How we kill a process in a traditional way?<\/h6>\n<p>In most general way, we first list all the process using command\u00a0<code>ps -A<\/code>\u00a0and pipeline it with\u00a0<strong>grep<\/strong>\u00a0to find a process\/service (say\u00a0<strong>apache2<\/strong>), simply as:<\/p>\n<pre>$ ps -A | grep -i apache2\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>1006 ?        00:00:00 apache2\r\n 2702 ?        00:00:00 apache2\r\n 2703 ?        00:00:00 apache2\r\n 2704 ?        00:00:00 apache2\r\n 2705 ?        00:00:00 apache2\r\n 2706 ?        00:00:00 apache2\r\n 2707 ?        00:00:00 apache2\r\n<\/pre>\n<p>The above output shows all currently running\u00a0<strong>apache2<\/strong>\u00a0processes with their\u00a0<strong>PID\u2019s<\/strong>, you can then use these PID\u2019s to kill apache2 with the help of following command.<\/p>\n<pre># kill 1006 2702 2703 2704 2705 2706 2707\r\n<\/pre>\n<p>and then cross check if any process\/service with the name \u2018<strong>apache2<\/strong>\u2018 is running or not, as:<\/p>\n<pre>$ ps -A | grep -i apache2\r\n<\/pre>\n<p>However we can do it in a more understandable format using utilities like\u00a0<strong>pgrep<\/strong>\u00a0and\u00a0<strong>pkill<\/strong>. You may find relevant information about a process just by using\u00a0<strong>pgrep<\/strong>. Say you have to find the process information for\u00a0<strong>apache2<\/strong>, you may simply do:<\/p>\n<pre>$ pgrep apache2\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>15396\r\n15400\r\n15401\r\n15402\r\n15403\r\n15404\r\n15405\r\n<\/pre>\n<p>You may also list process name against\u00a0<strong>pid<\/strong>\u00a0by running.<\/p>\n<pre>$ pgrep -l apache2\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>15396 apache2\r\n15400 apache2\r\n15401 apache2\r\n15402 apache2\r\n15403 apache2\r\n15404 apache2\r\n15405 apache2\r\n<\/pre>\n<p>To kill a process using\u00a0<strong>pkill<\/strong>\u00a0is very simple. You just type the name of resource to kill and you are done. I have written a post on\u00a0<strong>pkill<\/strong>\u00a0which you may like to refer here :\u00a0<a href=\"https:\/\/www.tecmint.com\/how-to-kill-a-process-in-linux\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.tecmint.com\/how-to-kill-a-process-in-linux\/<\/a>.<\/p>\n<p>To kill a process (say\u00a0<strong>apache2<\/strong>) using\u00a0<strong>pkill<\/strong>, all you need to do is:<\/p>\n<pre># pkill apache2\r\n<\/pre>\n<p>You may verify if\u00a0<strong>apache2<\/strong>\u00a0has been killed or not by running the below command.<\/p>\n<pre>$ pgrep -l apache2\r\n<\/pre>\n<p>It returns the prompt and prints nothing means there is no process running by the name of\u00a0<strong>apache2<\/strong>.<\/p>\n<p>That\u2019s all for now.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/mkdir-tar-and-kill-commands-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u00a0Linux 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! 4 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/26\/4-useful-tips-on-mkdir-tar-and-kill-commands-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;4 Useful Tips on mkdir, tar and kill Commands in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12396","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12396"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12396\/revisions"}],"predecessor-version":[{"id":12397,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12396\/revisions\/12397"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}