{"id":183,"date":"2020-08-15T20:10:45","date_gmt":"2020-08-16T01:10:45","guid":{"rendered":"https:\/\/www.brezeale.com\/?p=183"},"modified":"2020-08-15T20:10:45","modified_gmt":"2020-08-16T01:10:45","slug":"bash-shell-examples","status":"publish","type":"post","link":"https:\/\/www.brezeale.com\/?p=183","title":{"rendered":"bash Shell Examples"},"content":{"rendered":"\n<p>I originally created this on June 23, 2004.  These examples primarily use the <code>for<\/code> loop in the bash shell.<\/p>\n\n\n\n<ol>\n  <li>Count the number of lines with <span class=\"example\">the_word <\/span>in each <span class=\"example\">.txt<\/span> file<\/li>\n  <blockquote> \n    <table width=\"80%\" border=\"1\">\n      <tr> \n        <td bgcolor=\"#CCCCCC\"> \n          <pre>\nfor file in *.txt; do \n\tgrep the_word $file | wc -l\ndone\n<\/pre>        <\/td>\n      <\/tr>\n    <\/table>\n  <\/blockquote>\n  <li>This code creates directories (under the current directory, as written) \n    for any groups whose names begin with <span class=\"example\">dev-<\/span>.<\/li>\n  <blockquote> \n    <table width=\"80%\" border=\"1\">\n      <tr> \n        <td bgcolor=\"#CCCCCC\"> \n          <pre>\nfor GRP in `getent group | cut -d: -f1 | grep ^dev-`; do\n\t\tmkdir $GRP\ndone\n<\/pre>        <\/td>\n      <\/tr>\n    <\/table>\n  <\/blockquote>\n  <li>Create directories named <span class=\"example\">dev1, dev2, &#8230;, dev10<\/span><\/li>\n  <blockquote> \n    <table width=\"80%\" border=\"1\">\n      <tr> \n        <td bgcolor=\"#CCCCCC\"> \n          <pre>\nfor (( num=1; num &lt;= 10; num++ )); do\n\t\tmkdir &quot;dev$num&quot;\ndone\n<\/pre>        <\/td>\n      <\/tr>\n    <\/table>\n    <p>or<\/p>\n    <table width=\"80%\" border=\"1\">\n      <tr>\n        <td height=\"52\" bgcolor=\"#CCCCCC\"><pre>\nfor num in $(seq 1 10); do\n\t\tmkdir dev$num\ndone   <\/pre>        <\/td>\n      <\/tr>\n    <\/table>\n  <\/blockquote>\n  <li>If you need to have a special format, the Unix command <span class=\"example\">seq<\/span> \n    supports some of the printf standards. For instance, to convert the image \n    files named <span class=\"example\">frame001.jpg, frame002.jpg,&#8230;, frame999.jpg<\/span> \n  to GIFs, I can use<\/li>\n  <blockquote> \n    <table width=\"80%\" border=\"1\">\n      <tr> \n        <td bgcolor=\"#CCCCCC\"> \n          <pre>\nfor num in $(seq -f %03g 1 999); do\n\t\tconvert frame$num.jpg frame$num.gif\ndone\n          <\/pre><\/td>\n      <\/tr>\n    <\/table>\n  <\/blockquote>\n  <li>Change all files with the extension <span class=\"example\">out<\/span> to lowercase<\/li>\n  <blockquote> \n    <table width=\"80%\" border=\"1\">\n      <tr> \n        <td bgcolor=\"#CCCCCC\"> \n          <pre>\nfor file in *.out; do\n\tmv $file `echo $file | tr '[A-Z]' '[a-z]'`\ndone \n          <\/pre><\/td>\n      <\/tr>\n    <\/table>\n  <\/blockquote>\n  <li>Change filenames with the extension <span class=\"example\">chm<\/span> that consist of words separated by periods (e.g., <span class=\"example\">this.is.a.long.name.chm<\/span>) to use underscores instead except for the period that typically exists between the prefix and file extension.  Here sed&#8217;s &#8216;-e&#8217; option is used to combine expressions.<\/li>\n  <blockquote> \n    <table width=\"80%\" border=\"1\">\n      <tr> \n        <td bgcolor=\"#CCCCCC\"> \n          <pre>\nfor file in *.chm; do \n\tmv $file `echo $file | sed -e 's\/\\.\/_\/g' -e 's\/_chm\/.chm\/'`\ndone\n<\/pre>        <\/td>\n      <\/tr>\n    <\/table>\n  <\/blockquote>\n  <li>Find all directories which have files within 2 levels of the current location that have changed in the past day. Use sed to get just the directory name and tar it with the current date in the tar&#8217;ed filename. <\/li>\n  <blockquote> \n    <table width=\"80%\" border=\"1\">\n      <tr> \n        <td bgcolor=\"#CCCCCC\"> \n          <pre>SRC=\"\/windows_data\/data\"\nDEST=\"\/windows_data\/data_backups\"\nDATE=`date '+%y%m%d'`\n\ncd $SRC\n\nfor file in `find . -maxdepth 2 -mtime -1 | sed 's\/\\.\\\/\/\/' | sed 's\/\\(.*\\)\\\/.*\/\\1\/' | sort|uniq`\ndo\n  if [ ! -e \"$file\" ]       # Check if file exists.\n  then\n    echo \"$file does not exist.\"; echo\n    continue                # On to next.\n  fi\n  if [ ! \"$file\" == \".\" ]\n  then\n    tar -czf $DEST\/$file$DATE.tgz $file \n  fi\ndone\n<\/pre>        <\/td>\n      <\/tr>\n    <\/table>\n  <\/blockquote>\n  <li>Count the number of lines in each file with the extension <span class=\"example\">dct<\/span> and divide it by 480.<\/li>\n  <blockquote> \n    <table width=\"80%\" border=\"1\">\n      <tr> \n        <td bgcolor=\"#CCCCCC\"> \n          <pre>\nfor file in *.dct; do\n\tLC=`cat $file | wc -l`\n\techo $file\n\tCOUNT=$(echo \"scale=3; $LC \/ 480\" | bc)\n\techo \"  lines = \" $LC\", ratio = \" $COUNT\ndone\n          <\/pre><\/td>\n      <\/tr>\n    <\/table>\n  <\/blockquote>\n  <li>For each line in <span class=\"example\">test.txt<\/span>, count the number of words.<\/li>\n  <blockquote> \n    <table width=\"80%\" border=\"1\">\n      <tr> \n        <td bgcolor=\"#CCCCCC\"> \n          <pre>\nwhile read line; do  \n\techo $line | wc -w; \ndone &lt; test.txt\n          <\/pre><\/td>\n      <\/tr>\n    <\/table>\n  <\/blockquote>\n  <li>extract the files for user1010 from the compressed files<\/li>\n  <blockquote> \n    <table width=\"80%\" border=\"1\">\n      <tr> \n        <td bgcolor=\"#CCCCCC\"> \n          <pre>\nfor file in *sequence.csv.tgz; do \n\ttar tvzf $file | grep user1010 |sed 's\/.*\\(user1010.*\\)\/\\1\/' > tempvar\n\ttar xzf $file `cat tempvar`\ndone\n<\/pre><\/td>\n      <\/tr>\n    <\/table>\n  <\/blockquote>\n  <li>combine the contents of all user files (which have names of the form <span class=\"example\">user1234-sequence.csv<\/span>), with each user&#8217;s part preceded by the user name and separated by a blank line<\/li>\n  <blockquote> \n    <table width=\"80%\" border=\"1\">\n      <tr> \n        <td bgcolor=\"#CCCCCC\"> \n          <pre>\nfor file in user*sequence.csv; do \n\tprefix=`basename $file -sequence.csv`\n\techo $prefix >> allusers.txt\n\tcat $file >> allusers.txt\n\techo \"\" >> allusers.txt\ndone\n<\/pre><\/td>\n      <\/tr>\n    <\/table>\n  <\/blockquote>\n  <li>rename files of the form <span class=\"example\">test3-q01.c, test3-q02.c,&#8230;<\/span> to <span class=\"example\">test2-q01.c, test2-q02.c,&#8230;<\/span><\/li>\n  <blockquote> \n    <table width=\"80%\" border=\"1\">\n      <tr> \n        <td bgcolor=\"#CCCCCC\"> \n          <pre>\nfor file in test3*.c; do \n    suf=`echo $file | sed 's\/test3-\/\/'`\n    mv $file test2-$suf\ndone\n<\/pre><\/td>\n      <\/tr>\n    <\/table>\n  <\/blockquote>\n<\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I originally created this on June 23, 2004. These examples primarily use the for loop in the bash shell. Count the number of lines with the_word in each .txt file for file in *.txt; do grep the_word $file | wc -l done This code creates directories (under the current directory, as written) for any groups whose names begin with dev-. for GRP in `getent group | cut -d: -f1 | grep ^dev-`; do mkdir $GRP done Create directories named dev1,&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.brezeale.com\/?p=183\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/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":[7],"tags":[],"class_list":["post-183","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=183"}],"version-history":[{"count":1,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/183\/revisions"}],"predecessor-version":[{"id":184,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/183\/revisions\/184"}],"wp:attachment":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}