February 22, 2019

Have a Bash With This Linux Shell - page 2

Bashing About

  • February 19, 2004
  • By Carla Schroder

Directing command output into a file is very useful. Some operations generate a lot of output; saving it in a file lets you study it at leisure. This command creates a new file, or replaces the contents of an existing file:

ls -al ~ > homefiles.txt

This creates a new file, or appends to the end of an existing file:

ls -al ~ >> homefiles.txt
This is a fast way to append lines to an existing file:
cat >> homefiles.txt
testing, testing, new line

Hit CTRL + D to finish.

I like piping the output of make to a file, so I know what files were installed, and where. And to review any error messages. In this example, I'm installing JOE, Joe's Own Editor:

make install | tee joe-makeinstall.txt

The tee command is one of those odd little specialized Unix commands that come in handy at the strangest times. It both displays the command output on the screen, and stores it in a file. Use tee -a to append, instead of overwrite.

You doubtless noticed how most of the pieces of the above examples are not Bash features at all. The only Bash bits are:

>redirection operator
Redirection operators work both ways, for input and output. This takes the contents of file1 and directs it to the output file, file2:
cat < file1 > file2
This works great when you're feeding a list, or other data in a file, to a script:
scriptname < filelist > outputfile.txt

Most Popular LinuxPlanet Stories