{"id":7225,"date":"2018-11-13T16:36:58","date_gmt":"2018-11-13T13:36:58","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-shuf-command\/"},"modified":"2018-11-13T16:36:58","modified_gmt":"2018-11-13T13:36:58","slug":"linux-shuf-command-tutorial-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-shuf-command-tutorial-for-beginners-with-examples\/","title":{"rendered":"Linux shuf Command Tutorial for Beginners (with Examples)"},"content":{"rendered":"<article readability=\"58.820099731492\">\n<p>If you ever played the game of cards, you&#8217;d likely be aware of the term shuffling. A bit hard to imagine, there&#8217;s a Linux command line tool that exactly does that with lines in files. In this tutorial, we will discuss the basics of the &#8216;shuf&#8217; command using some easy to understand examples.<\/p>\n<p>But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-shuf-command\">Linux shuf command<\/h2>\n<p>The shuf command in Linux lets you generate random permutations. Following is its syntax:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">shuf [OPTION]... [FILE]<\/code><\/pre>\n<p>And here&#8217;s is how the tool&#8217;s man page explains it:<\/p>\n<pre><code spellcheck=\"false\">Write a random permutation of the input lines to standard output.<\/code><\/pre>\n<p>Following are some Q&amp;A styled examples that should give you a better idea on how the shuf command works.<\/p>\n<h2 id=\"q-how-to-use-shuf-command\">Q1. How to use shuf command?<\/h2>\n<p>Basic usage is very simple. Just pass a file name as input. Here&#8217;s an example:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">shuf test.txt<\/code><\/pre>\n<p>Now, the test.txt file contains the following lines:<\/p>\n<pre><code spellcheck=\"false\">1<br\/>2<br\/>3<br\/>4<br\/>5<br\/>6<br\/>7<br\/>8<br\/>9<\/code><\/pre>\n<p>But the shuf command above produced the following output:<\/p>\n<pre><code spellcheck=\"false\">3<br\/>4<br\/>6<br\/>5<br\/>1<br\/>8<br\/>2<br\/>7<br\/>9<\/code><\/pre>\n<p>So you can see the lines got shuffled.<\/p>\n<h2 id=\"q-how-to-limit-the-number-of-lines-in-output\">Q2. How to limit the number of lines in output?<\/h2>\n<p>If you want to limit the number of lines in shuf output, use the -n command line option, which requires you to pass a number that represents the maximum count of lines you want in output.<\/p>\n<p>For example:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">shuf -n 3 test.txt<\/code><\/pre>\n<p>The above command produced the following output:<\/p>\n<pre><code spellcheck=\"false\">6<br\/>4<br\/>9<\/code><\/pre>\n<h2 id=\"q-how-to-allownbsprepetition-of-lines-in-output\">Q3. How to allow\u00a0repetition of lines in output?<\/h2>\n<p>Suppose the input file contains 9 lines, but you want shuf to produce more lines than that in output (using the -n option discussed above). This can be done using the -r option.<\/p>\n<pre><code spellcheck=\"false\">-r, --repeat<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 output lines can be repeated<\/code><\/pre>\n<p>So if you want shuf to produce 25 lines in output, then run the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">shuf -r -n 25 test.txt<\/code><\/pre>\n<p>Here&#8217;s the output this command produced:<\/p>\n<pre><code spellcheck=\"false\">6<br\/>8<br\/>4<br\/>5<br\/>9<br\/>3<br\/>3<br\/>1<br\/>2<br\/>5<br\/>7<br\/>5<br\/>9<br\/>5<br\/>8<br\/>5<br\/>2<br\/>8<br\/>8<br\/>7<br\/>4<br\/>4<br\/>8<br\/>5<br\/>3<\/code><\/pre>\n<h2 id=\"q-how-to-make-shuf-treat-input-arguments-as-lines\">Q4. How to make shuf treat input arguments as lines?<\/h2>\n<p>For this, use the -e command line option. Here&#8217;s an example:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">shuf -e 1 2 3 4 5 6 7 8 9<\/code><\/pre>\n<p>Here&#8217;s the output produced by this command:<\/p>\n<pre><code spellcheck=\"false\">9<br\/>1<br\/>7<br\/>8<br\/>6<br\/>2<br\/>3<br\/>5<br\/>4<\/code><\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Agreed, shuf is not the kind of tool you&#8217;d require every day, but there&#8217;s no harm in learning about this utility especially since its learning curve is not that steep. Once you&#8217;re done practicing whatever we&#8217;ve discussed here, head to the tool&#8217;s\u00a0<a href=\"https:\/\/linux.die.net\/man\/1\/shuf\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> to know more about it.<\/p>\n<\/article>\n<aside class=\"authorbox\" readability=\"10\">\n<img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-shuf-command-tutorial-for-beginners-with-examples.jpg\" alt=\"Himanshu Arora\" title=\"\"> <\/p>\n<p><strong>About Himanshu Arora<\/strong><\/p>\n<p>Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu&#8217;s work has also been featured in some of world&#8217;s other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.<\/p>\n<\/aside>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-shuf-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-shuf-command-tutorial-for-beginners-with-examples.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-shuf-command%2F&amp;text=Linux+shuf+Command+Tutorial+for+Beginners+%28with+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-shuf-command-tutorial-for-beginners-with-examples-1.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-shuf-command-tutorial-for-beginners-with-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-shuf-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-shuf-command-tutorial-for-beginners-with-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you ever played the game of cards, you&#8217;d likely be aware of the term shuffling. A bit hard to imagine, there&#8217;s a Linux command line tool that exactly does that with lines in files. In this tutorial, we will discuss the basics of the &#8216;shuf&#8217; command using some easy to understand examples. But before [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-7225","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=7225"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7225\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=7225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=7225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=7225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}