{"id":362,"date":"2010-01-08T12:08:02","date_gmt":"2010-01-08T17:08:02","guid":{"rendered":"http:\/\/www.webadminblog.com\/?p=362"},"modified":"2010-01-08T12:08:02","modified_gmt":"2010-01-08T17:08:02","slug":"stupid-unix-trick-command-mashups","status":"publish","type":"post","link":"https:\/\/www.webadminblog.com\/index.php\/2010\/01\/08\/stupid-unix-trick-command-mashups\/","title":{"rendered":"Stupid Unix Trick &#8211; Command Mashups"},"content":{"rendered":"<p>I&#8217;ve been a *nix Administrator in some form or fashion for about 10 years now.\u00a0 I remember back when I was first learning commands and how the OS works and every once in a while I&#8217;d come across something stupidly simple yet extremely useful to put in my bag of tricks.\u00a0 Yesterday I was reminded about one of those things and I figured I&#8217;d share it here so that you can throw it in your bag of tricks as well if it&#8217;s not already in there.<\/p>\n<p>To start out, let me illustrate the problem.\u00a0 You are writing a shell script or running a series of commands on the CLI.\u00a0 Let&#8217;s just say it&#8217;s something simple like creating a new directory, changing to that directory, and then creating a file.\u00a0 When I first started out, that command would look something like this:<\/p>\n<p><em>mkdir newdirectory; cd newdirectory; touch newfile<\/em><\/p>\n<p>The problem with this is that each command is executed on it&#8217;s own regardless of whether or not the previous command was successful.\u00a0 So if, for example, my mkdir and cd failed (permissions maybe?), I would be creating that newfile in whatever directory I started out in.\u00a0 At best, I just created a new file in the wrong directory.\u00a0 At worst, if the file which I&#8217;m creating was the same name as another file already in the current directory, I just overwrote it.\u00a0 Not good!<\/p>\n<p>The way to fix this is to add a dependency so that each command will not execute without the successful return of the command before that.\u00a0 The way you do this is by putting an &#8220;&amp;&amp;&#8221; between them instead of the semi-colon.\u00a0 So now the command string above should look like this:<\/p>\n<p><em>mkdir newdirectory &amp;&amp; cd newdirectory &amp;&amp;\u00a0touch newfile<\/em><\/p>\n<p>Now you have guaranteed that the new file will not be created with the touch command unless both the mkdir and cd commands before it are successful.\u00a0 Stupid simple, right?\u00a0 Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been a *nix Administrator in some form or fashion for about 10 years now.\u00a0 I remember back when I was first learning commands and how the OS works and every once in a while I&#8217;d come across something stupidly simple yet extremely useful to put in my bag of tricks.\u00a0 Yesterday I was reminded [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[378,376,377],"tags":[],"class_list":["post-362","post","type-post","status-publish","format-standard","hentry","category-linux-operating-systems","category-operating-systems","category-unix-operating-systems"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pfI0c-5Q","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/posts\/362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=362"}],"version-history":[{"count":1,"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/posts\/362\/revisions"}],"predecessor-version":[{"id":363,"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/posts\/362\/revisions\/363"}],"wp:attachment":[{"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}