On Linux, you can find several commands with unusual functionalities. One such command is seq, which outputs a sequence of numbers depending on the arguments specified.
But what can you possibly do with a command-line utility that throws a bunch of digits at you? You’ll find out in this guide.
What Is the seq Command?
As mentioned above, the seq command in Linux quickly generates a sequence of numeric characters. Users can pass arguments to the command to generate different combinations of numbers. For example, you can get an incremented list by simply passing an additional argument to seq.
What’s the practical use of the command though? While seq might not seem like a powerful tool in its entirety, you can benefit from the command by implementing it with other Linux utilities. You can also use seq in bash scripts to unveil its true power.
How to Use seq in Linux
Seq only takes a few arguments, which makes it an easy-to-learn tool for anyone.
The basic syntax of the command is:
seq options numbers
…where options are the flags that you can specify to invoke different methods of the command and numbers are the arguments that you pass to generate the numeric sequence.
Generate a List of Numbers
Seq arguments follow the input format given below:
seq first last
seq first increment last
When you only specify one number, seq interprets it as the upper limit for the list and generates a sequence starting from one up to the number specified.
The aforementioned command will output the following:
When seq receives two numbers as the input, it interprets them as the lower limit and upper limit for the sequence. To generate a list of numbers from four to eight:
seq 4 8
But when you pass three numbers to the command, it interprets the second argument as the increment number. For example:
seq 3 2 13
The aforementioned command will output a list of numbers starting from three up to 13 with an increment of two.
Add a Separator Between Numbers
By default, seq uses a newline character as the separator for the list. This is the reason why each number in the list is on a separate line.
You can change this default behavior and use a custom separator using the -s flag. To use the Period (.) character as the separator:
seq -s . 3 7
Keep in mind that some characters like the Tilde (~) must be enclosed within quotes. This is because the terminal uses the Tilde character to denote the /home directory, and that would reflect in the output if you don’t add the quotes.
seq -s ~ 3 7
On the other hand, when you wrap the separator with quotes:
seq -s '~' 3 7
Tweak the Output Format
You can also change the format for the output sequence using the -f flag. By default, seq extracts the format style from the user input. For example, if you specify the numbers 0.1 and 0.5, the default output will have a floating-point number format.
seq 0.1 0.5
You can specify a custom output format using the various conversion specifications like %a, %e, %f, %g, %A, %E, %F, and %G.
You can use the %f specifier if you want the output to follow a floating-point number format.
seq-f %f 4 7
To modify the precision up to two decimal points:
seq -f %0.2f 4 7
You can also transform the output completely by specifying an output template. For example, to get a list of all the IP addresses that start with 192.168.5.x:
seq -f 192.168.5.%g 1 233
To add padding to the output, you can use the -w flag. The -w flag maintains the width of the output in accordance with the largest number specified.
To generate a sequence of numbers between one and 1,000 with an increment of 100 while maintaining a constant width:
seq -w 1 100 1000
Get seq Command Line Help
While seq is easy to use, sometimes users might feel the need to check the manual page for the command. The –help flag will display the seq man page:
As already mentioned, seq is primarily used with other Linux commands, for example, touch and expr.
Perform Mathematical Operations
If you want to quickly add or subtract a particular range of numbers, you can do so easily by using seq inside expr, which is a Linux command that treats the input as an expression and displays the corresponding output.
To add all the numbers between one and 100:
expr `(seq -s " + " 1 100)`
The seq command generates an output as follows:
1 + 2 + 3 + 4 + 5 + 6...
Expr treats it as an input expression and outputs the solution.
You can perform other mathematical operations by simply replacing the separator in the seq command with other operators.
Quickly Create Multiple Files
If you want to create multiple files on Linux whose names follow a similar pattern, you can do so easily using the touch command and seq.
For example, to create 10 files with the name file-x.txt, where x is a number from one to 10:
touch $(seq -f "file%g.txt" 1 10)
Touch will create the files for you in a flash.
Implementing seq in Scripts
Consider you’re writing a network scanner tool like Nmap in bash, you might want to get a list of all the open ports in a network. But for that, you need to ping every port (65535 in total) and analyze the response.
To save some time, you can choose to use seq and generate a list of IP addresses and port combinations that you can use in your script.
Let’s assume you want to get the list of all the ports of a device with the IP address 184.108.40.206. Here’s a quick command to generate the desired output:
seq -f 220.127.116.11:%g 1 65535
You can then use this output as a list and traverse through it, checking each port using your script and analyzing whether it is open or not.
How Fast Does seq Generate the Numbers?
You might be thinking if you can achieve similar results using a for loop in bash, why choose seq for the task? This is because the real power of seq lies in its speed. Seq is faster than any other command that generates a sequence of numbers on Linux.
You can even test its speed using the time utility on Linux. Let’s see how much time does it take for seq to generate a list of one million numbers starting from one.
time seq 1000000
Looking at the output below, you can see that it only took seq around four seconds to generate a list of one million numbers.
The Power of the Linux Command Line
Seq is not the only tool in Linux that focuses heavily on delivering quick and accurate results. While you can generate a list of numbers using a for loop in bash, it is not a recommended practice considering how blazing fast seq really is.
The Linux command line gives you more control over the operating system and its functionalities, which is also a reason why you should start using the terminal over GUI today.
The Linux command line has a high ground when it comes to performance, control, and ease of use.
About The Author