{"id":2834,"date":"2018-02-01T20:27:10","date_gmt":"2018-02-01T17:27:10","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-seq-command\/"},"modified":"2018-02-01T20:27:10","modified_gmt":"2018-02-01T17:27:10","slug":"linux-seq-command-tutorial-for-beginners-5-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-seq-command-tutorial-for-beginners-5-examples\/","title":{"rendered":"Linux seq Command Tutorial for Beginners (5 Examples)"},"content":{"rendered":"<p>Sometimes, you come across a command line tool that offers limited functionality on its own, but when used with other tools, you realize its actual potential. Once such tool is <strong>seq<\/strong>, which prints a sequence of numbers. In this tutorial, we will discuss the basics of this command line utility using easy to understand examples.<\/p>\n<p>But before jumping on to that, it&#8217;s worth mentioning that all examples in this article have been tested on an Ubuntu 16.04 machine.<\/p>\n<h2 id=\"linux-seq-command\">Linux seq command<\/h2>\n<p>As already mentioned, the seq command lets you print a sequence of numbers. Following is its syntax:<\/p>\n<p class=\"command\">seq [OPTION]&#8230; LAST<br \/>seq [OPTION]&#8230; FIRST LAST<br \/>seq [OPTION]&#8230; FIRST INCREMENT LAST<\/p>\n<p>And here&#8217;s how the tool&#8217;s man page explains it:<\/p>\n<pre readability=\"14\"> Print numbers from FIRST to LAST, in steps of INCREMENT. If FIRST or <br\/>INCREMENT is omitted, it defaults to 1.\u00a0 That is, an omitted\u00a0 INCREMENT <br\/>defaults to 1 even when LAST is smaller than FIRST.\u00a0 <p>The sequence of numbers ends when the sum of the current number and\u00a0 <br\/>INCREMENT\u00a0 would\u00a0 become\u00a0 greater than LAST.\u00a0 FIRST, INCREMENT, and LAST <br\/>are interpreted as floating point values.\u00a0 INCREMENT is usually positive if<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 FIRST\u00a0 is smaller than LAST, and INCREMENT is usually negative if FIRST<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 is greater than LAST.\u00a0 FORMAT must be suitable for printing\u00a0 one\u00a0 argu?<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ment\u00a0 of type 'double'; it defaults to %.PRECf if FIRST, INCREMENT, and<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LAST are all fixed point decimal numbers with maximum\u00a0 precision\u00a0 PREC,<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 and to %g otherwise.<\/p><\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a better idea on how the seq command works.<\/p>\n<h2 id=\"q-how-seq-command-works\">Q1. How seq command works?<\/h2>\n<p>Basic usage is very easy. All you have to do is to pass a number to seq, and the tool will producein output numbers from 1 to the input number.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">seq 8<\/p>\n<p><a class=\"fancybox\" id=\"img-seq-basic-usage\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/seq-basic-usage.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-seq-command-tutorial-for-beginners-5-examples.png\" alt=\"How seq command works\" width=\"310\" height=\"164\" title=\"\"><\/a><\/p>\n<p>Of course, you can also specify the number from which you want the output to begin with.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">seq 3 8<\/p>\n<p><a class=\"fancybox\" id=\"img-seq-basic-usage2\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/seq-basic-usage2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-seq-command-tutorial-for-beginners-5-examples-1.png\" alt=\"Example sequence\" width=\"326\" height=\"126\" title=\"\"><\/a><\/p>\n<p>Movin on, you can also set the incremental difference, which is 1 by default. For example, if you want seq to print from 1 to 9, but with a difference of 2, then here&#8217;s how you can do that:<\/p>\n<p class=\"command\">seq 1 2 9<\/p>\n<p><a class=\"fancybox\" id=\"img-seq-basic-usage3\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/seq-basic-usage3.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-seq-command-tutorial-for-beginners-5-examples-2.png\" alt=\"Another seq command example\" width=\"344\" height=\"110\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-add-a-separator\">Q2. How to add a separator?<\/h2>\n<p>If you want, you can also have a separator to make the seq output look better. This feature is available through the <strong><em>-s<\/em><\/strong> command line option.<\/p>\n<p>For example, the following command intends to use a comma (,) as a separator:<\/p>\n<p class=\"command\">seq -s, 1 9<\/p>\n<p><a class=\"fancybox\" id=\"img-seq-s-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/seq-s-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-seq-command-tutorial-for-beginners-5-examples-3.png\" alt=\"How to add a separator\" width=\"365\" height=\"41\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-specify-output-format\">Q3. How to specify output format?<\/h2>\n<p>The seq command allows you to use printf style\u00a0floating-point FORMAT. This feature is accessible\u00a0through the <strong>-f<\/strong> command line option. The tool&#8217;s man page doesn&#8217;t have much information on how to use this option, but the info page contains the required details. Here&#8217;s what the info page says:<\/p>\n<pre readability=\"12\">`-f FORMAT'<br\/>`--format=FORMAT'<br\/>Print all numbers using FORMAT. FORMAT must contain exactly one<br\/>of the `printf'-style floating point conversion specifications<br\/>`%a', `%e', `%f', `%g', `%A', `%E', `%F', `%G'. The `%' may be<br\/>followed by zero or more flags taken from the set `-+#0 '', then<br\/>an optional width containing one or more digits, then an optional<br\/>precision consisting of a `.' followed by zero or more digits.<br\/>FORMAT may also contain any number of `%%' conversion<br\/>specifications. All conversion specifications have the same<br\/>meaning as with `printf'.<p>The default format is derived from FIRST, STEP, and LAST. If<br\/>these all use a fixed point decimal representation, the default<br\/>format is `%.Pf', where P is the minimum precision that can<br\/>represent the output numbers exactly. Otherwise, the default<br\/>format is `%g'.<\/p><\/pre>\n<p>For example, you can use this option in the following way:<\/p>\n<p class=\"command\">seq -f &#8220;%02g&#8221; 6<\/p>\n<p><a class=\"fancybox\" id=\"img-seq-f-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/seq-f-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-seq-command-tutorial-for-beginners-5-examples-4.png\" alt=\"How to specify output format\" width=\"398\" height=\"251\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-use-seq-with-other-commands-use-case-\">Q4. How to use seq with other commands? (Use case 1)<\/h2>\n<p>Suppose you want to perform addition\u00a0of some numbers, say from 111 to 121. Here&#8217;s how you can do this using seq:<\/p>\n<p class=\"command\">expr `seq -s &#8221; + &#8221; 111 121`<\/p>\n<p>Here&#8217;s the above command in action:<\/p>\n<p><a class=\"fancybox\" id=\"img-seq-practical-1\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/seq-practical-1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-seq-command-tutorial-for-beginners-5-examples-5.png\" alt=\"How to use seq with other commands\" width=\"500\" height=\"38\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-use-seq-with-other-commands-use-case--2\">Q5. How to use seq with other commands? (Use case 2)<\/h2>\n<p>Suppose you want to create a bunch of new files with names where only an integer value changes. For example, file1, file2, file3, and so on. Here&#8217;s how you can do this using seq.<\/p>\n<p class=\"command\">touch $(seq -f &#8220;file%g&#8221; 1 10)<\/p>\n<p><a class=\"fancybox\" id=\"img-seq-practical2\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/seq-practical2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-seq-command-tutorial-for-beginners-5-examples-6.png\" alt=\"How to use seq with other commands? (Use case 2)\" width=\"500\" height=\"46\" title=\"\"><\/a><\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>So now you&#8217;ll agree how useful the seq command is. If you talk about the command line options Seq offers, there&#8217;s not much of a learning curve, just that you should know when and how to use the command and its options. We&#8217;ve covered several use cases here &#8211; should be enough to get you started with the tool. For more information on Seq, head to its <a href=\"https:\/\/linux.die.net\/man\/1\/seq\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, you come across a command line tool that offers limited functionality on its own, but when used with other tools, you realize its actual potential. Once such tool is seq, which prints a sequence of numbers. In this tutorial, we will discuss the basics of this command line utility using easy to understand examples. &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-2834","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2834","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=2834"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2834\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=2834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=2834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=2834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}