Now on ScienceBlogs: The Galaxy's Biggest Valentine

ScienceBlogs Book Club: Inside the Outbreaks

Greg Laden's Blog

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

Hornbill170.jpg Looking for stuff about birds?

Darwing_Face.jpg Learn more about Charles Darwin and his work.

Lion_mane170.jpg Lean more about lions

Congo_sidebar.jpg An archaeological expedition to the Congo


The Skeptical Search Engine


Nature Blog Network
Climate Defense Fund


The contents of Greg Laden's Blog are copyrighted by Greg Laden.

Recent Comments

Search

Profile


Click on "About" for the big picture, and "Archives" for the details.


Recent Posts

Blogroll

If you don't see yourself on my blogroll, just drop me a line and let me know. I'll add you.*
*Assuming that I'm on your blogroll, of course!

Archives

« 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 on Facebook
Share on StumbleUpon
Share on Facebook

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
Follow ScienceBlogs on Twitter

© 2006-2011 ScienceBlogs LLC. ScienceBlogs is a registered trademark of ScienceBlogs LLC. All rights reserved.