Readers like you help support How-To Geek. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.

Quick Links

The Linux 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.

The 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:

seq 6

seq 6 in a terminal window

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

seq 2 5 in a terminal window

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

seq 6 6 48 in a terminal window

Counting Backward

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

seq 24 -6 12 in a terminal window

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

seq 1 0.2 2 in a terminal window

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

time sqe 250000 in a terminal window

The results are displayed below the list. Even on our moderately powered test PC, seq is surprisingly fast.

Timing results for the seq 250000 command in a terminal window

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

time seq 250000 > numbers.txt in a terminal window

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

seq s, 6 6 36 in a terminal window

Using Format Strings

The 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

seq 6 in a terminal window

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

seq 6 in a terminal window

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

seq 6 in a terminal window

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

seq 6 in a terminal window

Creating Files With seq

The 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 touch and 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:

ls file*

seq 6 in a terminal window

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

seq 6 in a terminal window

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:

./loop.sh

seq 6 in a terminal window

Remember that 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.

Linux Commands

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 · scp · gzip · chattr · cut · find · umask · wc · tr

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 · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld

RELATED: Best Linux Laptops for Developers and Enthusiasts