The Linux seq command has been around for years, since 1985 to be exact. However, it is still one of those useful commands that many Linux users rarely use. The seq command prints a sequential list of integers or floating point numbers suitable for use in scripts and piping to other commands. With the seq command, you can control the start and end of the number sequence along with an increment. It also provides several formatting options. However, there is one limitation of the seq command is that it only works with the number, not letters.

The seq command is part of the GNU Core Utilities package that is available in just about any Linux system. In this article, we will see how to use the seq command to generate a sequence of numbers along with some examples and use cases.

Seq Command Syntax

Here is the basic syntax of the seq command:

seq [OPTION] FIRST INCREMENT LAST

In the above syntax, the [OPTION], FIRST and INCREMENT arguments are optional. The default value of FIRST and INCREMENT argument is always 1.

Basic Use of Seq Command          

The basic use of the seq command will print the sequential numbers from 1 to the last number you specify.

$ seq LAST

It will take the default increment of 1.

For example, to print the sequence of numbers from 1 to 5, specify 5 as the value of the LAST argument.

[email protected]:~$ seq 5
1
2
3
4
5

Specify Starting Number in Sequence

To start the sequence with a number other than 1, you can do:

$ seq FIRST LAST

The FIRST will be used as a starting number and LAST will be used as ending number in the generated list.

In the following example, we have printed the sequence of numbers from 3 to 5.

[email protected]:~$ seq 3 5
3
4
5

Specify The Increment Value

To print the sequence numbers with a fixed increment other than 1, use the following syntax:

$ seq FIRST INCREMENT LAST

In the INCREMENT, specify the number you want to increment by.

In the following example, we have printed the sequence of numbers from 2 to 10 with an increment of 2.

[email protected]:~$ seq 2 2 10  
2
4
6
8
10

Floating point numbers are also supported while generating the sequence number:

[email protected]:~$ seq 0.1 0.1 1
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0

Print Sequential Numbers in Reverse Order

To print sequence numbers in reverse order (from larger to smaller number), use negative increment.

In the following example, we have printed the sequence of numbers from 10 to 2 with a decrement of 2.

[email protected]:~$ seq 10 -4 2
10
6
2

Seq Command Options

Print Numbers with Printf Format Specifiers

With the -f option, Seq command allows to use printf style format for printing the numbers using the  %e, %f, %g, %E, %F, %G as the conversion characters. However, the %g is used as default.

In the following example, we have printed the Serial number from 1 to 5 using %g as the conversion character.

[email protected]:~$ seq -f "Serial No.0%g" 5
Serial No.01
Serial No.02
Serial No.03
Serial No.04
Serial No.05

In the following example, we have printed the version number in floating pint format from 1 to 5 using %f as the conversion character. The “%.2f” prints the floating numbers with 2 digits after the decimal separator.

[email protected]:~$ seq -f "Version Number %.2f" 5
Version Number 1.00
Version Number 2.00
Version Number 3.00
Version Number 4.00
Version Number 5.00

In the following example, we have printed the five sequential dates from 1st May to 5th May 2020.

[email protected]:~$ seq -f 'May %g,2020' 5
May 1,2020
May 2,2020
May 3,2020
May 4,2020
May 5,2020

Print Numbers with Same Width

To print the sequence number in the same width, use the -w option with seq command. It will add leading zeros to match the width of the largest number.

In the following example ,seq adds two leading zeros to the single digit numbers to match the width of the largest number (100).

[email protected]:~$ seq -w 100
001
002
003
...
097
098
099
100

Add Separator Between Sequential Numbers

The Seq command by default uses /n (newline) as a separator between the sequence numbers. This cause each number to print on separate line. You can specify any character as a separator between numbers like dot, comma, hyphen, etc.

To add a separator between the numbers, use -s option followed by the desired character.

In the following example, we have separated the numbers by commas using the -s, option:

[email protected]:~$ seq -s, 5
1,2,3,4,5

In the following example, we have separated the number with spaces by using the -s" ".

[email protected]:~$ seq -s" " 5
1 2 3 4 5

Seq Command Use Cases

Performing Basic Calculations

You can also perform simple calculations with seq command like addition, subtraction, multiplication, and division. For this, you will need to use any of the +,-,* and / separator and then pipe the output to the command-line calculator bc.

For example, the following command outputs the numbers from 2 to 10, incremented by 2 and separated by a + sign.

[email protected]:~$ seq -s+ 2 2 10
2+4+6+8+10

If we pipe the above output to the bc, it will perform the addition of these numbers like this:

[email protected]:~$ seq -s+ 2 2 10 | bc
30

Creating and Removing Files

With the seq command, you can create multiple files at once with sequential numbers added to the file name. In the following example, we have created three files with named file1, file2, and file3 using the seq command:

[email protected]:~$ touch $(seq -f "file%g" 3)
[email protected]:~$ ls -l	
total 24
drwxr-xr-x 2 kbuzdar kbuzdar 4096 Apr 30 17:27 Desktop
drwxr-xr-x 2 kbuzdar kbuzdar 4096 Apr 30 17:27 Documents
drwxr-xr-x 3 kbuzdar kbuzdar 4096 May  8 16:12 Downloads
-rw-r--r-- 1   kbuzdar kbuzdar    0    May  9 17:05 file1
-rw-r--r-- 1   kbuzdar kbuzdar    0    May  9 17:05 file2
-rw-r--r-- 1   kbuzdar kbuzdar    0    May  9 17:05 file3

Similarly, to remove the file, you can use

[email protected]:~$ rm $(seq -f "file%g" 3)

Writing the Sequence Number in a File

You can save the sequence number in a file rather than printing them to the shell using simple redirection.

For example, the following command prints the sequence numbers from 1 to 5:

$ seq -s 5

You can save the output in a file by using the > to redirect the output to a file, like so:

[email protected]:~$ seq 5 > myfile.txt
[email protected]:~$ cat myfile.txt 
1
2
3
4
5

Use Seq in For Loop

We can use the seq command with “for loops”. In the following example, we have used the seq command in for loop to remove files.


for n in `seq 5`
do
    rm file$n
done

Conclusion

In this article, we have learned how to use the seq command to generate the list of sequential numbers along with it’s different options. The apparently simple seq command when used alone, does not seem very useful. However, when you use seq with it’s options or pipe it to other commands, you realize its true significance.

Resources