Redirecting Output

Sean Powers’ tip of the day:

Thank you Sean. now, can we do a better job that Sean did and come up with some actual uses for this ability?

Here are the last few things I did with redirect.

I wanted to convert a series of files from .odt (word processor) format to text format, so I used ls to produce a list of files, redirected that list to a file, then stepped through that file to feed the file names to the conversion filter.

Later on, I took those very self same text files and mushed them all together to form one big file. I used the double carrot for that .

When I make my blogroll, there are a number of steps involved (it is all done with a bash script) and I use redirects at several stages, including running the master list (to which I’ve added sites over time) through a sorting filter that also produces a unique list … the output of which goes into a file.

Any other suggestions?

Comments

  1. #1 SteveC
    May 9, 2009

    You’re kidding, right?

    You can also redirect input with less-than. Also, look up “here document” if you don’t know what that is. You can redirect stdout and stderr with 1>&2 (that’s 1 greater-than ampersand 2, in case the blog eats what I typed.) Look up “tee” as well.

    Sometimes I want to watch output in real time as well as capture it in a file. You can do that with tee, or, something like:

    some_interesting_command > file & tail -f file

    Redirection is such a basic concept, asking for uses is a bit like asking what a lego brick can be used for.

  2. #2 Greg Laden
    May 9, 2009

    SteveC: You should be more respectful. People who are just starting out don’t need that kind of crap. I shall now rewrite your comment to show you how it is done.

    Steve says:

    Hey, no kidding, redirection is one of those basic, fundemental things that makes Linux so cool and powerful, right? Here’s some ideas for some of your readers who are experimenting with this to play around with:

    You can also redirect input with less-than.

    Also, look up “here document” and try that. It isn’t redirection but for some of the uses to which one might put redirection it works better.

    Normally, redirection sends standard output (stdout) to where the greater than or lesser than sign points, but you can also be more specific and versitle using amperstand notation.

    Also very cool is redirecting to two places at once using “tee” … that is worth looking up.

    Sometimes I want to watch output in real time as well as capture it in a file. You can do that with tee, or, something like:

    some_interesting_command > file & tail -f file

    (the amperstand in this case runs something in the background.

    Redirection is such a basic concept, asking for uses is a great idea to encourage beginning users, which is a perfect followup on this video, which is in a series of basic bash command instructions.

  3. #3 Fargo
    May 9, 2009

    I recall many moons ago working someplace where we had all this documentation on the various sites we had to support, but it was only available to us if we were at our site, which was retarded since we always had to work on-site.

    That’s a disturbing repetition of the word site.

    Anyway, I got fed up with it, made a simple html header and footer file to enclose the documentation in a pre tag, then wrote a simple script to cat all those docs to html files named by building. From there it was just a highly covert upload to geocities, and suddenly my life was so much easier.

  4. #4 SteveC
    May 9, 2009

    Sorry, didn’t mean to come off as an ass, though I seem to do it quite frequently. I guess I am kind of an ass. Oh well.

  5. #5 Jason Thibeault
    May 9, 2009

    I wanted to convert a series of files from .odt (word processor) format to text format, so I used ls to produce a list of files, redirected that list to a file, then stepped through that file to feed the file names to the conversion filter.

    Later on, I took those very self same text files and mushed them all together to form one big file. I used the double carrot for that .

    I did something very similar, converting all my .ogg’s to .mp3 format, in one of my very first posts over at my own blehg, here: http://www.lousycanuck.ca/?p=9

    However, I didn’t use output redirection, using instead a bash for loop (specifically, for i in *.ogg) to iterate through each filename, as all my files had spaces in them, and I didn’t want to have to output ls to a file then read each line back in. In fact, in the post, I specifically mention a pitfall of trying to use ls in-line in this method. So for your particular example, it can be done easier without output redirection (or, I should say, _possibly_ — I haven’t seen your script, so I don’t really know).

  6. #6 Greg Laden
    May 9, 2009

    I don’t remember, but I think I used the file holding the filenames because the utility I was using would not create an output file with a new extention (or whatever). It had to be given an explicit name. There are a lot of ways to construct a name using bash, but this was easy.

    ls by itself outputs filenames separated by newlines. Actually, the output of ls > filename ; cat filename should be the same as just ls, which will be file names separated by newlines, which should act the same way when iterated over. So my method was only necessary because of the secondary processing of the filename because the utility was a bit dumb.

    Also, I subscribe to the structured scripting school. We have a slightly different approach. We prefer to use extra processes, for instance.

  7. #7 Jason Thibeault
    May 9, 2009

    Structured scripting has its obvious advantages, e.g. modularity and reusability, especially in cases where you might not be sure what exactly you want the input or output to be, but for the quick and dirty one-offs I do all the time, they require a good deal more planning than I tend to have the patience for.

    A good use of output redirection might be a quick and dirty script to convert HTML pages to straight text, stripping out the HTML tags using sed and outputting the result to a file of your choosing. You could even get fancy and replace HTML with equivalent RTF or ODT code directly, though any such script would be reinventing the wheel with the very good conversion tools that exist presently.

Current ye@r *