Yahoo
Skip to main content
Advertisement
Advertisement
Advertisement
Advertisement

9 Practical Ways to Use the Linux seq Command

A Linux terminal with some commands.
Lucas Gouveia / How-To Geek

The seq command is far more than a simple number printer. It's a fundamental building block you can use for creating test files, controlling loops, performing network scans, and running mathematical calculations. Here are some common ways to use the seq command.

Basic Number Generation

At its core, seq prints numbers in sequence. You only need to give it a single number, and it will count from 1 up to that number. For example:

Generating basic sequences of specified integer values.

This prints numbers from 1 to 5. That's the simplest form, but the magic begins when you tweak the start, step, format, or pipe the output into other commands.

Advertisement
Advertisement

For example, if you want to start from a specific number, provide a starting number as the first argument and an ending number as the second.

This will generate numbers from 3 to 7 (i.e., 3, 4, 5, 6, 7).

Sometimes, you need to skip numbers in a sequence, like generating only even numbers or counting by fives. The increment argument, placed between the start and end values, handles this. For example, to generate odd numbers from 1 to 10, use a step of 2:

This command tells seq to start at 1, go up to 10, but take steps of 2, producing 1, 3, 5, 7, and 9.

You can also count backward. To do so, provide a negative increment value like this:

This prints numbers from 10 down to 1 with an increment of -1.

Generate Floating-Point Numbers

seq isn't limited to whole numbers; it handles floating-point (decimal) numbers just as easily. This is useful for scientific calculations or when working with data that requires decimal precision.

Advertisement
Advertisement

For example, to count from 0.5 to 2.0 in increments of 0.3, run:

Generating floating sequences using specified floating value

And you'll get a sequence of decimals with specifc step size.

Using a Custom Separator

By default, seq separates each number with a newline character. The -s (separator) flag lets you define a custom separator. For example, to generate numbers from 1 to 5 on a single line separated by a comma and a space, run:

Alternatively, you can pipe the output to xargs to place all numbers on a single line separated by spaces. xargs reads items from standard input and converts them into arguments for another command (by default, echo ).

Generate Formatted Sequences

Let's say you want to generate numbers from 1 to 10, but you want them all to be two digits long with a leading zero. To achieve this, you can use the -f option along with a format specifier like this:

Generate formatted sequence using padded zeros.

Here, %g is the standard number format, and 02 tells seq to pad the number to be 2 digits wide with leading zeros.

Advertisement
Advertisement

For the common task of simply padding all numbers with leading zeros to have the same width, seq provides the even simpler -w (equal width) flag.

This command ensures all numbers have equal width by adding leading zeros, producing 001, 002, and 099 to 100.

This type of formatting is also perfect for creating zero-padded files that sort correctly. By combining seq with touch and xargs, you can generate multiple empty files . For example, to create 5 empty, zero-padded text files, run this:

After running this, you'll have a list of files (text_01.jpg, text_02.jpg, etc.) that will always sort in the correct numerical order. You can also generate files with date-based names:

This loop gives you files for every day of March 2025.

Generate Test Files With Text

Beyond generating numbers, you can use seq to create test files with dummy data. Simply combine seq with piping and redirection along with the for loop .

Advertisement
Advertisement

For example, to test a script, you might need dummy files with some content. This script creates five files, each containing a line of text indicating its number:

Creating text files with dummy data using seq command.

Further, I also recommend using the -w option (seq -w 1 5) to pad numbers so the files sort nicely.

You can also generate files with multiple lines of content. The following script creates five files, each containing 100 uniquely numbered lines:

Here, the outer loop iterates over numbers 1 to 5 for filenames, while the inner loop generates 100 lines per file, with output redirected to a file named based on the outer loop's counter.

Repeat a Command a Specific Number of Times

Sometimes you need to run a command multiple times when you only care about the repetition, not the actual numbers. For instance, you might be testing network latency with ping or running a script repeatedly to check for a bug. seq provides a clean way to do this without writing a traditional for loop.

Advertisement
Advertisement

For example, you can use seq with xargs for stress-testing applications or checking if a service starts correctly.

This command hits the website ten times, which can help you verify if your web server handles concurrent requests properly.

seq also pairs beautifully with for loops to repeat actions. To print Hello, World! five times, you can use:

This prints the message five times.

Create Timestamps or Time Intervals

Another excellent use case of seq is generating a series of timestamps, especially when building test data or simulating logs. They could represent seconds, minutes, or days.

Suppose you need a list of every 15-minute interval in an hour for a report.

This produces 00, 15, 30, and 45.

Advertisement
Advertisement

You can also generate precise timestamps. This example prints a timestamp for every minute over the next 10 minutes:

The script prints all 10 future timestamps immediately. It does not wait one minute between outputs. If you want a real-time ticker that prints one line per minute, add sleep 60 inside the loop.

To create timestamps in an HH:MM format for a full hour, you can use a simple loop:

This will list 14:00, 14:01, all the way to 14:59. It's a powerful way to generate structured time-series data right from your terminal.

Check Port Availability

If you are a system administrator or developer, you can also use seq to scan a range of ports and check their availability. For example, you may need to deploy an application but aren't sure which port is free. Let's scan a range of ports on your local machine to see which ones are open.

Advertisement
Advertisement

We can use seq to generate port numbers and pipe them to a network utility like nc to check connections .

Check port availability using seq command.

Here is a breakdown of the command:

  • for port in $(seq 8000 8010): Loops through each port number.

  • nc -z -v -w 1 localhost $port: This is the core command that attempts a connection.

  • -z: Scans for listening services without sending data.

  • -v: Provides verbose output (which we hide).

  • -w 1: Sets a timeout of 1 second.

  • &> /dev/null: Redirects all output from nc so we only see our final message.

  • && echo "Port $port is OPEN": The && operator ensures the echo command only runs if the nc command succeeds (i.e., the port is open).

These methods require appropriate privileges and tools. Use them responsibly and only on systems you own or have permission to test.

Advertisement
Advertisement

Alternatively, you can use a Bash-specific feature that doesn't require nc:

Check port availability using seq with bash specific feature wihout using nc.

For serious port scanning, you should use dedicated tools like nmap , but for quick checks during development, this seq method works great.

Loop Through Ranges in Scripts

While Bash has its own ways to create loops, using seq is often more portable and readable, especially for complex ranges or floating-point numbers. When writing scripts, seq makes looping over numeric ranges clean and clear.

For example, to process a batch of items between two points, you can define START and END variables. The loop handles the rest:

This script is easy to maintain, you only need to change the variables at the top.

Advertisement
Advertisement

In addition, you can apply the same idea to real-world scenarios like batch data processing. For instance, you could check disk usage for a series of numbered directories using this:

Because seq supports padding, you can keep everything neatly ordered. Creating daily backups for a whole month becomes straightforward with:

This one-liner creates 31 compressed archives with properly formatted names (backup_2025-09-01.tar.gz, etc.), which makes sorting and retrieval much easier.

Split Files Based on Lines

If you need to split a large file into smaller chunks , seq can help you calculate the line ranges for each chunk. While the split command is designed for this, using seq offers more granular control over the process and file naming.

For example, to split bigfile into chunks of 100 lines each, run this:

This script first gets the total line count. The for loop then uses seq to generate the starting line number for each chunk (1, 101, 201, etc.). Inside the loop, sed extracts that block of lines and saves it to a uniquely named file.

Perform Mathematical Operations

seq is a great companion for command-line calculators like bc and awk . You can pipe a series of numbers directly into another program to perform a calculation.

To sum all numbers from 1 to 100, use this:

Here, instead of separating numbers with newlines, we place a + sign between them. The output is a single string sent as input to bc, which evaluates the expression.

With awk, you can achieve the same result:

Performing mathmatical operation using seq command along with awk and bc.

This pattern of generating, formatting, and calculating is extremely useful for quick data analysis on the command line.


Although seq seems like a minor utility, it elegantly solves many small, repetitive problems. It combines with other Linux commands to create powerful one-liners and scripts, making it an essential tool for any command-line user.

Advertisement
Advertisement
Mobilize your Website
View Site in Mobile | Classic
Share by: