Now on ScienceBlogs: The Lights Stay On Inside a Black Hole!

Seed Media Group

Collective Imagination

Greg Laden's Blog

Evolution, Life Sciences, Science Education, Human Evolution, and Stuff

Recent Comments

Profile


Welcome to Greg Laden's Blog.




Nature Blog Network



Search

Join the best atheist themed blogroll!

Archives

Recent Posts

« Adobe Photoshop on Linux | Main | Revolution OS »

Fun with the Linux seq command

Category: Computer TricksLinuxone-liners
Posted on: September 19, 2009 5:16 PM, by Greg Laden

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.

Share this: Stumbleupon Reddit Email + More

TrackBacks

TrackBack URL for this entry: http://scienceblogs.com/mt/pings/120161

Comments

1

So, it's like the jot command?

Posted by: Alcari | September 19, 2009 7:01 PM

2

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.

Posted by: MadScientist | September 19, 2009 9:32 PM

3

And danged handy combined with for

Posted by: D. C. Sessions | September 19, 2009 10:07 PM

4

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}

Posted by: Ted Powell | September 20, 2009 1:04 AM

5

but harvey does not like to be touched that way

Posted by: harvey | September 20, 2009 8:19 AM

Post a Comment

(Email is required for authentication purposes only. On some blogs, comments are moderated for spam, so your comment may not appear immediately.)





ScienceBlogs

Search ScienceBlogs:

Go to:

Advertisement
Enter to win a free copy of The Monty Hall Problem
Visit the Collective Imagination blog
Advertisement
Collective Imagination

© 2006-2009 Seed Media Group LLC. ScienceBlogs is a registered trademark of Seed Media Group. All rights reserved.

Sites by Seed Media Group: Seed Media Group | ScienceBlogs | SEEDMAGAZINE.COM