Fun with the Linux seq command

Do you know about the obscure Linux command ‘seq’????? It is actually quite cool.

seq ‘prints’ (sends to standard output) a sequence of numbers. By itself it does nothing, but with a few parameters it functions nicely.

Watch:

greg@workstation:~$ seq 10
1
2
3
4
5
6
7
8
9
10
greg@workstation:~$ seq 3 9
3
4
5
6
7
8
9
greg@workstation:~$ seq 10 10 110
10
20
30
40
50
60
70
80
90
100
110
greg@workstation:~$

But why would you want to do this?

To sequentially number files that don’t exist yet using touch, for example!

seq has c-like format capabilities. For those of you who don’t know c, this means that seq understands certain wildcard like tokens that are used to format stuff.

Let’s say we want a sequence of files called “harvey” numbered from 01 to 10. The format for that would be %02g which can be put in the middle of a double-quoted string. So the string “harvey%02g” is a template. The command:

seq -f "harvey%02g" 10

will produce a sequence of ten strings each starting with harvey and ending with a number:

harvey01
harvey02
harvey03
harvey04
harvey05
harvey06
harvey07
harvey08
harvey09
harvey10

To make these words into empty files, you use the “touch” comand. “touch” followed by a legal filename will update the timestamp of the file referred to. But, if there is no file of that name, touch creates it!

So the string of made-up filenames is fed to touch as follows:

touch $(seq -f "harvey%02g" 10)

This one-liner will get you ten harveys!

Oh, the -f flag in seq tells seq to use the formatting codes.

Comments

  1. #1 Alcari
    September 19, 2009

    So, it’s like the jot command?

  2. #2 MadScientist
    September 19, 2009

    I forgot all about seq; I last used it in 2003 to generate several GB of mock data files while testing an instrument which I was building at the time.

  3. #3 D. C. Sessions
    September 19, 2009

    And danged handy combined with for

  4. #4 Ted Powell
    September 20, 2009

    The seq command has lots of power, but if you are using the bash shell, the last example can be done more simply as:
    touch harvey{01..10}

  5. #5 harvey
    September 20, 2009

    but harvey does not like to be touched that way

  6. #6 Satish Goda
    February 14, 2012

    :D :D :D

    Nice tips btw…