Learn › Linux Foundations › Time
seq - a hands-on Linux lab on a real virtual machine.
Print a run of numbers with seq. Count to a number, choose both ends, step by any amount (the middle number is the step), zero-pad to equal width with -w, and join on one line with -s. seq is deterministic, so every number shown is exactly what you will see.
Say a job needs to run one hundred times. Or you need one hundred files named neatly, part-001 through part-100. Or a script has to walk every number from 2 to 200, in twos. Typing those out by hand is a non-starter, and getting one wrong breaks the whole thing.
There is a tiny command whose only job is to count. You tell it where to start and where to stop, and it prints the whole run of numbers, one per line, ready to hand to anything else. It is called seq, short for sequence, and by the end of this lesson you will reach for it without thinking.
The black boxes below are a practice terminal: a safe sandbox that checks only the one command each step teaches, so you cannot break anything. Unlike a command that reads the disk or the clock, seq is fully predictable. The numbers it prints are decided entirely by the arguments you type. The outputs shown here are exactly what you will see on any Linux machine.
seq prints a sequence of numbers, one on each line, and then stops. That is the whole job. It does not do math, it does not loop on its own, it does not read files. It counts, out loud, and hands the list to whatever comes next.
The reason it matters is that so much of Linux work is repetition. Run this five times. Make ten numbered folders. Step through a range. Other tools do the repeating; seq supplies the numbers they repeat over. Think of it as the tally counter at a door: click, click, click, one number at a time, in order.
seq comes from GNU coreutils, the same core toolbox that gives you cat, head, and wc. It has been a quiet fixture on Linux systems for decades, and it does one thing so reliably that nobody thinks about it until they need a hundred of something.
The simplest form takes a single number. Hand seq one value and it counts from 1 up to that value, printing each number on its own line. Nothing to configure, nothing to remember.
Before you run the next line, commit to an answer in your head: if you type seq 5, how many lines print, and what is the first one? Hold that thought, then run it:
seq 5
prompt: student@linuxcamp:~$ answer: seq 5 output: 1 2 3 4 5 hint: Type seq, a space, and a single number: seq 5
Five lines, starting at 1. With one number, seq always starts from 1 and counts up to the number you gave, one per line. That is the default: seq LAST means "count from 1 to LAST". The number you passed is the last one printed, not a count of how many, though here they happen to be the same.
Counting from 1 is not always what you want. Give seq two numbers instead, and the first becomes the starting point and the second the stopping point. It still steps up by 1, but now you choose both ends.
Ask it to count from 2 to 5:
seq 2 5
prompt: student@linuxcamp:~$ answer: seq 2 5 output: 2 3 4 5 hint: Two numbers this time, start then end: seq 2 5
It began at 2, not 1, and stopped at 5. That is the two-number form: seq FIRST LAST. The left number is where the count starts, the right number is where it ends, and the step is still 1. This is the form you will use most, because you rarely want to start at 1.
Here is the form that trips people up, so read the order carefully. Give seq three numbers and the MIDDLE one is the step, the amount it adds each time. So it is start, then step, then end. Not start, end, step.
Say that to yourself once more: with three numbers, the middle number is how much to count by. Now count from 1 to 10 in twos, and decide first which numbers you expect to see:
seq 1 2 10
prompt: student@linuxcamp:~$ answer: seq 1 2 10 output: 1 3 5 7 9 hint: Three numbers: start, then the step in the middle, then end: seq 1 2 10
The odd numbers, and it stopped at 9. Read left to right: start at 1, add 2 each time (the middle number is the step), and never go past 10. Adding 2 to 9 would reach 11, which is past the end, so seq stopped. This is the one form to memorise the order of: seq FIRST STEP LAST, with the step in the middle.
Now for the flags that make seq genuinely useful. The first solves a real annoyance: numbered files. If you name files 1, 2, up to 10, the computer sorts them 1, 10, 2, because it compares text, not value. Padding every number to the same width with leading zeros fixes that: 08, 09, 10 sort correctly.
The -w flag does exactly this. -w is short for equal width. It looks at the largest number in the run, then pads every number with leading zeros so they are all that wide. Count from 8 to 10 with it:
seq -w 8 10
prompt: student@linuxcamp:~$ answer: seq -w 8 10 output: 08 09 10 hint: Add the -w flag right after seq, then your start and end: seq -w 8 10
Because the widest number is 10 (two digits), seq padded 8 and 9 with a leading zero to match: 08, 09, 10. Now they line up and sort in the right order. This is the difference between file-08 and a mess where file-10 jumps ahead of file-8. Whenever numbers are going into filenames, reach for -w.
By default seq prints one number per line, which is perfect for feeding other tools but ugly when you just want a list. The -s flag changes the separator, the thing placed between numbers. Give it a separator and the whole sequence lands on one line, joined by whatever you chose.
Join 1 through 5 with commas. The separator comes right after -s:
seq -s , 1 5
prompt: student@linuxcamp:~$ answer: seq -s , 1 5 output: 1,2,3,4,5 hint: -s takes the separator right after it, then your start and end: seq -s , 1 5
One line, commas between every number, no newline splitting them up. -s <string> replaces the default line break with whatever you hand it, here a comma. You could pass a dash, a space, or any short string. This is how you turn a sequence into a ready-made list like 1,2,3,4,5.
There is a third formatting flag worth knowing: -f, for a printf-style format. seq -f "%02g" 8 10 prints 08 09 10, the same as -w here, but -f lets you go further, like seq -f "server-%g" 1 3 for server-1, server-2, server-3. The width in a format like %02g depends on the number you pass it, so it is the one seq behavior whose exact output changes with your arguments, not with the machine.
Two error messages show up often enough to recognise on sight. Both come straight from seq, so the text is exactly what you will see.
The first: you hand seq something that is not a number.
seq: invalid floating point argument: 'x'
seq only counts numbers. A letter, a typo, or an empty value trips this. The 'x' in the message is the exact argument it choked on, so check that you typed a number there and not a letter.
The second: you forget to give it any number at all.
seq: missing operand
An operand is just a value the command needs to do its job, here the number to count to. You ran seq with nothing after it. Give it at least one number and it is happy.
Scaffolding off. No command is shown this time, and it is a twist on what you know.
You want to count DOWN, from 5 to 1: 5 4 3 2 1. Here is the catch you can reason out from the three-number form: seq adds the step each time, so to go downward the step has to be negative. You have the shape already, seq FIRST STEP LAST, with the step in the middle. Start at 5, end at 1, and make the middle number a negative 1.
prompt: student@linuxcamp:~$ answer: seq 5 -1 1 output: 5 4 3 2 1 hint: Three-number form, start 5, end 1, and a step of minus one in the middle: seq 5 -1 1
That is the whole trick. With seq 5 -1 1, the start is above the end, so a plain step of 1 would print nothing (you cannot count up from 5 to 1). A step of -1 in the middle tells seq to subtract 1 each time, so it counts down: 5, 4, 3, 2, 1. Any time the first number is bigger than the last, the step has to be negative, or seq prints nothing at all. You worked that out from a form you already knew, which is the recall the real machine will ask of you.
You earned this cheat sheet. Every row is a form of seq you just ran:
The one thing to fix in memory: with three numbers, the MIDDLE one is the step. seq FIRST STEP LAST. Get that order right and every other form falls into place.
seq on its own just prints numbers. Its real power comes when you feed those numbers to something else, a loop that runs a job for each number, or a command that builds a file per number. You will wire it up that way in later work. For now, owning the counting itself is the win.
The practice terminal has shown you every form of seq. You counted to a number, chose both ends, and stepped by any amount with the step in the middle. You padded to equal width with -w, joined on one line with -s, and counted down with a negative step. You typed each one yourself.
The Time module ends with one real Linux machine, the Time capstone mission, and that is where you run seq for real alongside the other timing and repetition tools from this module. A VM boots just for you. It hands you objectives that use exactly what you practiced. One difference from here: the mission shows no commands. You read the objective, you recall the command, you type it. That recall is what makes it stick.
Finish the other Time lessons, then go count something real for yourself.
Practice seq in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.