On Linux you will find several commands with unusual functions. One such command is seq, which outputs a sequence of numbers depending on the given arguments.
But what can you do with a command line utility that throws a lot of digits at you? You will 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 various combinations of numbers. For example, you can get an incremented list simply by passing an additional argument to seq.
But what is the practical use of the command? While seq doesn't seem like a powerful tool in its entirety, you can take advantage of the command by implementing it with other Linux utilities. You can also use seq in bash scripts to reveal its true power.
How to use seq on Linux
Seq takes few arguments, which makes it an easy tool for anyone to learn.
The basic syntax of the command is:
seq option numbers
…Where Options are the flags you can specify to invoke various methods of the command and numbers are the arguments that you pass in 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
If you only enter one number, seq interprets this as the upper limit for the list and generates a sequence starting with one up to the specified number.
The above command outputs:
When seq receives two numbers as input, it interprets them as the lower bound and upper bound for the sequence. To make a list of numbers from four to eight:
Continuation 4 8
However, if you pass three numbers to the command, it interprets the second argument as an increment number. For example:
Continued 3 2 13
The above command prints a list of numbers from three to 13 in increments of two.
Add separators between numbers
By default, seq uses a newline character as a separator for the list. For this reason, each number in the list is on a separate line.
You can change this default behavior and use a custom separator by typing -s Flag. To use that Period (.) Characters as separators:
seq -s. 3 7
Remember, some characters like that tilde (~) must be in quotation marks. This is because the terminal has the tilde Characters for designating the /At home Directory, and that would be reflected in the output if you didn't add the quotes.
seq -s ~ 3 7
3 / home / 4 / home / 5 / home / 6 / home / 7
If, on the other hand, you enclose the separator in quotation marks:
seq -s & # 39; ~ & # 39; 3 7
3 ~ 4 ~ 5 ~ 6 ~ 7
Optimize the output format
You can also change the format for the output order with the button. to change -f Flag. By default, seq extracts the format style from user input. For example, if you specify the numbers 0.1 and 0.5, the standard output is in a floating-point number format.
Seq 0.1 0.5
You can specify a custom output format using the various conversion specifications such as% a,% e,% f,% g,% A,% E,% F and% G.
You can use the … % f -Identifier if the output is to follow a floating point number format.
seq-f% f 4 7
To change the precision up to two decimal places:
seq -f% 0.2f 4 7
You can also completely transform the output by specifying an output template. For example, to get a list of all IP addresses that start with. start 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 according to the largest number specified.
How 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 the seq command line help
Although seq is easy to use, users sometimes feel the need to check the manual page for the command. The –Help flag displays the seq man page:
As mentioned earlier, seq is mainly used with other Linux commands, such as touch and expr.
Perform math operations
If you want to quickly add or subtract a certain range of numbers, you can easily do so using seq inside Expression, a Linux command that treats input as an expression and displays the appropriate output.
To add up all the numbers between one and 100:
expr `(seq -s" + "1 100)`
The seq command produces output like this:
1 + 2 + 3 + 4 + 5 + 6 …
Expr treats it as an input expression and prints the solution.
You can perform other math operations by simply replacing the separator in the seq command with other operators.
Create multiple files quickly
On Linux, if you want to create multiple files whose names follow a similar pattern, you can just do so using the touch and seq command.
For example, to create 10 files named create File-x.txt, Where x is a number from one to 10:
touch $ (seq -f "file% g.txt" 1 10)
Touch creates the files for you in no time.
Implementing seq in scripts
Assuming you write a network scanner tool like Nmap in bash, you might want to get a list of all the open ports on a network. To do this, however, you need to ping each port (a total of 65535) and analyze the response.
To save time, you can use seq and make a list of IP addresses and port combinations to use in your script.
Let's say you want to get the list of all ports of a device with the IP address 220.127.116.11. Here is a quick command to generate the output you want:
seq -f 18.104.22.168:%g 1 65535
You can then use that output as a list and iterate over it, checking each port with your script and analyzing whether it is open or not.
How quickly does seq generate the numbers?
You may be thinking that if you can do similar results using a for loop in bash, why choose seq for the task? This is because seq's real strength lies in its speed. Seq is faster than any other command that generates a sequence of numbers on Linux.
You can even test the speed with the time utility on Linux. Let's see how long it takes for seq to generate a list of a million numbers starting with one.
Time sequence 1000000
If you look at the output below, you can see that it only took seq about four seconds to generate a list of a million numbers.
The power of the Linux command line
Seq isn't the only tool on Linux that has a heavy focus on delivering quick, accurate results. While you can use a for loop in bash to generate a list of numbers, it is not recommended considering how fast seq really is.
The Linux command line gives you more control over the operating system and its functions. This is also one of the reasons why you should be using the terminal via the GUI today.
5 reasons to choose the Linux terminal via the GUI
The Linux command line is very important when it comes to performance, control, and ease of use.
About the author
(65 published articles)
Deepesh is Junior Editor for Linux at MUO. He has been writing informational content on the Internet for over 3 years. In his spare time he enjoys writing, listening to music and playing the guitar.
From Deepesh Sharma
Subscribe to our newsletter
Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive offers!
One more step …!
Please confirm your email address in the email you just sent.