{"id":88,"date":"2017-04-18T17:08:08","date_gmt":"2017-04-18T20:08:08","guid":{"rendered":"http:\/\/xaxowareti.com.br\/?p=88"},"modified":"2017-04-18T17:08:08","modified_gmt":"2017-04-18T20:08:08","slug":"bash-command-line-examples","status":"publish","type":"post","link":"https:\/\/xaxowareti.com.br\/?p=88","title":{"rendered":"Bash Command Line Examples."},"content":{"rendered":"<p class=\"documentDescription\"><span id=\"parent-fieldname-description\" class=\"\">After having spent years on unix systems, shell scripting still continues to elude me. Hence these reminders.<\/span><\/p>\n<dl id=\"document-toc\" class=\"portlet toc\">\n<dt class=\"portletHeader\">contents<\/dt>\n<dd class=\"portletItem\">\n<ol class=\"TOC1Digit\">\n<li><a href=\"http:\/\/www.schmut.com\/cheat-sheets\/bash-command-line-examples#section-0\">For Loops over lists<\/a><\/li>\n<li><a href=\"http:\/\/www.schmut.com\/cheat-sheets\/bash-command-line-examples#section-1\">While loop<\/a><\/li>\n<li><a href=\"http:\/\/www.schmut.com\/cheat-sheets\/bash-command-line-examples#section-2\">The Absolute Current Working Directory<\/a><\/li>\n<li><a href=\"http:\/\/www.schmut.com\/cheat-sheets\/bash-command-line-examples#section-3\">Getting a line count on various directories<\/a><\/li>\n<\/ol>\n<\/dd>\n<\/dl>\n<div id=\"parent-fieldname-text\" class=\"\">\n<div class=\"line-block\">\n<div class=\"line\"><\/div>\n<\/div>\n<p><a name=\"section-0\"><\/a><\/p>\n<h2><a id=\"for-loops-over-lists\" name=\"for-loops-over-lists\" shape=\"rect\"><\/a>For Loops over lists<\/h2>\n<div class=\"section\">\n<ul class=\"simple\">\n<li>To add a bunch of files to subversion<\/li>\n<\/ul>\n<pre class=\"literal-block\">for f in Button.phi Buttons.phi Cells.phi ; do svn add $f; done\r\nfor f in `ls -l *.phi`; do svn add $f; done<\/pre>\n<ul class=\"simple\">\n<li>A more complete sample<\/li>\n<\/ul>\n<pre class=\"literal-block\">ls *.xml\r\nfile1.xml  file2.xml  file3.xml\r\n\r\nls *.xml &gt; list\r\n\r\nfor i in `cat list`; do cp \"$i\" \"$i\".bak ; done\r\n\r\nls *.xml*\r\nfile1.xml  file1.xml.bak  file2.xml  file2.xml.bak  file3.xml  file3.xml.bak<\/pre>\n<div class=\"line-block\">\n<div class=\"line\"><\/div>\n<\/div>\n<ul class=\"simple\">\n<li>To clear emails to <span class=\"link-\"><a class=\"reference\" href=\"mailto:user@example.com\" shape=\"rect\">user@example.com<\/a><\/span> out of my mail queue after inspecting the affected files<\/li>\n<\/ul>\n<pre class=\"literal-block\">cd \/path\/to\/mailqueue\r\nls -l `grep -l 'user@example.com' \\`find . -type f\\``\r\nrm -f `grep -l 'user@example.com' \\`find . -type f\\``<\/pre>\n<p><strong>Note:<\/strong> Be sure to run the <strong>ls<\/strong> before the <strong>rm<\/strong>.<\/p>\n<div class=\"line-block\">\n<div class=\"line\"><\/div>\n<\/div>\n<ul class=\"simple\">\n<li>A little awking. PIDs of all processes using alsa. Grabs the 2nd column out of a listing.<\/li>\n<\/ul>\n<pre class=\"literal-block\">lsof | grep alsa | awk '{print $2}'<\/pre>\n<ul class=\"simple\">\n<li>This alias produces a sorted list of services and the ports they&#8217;re listening on.<\/li>\n<\/ul>\n<pre class=\"literal-block\">alias ports='sudo \/usr\/bin\/lsof -nPi | grep LIST | awk '\\''{printf \"%-20s%-5s%-5s%s\\n\",$1,$5,$7,$8}'\\'' | sort | uniq'<\/pre>\n<ul class=\"simple\">\n<li>Running ports would then produce something like this:<\/li>\n<\/ul>\n<pre class=\"literal-block\">cupsd               IPv4 TCP  127.0.0.1:631\r\nmaster              IPv4 TCP  127.0.0.1:25\r\nmysqld              IPv4 TCP  127.0.0.1:3306\r\nportmap             IPv4 TCP  *:111\r\nrpc.mount           IPv4 TCP  *:657\r\nrpc.statd           IPv4 TCP  *:894\r\nsmbd                IPv4 TCP  *:139\r\nsmbd                IPv4 TCP  *:445\r\nsshd                IPv6 TCP  *:22<\/pre>\n<div class=\"line-block\">\n<div class=\"line\"><\/div>\n<\/div>\n<\/div>\n<p><a name=\"section-1\"><\/a><\/p>\n<h2><a id=\"while-loop\" name=\"while-loop\" shape=\"rect\"><\/a>While loop<\/h2>\n<div class=\"section\">\n<ul class=\"simple\">\n<li>Keeping my mail inbox from overflowing while running email stress tests<\/li>\n<\/ul>\n<pre class=\"literal-block\">cd \/path\/to\/maildir\/new\r\nwhile true; do rm -f *; sleep 5; done<\/pre>\n<div class=\"line-block\">\n<div class=\"line\"><\/div>\n<\/div>\n<\/div>\n<p><a name=\"section-2\"><\/a><\/p>\n<h2><a id=\"the-absolute-current-working-directory\" name=\"the-absolute-current-working-directory\" shape=\"rect\"><\/a>The Absolute Current Working Directory<\/h2>\n<div class=\"section\">\n<ul class=\"simple\">\n<li>AKA Getting the realpath of a location.<\/li>\n<li>This is really more useful in scripts, but here goes:<\/li>\n<\/ul>\n<pre class=\"literal-block\">MYCWD=`dirname \\`readlink -e $0\\``<\/pre>\n<div class=\"line-block\">\n<div class=\"line\"><\/div>\n<\/div>\n<\/div>\n<p><a name=\"section-3\"><\/a><\/p>\n<h2><a id=\"getting-a-line-count-on-various-directories\" name=\"getting-a-line-count-on-various-directories\" shape=\"rect\"><\/a>Getting a line count on various directories<\/h2>\n<div class=\"section\">\n<pre class=\"literal-block\">T=0; \\\r\nfor f in dir1 dir3 dir10; do \\\r\n  N=0; \\\r\n for n in `find $f -type f -exec wc -l {} \\; | awk '{print $1}'`; do\\\r\n    N=$(\u200e( $M + $n )); \\\r\n  done; \\\r\n  T=$(\u200e( $T + $N)); \\\r\n  echo $N - $f; \\\r\ndone; \\\r\necho $T - Total\r\n\r\n4686 - dir1\r\n4894 - dir3\r\n981037 - dir10\r\n990617 - Total<\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>After having spent years on unix systems, shell scripting still continues to elude me. Hence these reminders. contents For Loops over lists While loop The Absolute Current Working Directory Getting a line count on various directories For Loops over lists To add a bunch of files to subversion for f in Button.phi Buttons.phi Cells.phi ; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-dicassolucoes"],"_links":{"self":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=88"}],"version-history":[{"count":1,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts\/88\/revisions\/89"}],"wp:attachment":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}