The seq command in Linux: generate sequences of numbers

Introduction

In the Linux world, the ability to quickly generate number sequences is essential for tasks such as creating filenames, testing loops, or feeding data to other programs. The seq command offers a simple and powerful solution for producing numeric lists with just a few keystrokes. In this article we will explore its syntax, its most useful options, and several practical examples you can apply in your scripts and on the command line.

Basic Syntax

The simplest way to use seq is to specify an ending number. For example:

seq 5

This will print the numbers from 1 to 5, each on a separate line. If you also want to define the starting number, just provide two arguments:

seq 3 8

The result will be the sequence 3, 4, 5, 6, 7, 8. Finally, you can add a third argument that represents the increment (or step) between values:

seq 0 2 10

This line generates 0, 2, 4, 6, 8, 10. Note that the last number does not exceed the set limit.

Useful Options

  • -f or –format: Allows applying a printf format to each number. For example, seq -f '%02g' 1 9 produces 01, 02, …, 09.
  • -s or –separator: Changes the separator between numbers. By default it is a newline; with seq -s ', ' 1 5 you get 1, 2, 3, 4, 5.
  • -w or –equal-width: Pads with leading zeros so that all numbers have the same width, useful when alignment is needed.
  • –help and –version: Display the help and version of the command.

Practical Examples

Below are some scenarios where seq is particularly useful.

Creating numbered filenames

Suppose you need to generate ten test files named file

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .