Understanding the seq
Command in Linux
The seq
command in Linux is a versatile tool that generates a sequence of numbers. It is especially useful in shell scripting and automating repetitive tasks. In this guide, we’ll explore its basic usage, practical applications, and examples.
Basic Usage of the seq
Command
1. Generating Numbers with Default Step
The simplest form of the command generates a sequence of numbers starting from 1 with a step of 1.
[root@server ~]# seq 5
1
2
3
4
5
2. Custom Start and End Points
By specifying two numbers, you can define the start and end points.
[root@server ~]# seq 21 25
21
22
23
24
25
3. Adding a Step
Add a third argument to specify the step size. For example, the command below generates numbers from 5 to 50 in increments of 5.
[root@server ~]# seq 5 5 50
5
10
15
20
25
30
35
40
45
50
4. Generating Numbers in Descending Order
Use a negative step size to generate numbers in descending order.
[root@server ~]# seq 10 -1 1
10
9
8
7
6
5
4
3
2
1
Practical Applications of the seq
Command
1. Creating Multiple Files
Use the seq
command with the touch
command to create multiple files at once. The -f
option formats the file names.
[root@server seq]# touch $(seq -f "file-%g.txt" 1 10)
This command creates files named file-1.txt
, file-2.txt
, …, file-10.txt
.
2. Using seq
in Loops
The seq
command is often used in scripting to iterate over a sequence of numbers.
Example: For Loop
[root@server seq]# for i in $(seq 1 10); do echo "Hello $i"; done
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
Why Use the seq
Command?
The seq
command is a powerful tool for automation, especially when working with repetitive tasks. Its practical applications include:
- Creating files with sequential names.
- Looping through a set of values in scripts.
- Generating data for testing.
Learn More
For a complete list of options and details, refer to the man page.