{"id":15,"date":"2011-04-25T08:46:00","date_gmt":"2011-04-25T08:46:00","guid":{"rendered":"http:\/\/mattdturner.com\/wordpress\/?p=15"},"modified":"2011-04-26T21:42:23","modified_gmt":"2011-04-27T03:42:23","slug":"useful-bashrc-functions","status":"publish","type":"post","link":"https:\/\/mattdturner.com\/wordpress\/2011\/04\/useful-bashrc-functions\/","title":{"rendered":"Useful .bashrc Functions"},"content":{"rendered":"<p style=\"text-align: center;\"><a href=\"http:\/\/mattdturner.com\/wordpress\/wp-content\/uploads\/2011\/04\/bashrc-screenshot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-62\" title=\"bashrc screenshot\" src=\"http:\/\/mattdturner.com\/wordpress\/wp-content\/uploads\/2011\/04\/bashrc-screenshot.png\" alt=\"Screenshot of bashrc file\" width=\"499\" height=\"116\" srcset=\"https:\/\/mattdturner.com\/wordpress\/wp-content\/uploads\/2011\/04\/bashrc-screenshot.png 624w, https:\/\/mattdturner.com\/wordpress\/wp-content\/uploads\/2011\/04\/bashrc-screenshot-300x69.png 300w\" sizes=\"(max-width: 499px) 100vw, 499px\" \/><\/a>For those of you that read my other post about <a href=\"http:\/\/mattdturner.com\/wordpress\/2011\/04\/some-useful-cshrc-functions\/\">useful .cshrc functions<\/a>, you know that I like to automate as many processes as possible.  If there is something redundant that I do frequently, I will script it or make a function to take care of it.<\/p>\n<p>In an effort to assist those of you out there that like to automate processes but use bash instead of C-Shell, I have compiled a list of some useful functions to stick in your .bashrc file.  Here is a list of the functions that this post will contain:<\/p>\n<ol>\n<li><a href=\"#extract\">Extract<\/a>:  This function will determine the type of archive you are dealing with, and issue the corresponding command to extract it.<\/li>\n<li><a href=\"#compress\">Compress<\/a>:  This function will create a .tar.gz archive of whatever files you give.<\/li>\n<li><a href=\"#swap\">Swap<\/a>:  This function will swap the names of 2 different files.<\/li>\n<li><a href=\"#clock\">Clock<\/a>:  This is just a random function that will display a digital clock in your terminal window and update every second.<\/li>\n<li><a href=\"#cpg\">Cpg<\/a>:  This function will copy a file to a directory, and also <code>cd<\/code> you to a directory.<\/li>\n<li><a href=\"#mvg\">Mvg<\/a>:  This function works like Cpg, but moves the file instead of copying it.<\/li>\n<li><a href=\"#findit\">Findit<\/a>:  This function will search for a file with the given pattern in the name.<\/li>\n<\/ol>\n<p><!--more--><\/p>\n<div>\n<h2>Function Extract<\/h2>\n<p><a name=\"extract\"><\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\"># Handy Extract Program.\r\nfunction extract() {\r\n          if [ -f $1 ] ; then\r\n             case $1 in\r\n              *.tar.bz2)   tar xvjf $1     ;;\r\n             *.tar.gz)    tar xvzf $1     ;;\r\n             *.bz2)       bunzip2 $1      ;;\r\n             *.rar)       unrar x $1      ;;\r\n             *.gz)        gunzip $1       ;;\r\n             *.tar)       tar xvf $1      ;;\r\n             *.tbz2)      tar xvjf $1     ;;\r\n             *.tgz)       tar xvzf $1     ;;\r\n             *.zip)       unzip $1        ;;\r\n             *.Z)         uncompress $1   ;;\r\n             *.7z)        7z x $1         ;;\r\n             *)           echo &quot;'$1' cannot be extracted via extract()&quot; ;;\r\n         esac\r\n     else\r\n         echo &quot;'$1' is not a valid file&quot;\r\n     fi\r\n}<\/pre>\n<\/div>\n<div>\n<p><strong>Use<\/strong>: <code>extract archive.tar.gz<\/code><\/p>\n<h2>Function Compress<\/h2>\n<p><a name=\"compress\"><\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nfunction compress()  # create .tar.gz archive\r\n{\r\n    tar -cvzf $1 $2\r\n}<\/pre>\n<p><strong>Use<\/strong>: <code>compress archive.tar.gz ~\/Desktop\/*<\/code><\/p>\n<h2>Function Swap<\/h2>\n<p><a name=\"swap\"><\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nfunction swap()  # Swap 2 filenames around, if they exist\r\n                          #(from Uzi's bashrc).\r\n{\r\n    local TMPFILE=tmp.$$\r\n\r\n    [ $# -ne 2 ] &amp;amp;&amp;amp; echo &quot;swap: 2 arguments needed&quot; &amp;amp;&amp;amp; return 1\r\n    [ ! -e $1 ] &amp;amp;&amp;amp; echo &quot;swap: $1 does not exist&quot; &amp;amp;&amp;amp; return 1\r\n    [ ! -e $2 ] &amp;amp;&amp;amp; echo &quot;swap: $2 does not exist&quot; &amp;amp;&amp;amp; return 1\r\n\r\n    mv &quot;$1&quot; $TMPFILE\r\n    mv &quot;$2&quot; &quot;$1&quot;\r\n    mv $TMPFILE &quot;$2&quot;\r\n}\r\n<\/pre>\n<p><strong>Use<\/strong>: <code>swap file1.txt file2.txt<\/code><\/p>\n<h2>Function Clock<\/h2>\n<p><a name=\"clock\"><\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\"># clock - a little clock that appeares in the terminal window.\r\n# Usage: clock.\r\n#\r\nclock ()\r\n{\r\nwhile true;do clear;echo &quot;===========&quot;;date +&quot;%r&quot;;echo &quot;===========&quot;;sleep 1;done\r\n}<\/pre>\n<p><strong>Use<\/strong>: <code>clock<\/code><\/p>\n<h2>Function Cpg<\/h2>\n<p><a name=\"cpg\"><\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">#copy and go to dir\r\ncpg (){\r\n  if [ -d &quot;$2&quot; ];then\r\n    cp $1 $2 &amp;amp;&amp;amp; cd $2\r\n  else\r\n    cp $1 $2\r\n  fi\r\n}\r\n<\/pre>\n<p><strong>Use<\/strong>: <code>cpg file.txt \/path\/to\/dir<\/code><\/p>\n<h2>Function Mvg<\/h2>\n<p><a name=\"mvg\"><\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">#move and go to dir\r\nmvg (){\r\n  if [ -d &quot;$2&quot; ];then\r\n    mv $1 $2 &amp;amp;&amp;amp; cd $2\r\n  else\r\n    mv $1 $2\r\n  fi\r\n}<\/pre>\n<p><strong>Use<\/strong>: <code>mvg file.txt \/path\/to\/dir<\/code><\/p>\n<h2>Function Findit<\/h2>\n<p><a name=\"findit\"><\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\"># find a file with pattern in name\r\nfindit (){\r\n   if [ -z ${1} ];then\r\n      echo &quot;Please pass an argument that you want to search for&quot;\r\n   else\r\n      find . -iname &quot;*$1*&quot; -print\r\n   fi\r\n}<\/pre>\n<p><strong>Use<\/strong>: <code>findit stringToSearchFor<\/code><br \/>\n<strong>Note<\/strong>:  <code>findit<\/code> function is NOT case-sensitive<\/p>\n<p>Feel free to modify any of the functions to your liking.<\/p>\n<p>Have a function that you would like to share?  Post it in the comments!<\/p>\n<p>74VYGRY8335Z<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>For those of you that read my other post about useful .cshrc functions, you know that I like to automate as many processes as possible. If there is something redundant that I do frequently, I will script it or make a function to take care of it. In an effort to assist those of you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,11,20],"tags":[46,47,50,49,48],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-command-line","category-mac","category-scripting","tag-bashrc","tag-function","tag-linux","tag-ubuntu","tag-unix","no-thumb"],"_links":{"self":[{"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/posts\/15"}],"collection":[{"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":8,"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":36,"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/posts\/15\/revisions\/36"}],"wp:attachment":[{"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}