seq command generates lists of numbers in the blink of an eye. But how can this functionality be put to practical use? We’ll show you how seq might come in handy for you.
The seq Command
At first glance, the Linux
seq command appears to be something of an oddity. It allows you to generate sequences of numbers quickly and that’s it! The keyword here, though, is “quickly.” In a moment, you’ll see just how fast this little command can run.
Regardless of how they’re generated, though, how useful is a list of numbers? The
seq command was added to the 8th edition of Unix in 1985. It’s been there ever since, so it must do something worthwhile.
The philosophy of Unix is that it’s full of little utilities that do one thing and do it well. One of the central tenets of this philosophy is to write programs that accept input from other programs. Of course, that also means these programs have to generate output that can be used as input by other programs.
seq command comes into its own when it’s used with other commands that make use of its output, either through pipes or command-line expansion.
Basic List Generation
If you launch
seq with a single number as a command-line parameter, it counts from one to that number. It then prints the numbers in the terminal window, one number per line, as shown below:
If you type two numbers on the command line, the first will be the start number and the second will be the end number, as shown below:
seq 4 10
You can set a step size by including a third number. It sits between the start and end numbers. We type the following to ask
seq to create a list of numbers that starts with six, ends at 48, and uses a step of six:
seq 6 6 48
We can also ask
seq to create a list of numbers from highest to lowest. To do so, however, we must provide a step that’s negative.
The following command produces a list that counts from 24 to 12 in steps of 6 because we type the step as a negative number:
seq 24 -6 12
Counting With Decimals
The start, end, and step numbers can also be decimals. If any of the numbers is a decimal, the others are also treated as decimals. The following command generates a list of numbers with a step of 0.2:
seq 1 0.2 2
The Speed of seq
seq is blazingly fast—the only bottleneck is the time it takes you to type the command in the terminal window. To test its speed, let’s ask for a list of 250,000 numbers.
We type the following, using the
time command to see how long the process takes to complete:
time seq 250000
The results are displayed below the list. Even on our moderately powered test PC,
seq is surprisingly fast.
The entire list was created and written to the screen in about 1/3 of a second. If we redirect the list into a file, we can even avoid the overhead of typing in the terminal window.
To do so, we type the following:
time seq 250000 > numbers.txt
The time it takes to complete the list and create the file is now about 1/7 of a second.
Using a Separator
A new line character is the default character displayed between each number in a list. That’s why they appear as a vertical list, with each number on its own line. If you need to, you can provide another separator.
For example, say you need to create a comma-delimited list, a list divided by colons, or any other punctuation mark or symbol. The delimiter is actually a string, so you can use more than one character.
We’ll use the
-s (separator) option. The following command will produce a comma-delimited list:
seq s, 6 6 36
This command will use a colon (
:) as the separator:
seq -s: 6 6 36
This command tells
seq to use two hyphens (
-) as the separator:
seq -s-- 6 6 36
Using Format Strings
seq command also supports C language-style format strings. These allow you to format the output with much more control than just specifying a separator. To use a format string, you use the
-f (format) option.
The following command tells
seq to use zeroes to pad the output to two characters:
seq -f "%02g" 6
We can format the string with any text we like, and place the number anywhere in the string, as follows:
seq -f "Number %02g in a C-like format string" 6
A Quick Way to Set Zero Padding
The quickest way to set zero padding is to use the
-w (equal width) option. This tells
seq to use zeroes to pad the numbers, so they’re all the same width as the largest number.
The following command counts from 0 to 1,000 in steps of 100, and all numbers will be padded with zeroes:
seq -w 0 100 1000
The longest number takes four characters, so all narrower numbers are padded with zeroes to that width (even 0 is padded to four zeroes).
Piping seq Into bc
By setting the separator as a mathematical symbol and piping the list into the
bc command, we can evaluate the numbers in that list.
The following command generates a list of numbers separated by asterisks (
*), starting at one and ending with six:
seq -s* 6
If we feed that list into
bc, it evaluates the list using the asterisks (
*) as multiplication symbols:
seq -s* 6 | bc
We can do this with other symbols, as well. The command below uses a plus sign (
+) to create a list in which all the numbers are added:
seq -s+ 5
We type the following to pipe that into
bc and evaluate the list:
seq -s+ 5 | bc
Creating Files With seq
touch command updates time and date stamps on files. If the file doesn’t exist, touch creates it. We can use command-line expansion with
seq to create a collection of thematically-named, but differently numbered files.
We’ll create a set of 10 files with the same base name and a different number (file-1.txt, file-2.txt, and so on). We type the following:
touch $(seq -f "file-%g.txt" 1 10)
Then, we type the following to check the files:
Using seq in Bash Loops
We can use
seq in Bash scripts to control loops with decimals.
Type the following text into an editor, and then save it as “loops.sh”:
#!/bin/bash for val in $(seq 5 0.2 6.6); do echo "The value is now: $val" done
Next, we type the following to make our new script executable:
chmod +x loop.sh
When we run the script, the loop counter is printed in the terminal window. We can then type the following to see the decimal loop counter increase with each iteration of the loop:
seq can count backwards, too; you can use that in loops in the same way.
Nice and Simple
One thing about
seq is there’s not much of a learning curve. It has a refreshingly short
man page, but you can still use it in interesting ways.
Because we often need to quickly create test files with realistic sizes, we use
seq with a format string. We then redirect the output to create a file containing as many lines of dummy data as we want.
|Files||tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm|
|Processes||alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg|
|Networking||netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw|
- › Why Is My Printer Offline? (and How to Get It Online)
- › Google Is Not Apple, and It Should Stop Trying to Be
- › How to Cope With Too Much Good TV: Watch the Pilot and Move On
- › Windows 11 Opens the Floodgates for Third-Party Widgets
- › It’s Okay to Keep Your Tablet in a Drawer
- › The 5 Best Blu-ray Players of 2022