{"id":427,"date":"2019-04-04T14:19:50","date_gmt":"2019-04-04T22:19:50","guid":{"rendered":"https:\/\/angrysysadmins.tech\/?p=427"},"modified":"2019-09-17T15:07:08","modified_gmt":"2019-09-17T23:07:08","slug":"useful-bash-one-liners","status":"publish","type":"post","link":"https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/","title":{"rendered":"Useful Bash One-liners"},"content":{"rendered":"\n<p>Below is a collection of useful Bash one-liners that we have come across and use semi-often (or at least often enough to not want to lose them). Honestly we mostly made this just in case we need them again and have lost them elsewhere, but hopefully they end up helping some of you as well!<br><br>Because theme reasons, only Bailey is listed as an author, but <a href=\"https:\/\/angrysysadmins.tech\/index.php\/author\/grassyloki\/\">Ryan<\/a>, <a href=\"https:\/\/angrysysadmins.tech\/index.php\/author\/robert\/\">Rob<\/a>, and <a href=\"https:\/\/angrysysadmins.tech\/index.php\/author\/suser\/\">James<\/a> also contribute to this list.<\/p>\n\n\n\n<p><br>Recursively search all files in current directory, printing file name, line number, and match line:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">grep -rnw .\/ -e '$search_for'<\/pre>\n\n\n\n<p><br>Generate a random number between 1 and 10:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">grep -m1 -ao '[0-9]' \/dev\/urandom | sed s\/0\/10\/ | head -n1<\/pre>\n\n\n\n<p><br>Copy a file\/folder with visible progress and checksum confirmation:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">rsync -avz --progress $from $to<\/pre>\n\n\n\n<p><br>Send a packet over TCP or UDP on a specific port<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo \"Message\" > \/dev\/udp\/$ip\/$port\necho \"Message\" > \/dev\/tcp\/$ip\/$port<\/pre>\n\n\n\n<p><br>List all zombie process ID&#8217;s:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ps aux | awk '{if ($8==\"Z\") { print $2 }}'<\/pre>\n\n\n\n<p><br>Get the name and parent of those processes:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pstree -p -s `ps aux | awk '{if ($8==\"Z\") { print $2 }}'`<\/pre>\n\n\n\n<p><br>Paste the current contents of the buffer:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">xsel --clipboard --output<br><\/pre>\n\n\n\n<p><br>Print each line of a script as it runs:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bash -x $script<\/pre>\n\n\n\n<p><br>Repeat previous command with sudo privileges:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo !!<\/pre>\n\n\n\n<p><br>Breakdown of init systems:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>Init Manager<\/td><td><a href=\"https:\/\/www.freedesktop.org\/wiki\/Software\/systemd\/\">systemd<\/a><\/td><td><a href=\"https:\/\/wiki.gentoo.org\/wiki\/OpenRC\">OpenRC<\/a><\/td><td><a href=\"https:\/\/wiki.archlinux.org\/index.php\/SysVinit\">SysV<\/a><\/td><\/tr><tr><td>List run level services<\/td><td>systemctl list-units<\/td><td>rc-update show<\/td><td>ls \/etc\/rc*.d<\/td><\/tr><tr><td>List all scripts<\/td><td>systemctl list-unit-files &#8211;type=service<\/td><td>rc-update -v show<\/td><td>service &#8211;status-all (or <code>ls \/etc\/rc*.d<\/code> or install sysv-rc-conf)<\/td><\/tr><tr><td>Basic service management<\/td><td>systemctl (start|stop|status) $service<\/td><td>rc-service $service (start|stop|status)<\/td><td>service $service (start|stop|status)<\/td><\/tr><tr><td>Add\/remove service to start at boot<\/td><td>systemctl (enable|disable) $service<\/td><td>rc-service (add|del) $service (boot|default)<\/td><td>Make sure the service file is in \/etc\/init.d, then run <code>update-rc.d $service defaults<\/code><\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Here I would like to plug our article about cloning via dd, SSH, and gzip. It has a few useful commands that end up being extremely useful under the right circumstances. Here&#8217;s the link to that:<\/p>\n\n\n\n<figure class=\"wp-block-embed-wordpress wp-block-embed is-type-wp-embed is-provider-angry-sysadmins\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"Ht8f3WzQok\"><a href=\"https:\/\/angrysysadmins.tech\/index.php\/2019\/01\/grassyloki\/copy-a-hard-drive-to-any-vm-anywhere-with-ssh\/\">Copy a Hard Drive to Any VM Anywhere with SSH<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Copy a Hard Drive to Any VM Anywhere with SSH&#8221; &#8212; Angry Sysadmins\" src=\"https:\/\/angrysysadmins.tech\/index.php\/2019\/01\/grassyloki\/copy-a-hard-drive-to-any-vm-anywhere-with-ssh\/embed\/#?secret=FhiNpPXRv5#?secret=Ht8f3WzQok\" data-secret=\"Ht8f3WzQok\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Below is a collection of useful Bash one-liners that we have come across and use semi-often (or at least often enough to not want to lose them). Honestly we mostly made this just in case we need them again and have lost them elsewhere, but hopefully they end up helping some of you as well! <br \/><a class=\"read-more-button\" href=\"https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,8,40],"tags":[88,6,64],"coauthors":[37,39,36,38],"class_list":["post-427","post","type-post","status-publish","format-standard","hentry","category-bash","category-linux","category-scripting","tag-bash","tag-linux","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Useful Bash One-liners - Angry Sysadmins<\/title>\n<meta name=\"description\" content=\"Honestly we just went through our bash histories to find entries that made us go &quot;Oh yeah, that was cool\/useful!&quot; and then wrote them here.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Useful Bash One-liners - Angry Sysadmins\" \/>\n<meta property=\"og:description\" content=\"Honestly we just went through our bash histories to find entries that made us go &quot;Oh yeah, that was cool\/useful!&quot; and then wrote them here.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/\" \/>\n<meta property=\"og:site_name\" content=\"Angry Sysadmins\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-04T22:19:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-09-17T23:07:08+00:00\" \/>\n<meta name=\"author\" content=\"Cat Kasin, Ryan Parker, Rob Hancock, James Block\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Cat Kasin, Ryan Parker, Rob Hancock, James Block\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/angrysysadmins.tech\\\/index.php\\\/2019\\\/04\\\/bailey\\\/useful-bash-one-liners\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/angrysysadmins.tech\\\/index.php\\\/2019\\\/04\\\/bailey\\\/useful-bash-one-liners\\\/\"},\"author\":{\"name\":\"Cat Kasin\",\"@id\":\"https:\\\/\\\/angrysysadmins.tech\\\/#\\\/schema\\\/person\\\/151b2d23439b55b970060836f317a14d\"},\"headline\":\"Useful Bash One-liners\",\"datePublished\":\"2019-04-04T22:19:50+00:00\",\"dateModified\":\"2019-09-17T23:07:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/angrysysadmins.tech\\\/index.php\\\/2019\\\/04\\\/bailey\\\/useful-bash-one-liners\\\/\"},\"wordCount\":284,\"commentCount\":0,\"keywords\":[\"bash\",\"Linux\",\"Scripting\"],\"articleSection\":[\"Bash\",\"Linux\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/angrysysadmins.tech\\\/index.php\\\/2019\\\/04\\\/bailey\\\/useful-bash-one-liners\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/angrysysadmins.tech\\\/index.php\\\/2019\\\/04\\\/bailey\\\/useful-bash-one-liners\\\/\",\"url\":\"https:\\\/\\\/angrysysadmins.tech\\\/index.php\\\/2019\\\/04\\\/bailey\\\/useful-bash-one-liners\\\/\",\"name\":\"Useful Bash One-liners - Angry Sysadmins\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/angrysysadmins.tech\\\/#website\"},\"datePublished\":\"2019-04-04T22:19:50+00:00\",\"dateModified\":\"2019-09-17T23:07:08+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/angrysysadmins.tech\\\/#\\\/schema\\\/person\\\/151b2d23439b55b970060836f317a14d\"},\"description\":\"Honestly we just went through our bash histories to find entries that made us go \\\"Oh yeah, that was cool\\\/useful!\\\" and then wrote them here.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/angrysysadmins.tech\\\/index.php\\\/2019\\\/04\\\/bailey\\\/useful-bash-one-liners\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/angrysysadmins.tech\\\/index.php\\\/2019\\\/04\\\/bailey\\\/useful-bash-one-liners\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/angrysysadmins.tech\\\/index.php\\\/2019\\\/04\\\/bailey\\\/useful-bash-one-liners\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/angrysysadmins.tech\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Useful Bash One-liners\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/angrysysadmins.tech\\\/#website\",\"url\":\"https:\\\/\\\/angrysysadmins.tech\\\/\",\"name\":\"Angry Sysadmins\",\"description\":\"A site full of angry sysadmins here to vent and help\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/angrysysadmins.tech\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/angrysysadmins.tech\\\/#\\\/schema\\\/person\\\/151b2d23439b55b970060836f317a14d\",\"name\":\"Cat Kasin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e83bfa1b7d9ce082bd6b68938f580039db8d5571ad6c5d012e6a5243a189309e?s=96&d=mm&r=g23b0ffb86dd6c08514a66a6a50f7a0a9\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e83bfa1b7d9ce082bd6b68938f580039db8d5571ad6c5d012e6a5243a189309e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e83bfa1b7d9ce082bd6b68938f580039db8d5571ad6c5d012e6a5243a189309e?s=96&d=mm&r=g\",\"caption\":\"Cat Kasin\"},\"description\":\"I build virtual environments and challenges for Cybersecurity students to complete as a way to gain experience before graduating and entering the workforce.\",\"url\":\"https:\\\/\\\/angrysysadmins.tech\\\/index.php\\\/author\\\/bailey\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Useful Bash One-liners - Angry Sysadmins","description":"Honestly we just went through our bash histories to find entries that made us go \"Oh yeah, that was cool\/useful!\" and then wrote them here.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/","og_locale":"en_US","og_type":"article","og_title":"Useful Bash One-liners - Angry Sysadmins","og_description":"Honestly we just went through our bash histories to find entries that made us go \"Oh yeah, that was cool\/useful!\" and then wrote them here.","og_url":"https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/","og_site_name":"Angry Sysadmins","article_published_time":"2019-04-04T22:19:50+00:00","article_modified_time":"2019-09-17T23:07:08+00:00","author":"Cat Kasin, Ryan Parker, Rob Hancock, James Block","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Cat Kasin, Ryan Parker, Rob Hancock, James Block","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/#article","isPartOf":{"@id":"https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/"},"author":{"name":"Cat Kasin","@id":"https:\/\/angrysysadmins.tech\/#\/schema\/person\/151b2d23439b55b970060836f317a14d"},"headline":"Useful Bash One-liners","datePublished":"2019-04-04T22:19:50+00:00","dateModified":"2019-09-17T23:07:08+00:00","mainEntityOfPage":{"@id":"https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/"},"wordCount":284,"commentCount":0,"keywords":["bash","Linux","Scripting"],"articleSection":["Bash","Linux","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/","url":"https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/","name":"Useful Bash One-liners - Angry Sysadmins","isPartOf":{"@id":"https:\/\/angrysysadmins.tech\/#website"},"datePublished":"2019-04-04T22:19:50+00:00","dateModified":"2019-09-17T23:07:08+00:00","author":{"@id":"https:\/\/angrysysadmins.tech\/#\/schema\/person\/151b2d23439b55b970060836f317a14d"},"description":"Honestly we just went through our bash histories to find entries that made us go \"Oh yeah, that was cool\/useful!\" and then wrote them here.","breadcrumb":{"@id":"https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/angrysysadmins.tech\/index.php\/2019\/04\/bailey\/useful-bash-one-liners\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/angrysysadmins.tech\/"},{"@type":"ListItem","position":2,"name":"Useful Bash One-liners"}]},{"@type":"WebSite","@id":"https:\/\/angrysysadmins.tech\/#website","url":"https:\/\/angrysysadmins.tech\/","name":"Angry Sysadmins","description":"A site full of angry sysadmins here to vent and help","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/angrysysadmins.tech\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/angrysysadmins.tech\/#\/schema\/person\/151b2d23439b55b970060836f317a14d","name":"Cat Kasin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/e83bfa1b7d9ce082bd6b68938f580039db8d5571ad6c5d012e6a5243a189309e?s=96&d=mm&r=g23b0ffb86dd6c08514a66a6a50f7a0a9","url":"https:\/\/secure.gravatar.com\/avatar\/e83bfa1b7d9ce082bd6b68938f580039db8d5571ad6c5d012e6a5243a189309e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e83bfa1b7d9ce082bd6b68938f580039db8d5571ad6c5d012e6a5243a189309e?s=96&d=mm&r=g","caption":"Cat Kasin"},"description":"I build virtual environments and challenges for Cybersecurity students to complete as a way to gain experience before graduating and entering the workforce.","url":"https:\/\/angrysysadmins.tech\/index.php\/author\/bailey\/"}]}},"_links":{"self":[{"href":"https:\/\/angrysysadmins.tech\/index.php\/wp-json\/wp\/v2\/posts\/427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/angrysysadmins.tech\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/angrysysadmins.tech\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/angrysysadmins.tech\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/angrysysadmins.tech\/index.php\/wp-json\/wp\/v2\/comments?post=427"}],"version-history":[{"count":37,"href":"https:\/\/angrysysadmins.tech\/index.php\/wp-json\/wp\/v2\/posts\/427\/revisions"}],"predecessor-version":[{"id":677,"href":"https:\/\/angrysysadmins.tech\/index.php\/wp-json\/wp\/v2\/posts\/427\/revisions\/677"}],"wp:attachment":[{"href":"https:\/\/angrysysadmins.tech\/index.php\/wp-json\/wp\/v2\/media?parent=427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/angrysysadmins.tech\/index.php\/wp-json\/wp\/v2\/categories?post=427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/angrysysadmins.tech\/index.php\/wp-json\/wp\/v2\/tags?post=427"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/angrysysadmins.tech\/index.php\/wp-json\/wp\/v2\/coauthors?post=427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}