Saving Output to Other Files

Problem

You want to save the output with a redirect to elsewhere in the filesystem, not in the current directory.

Solution

Use more of a pathname when you redirect the output.

$ echo some more data > /tmp/echo.out

or:

$ echo some more data > ../../over.here

Discussion

The filename that appears after the redirection character (the >) is actually a path-name. If it begins with no other qualifiers, the file will be placed in the current directory.

If that filename begins with a slash (/) then this is an absolute pathname, and will be placed where it specifies in the filesystem hierarchy (i.e., tree) beginning at the root (provided all the intermediary directories exist and have permissions that allow you to traverse them). We used /tmp since it is a well-known, universally available scratch directory on virtually all Unix systems. The shell, in this example, will create the file named echo.out in the /tmp directory.

Our second example, placing the output into ../../over.here, uses a relative path-name, and the .. is the specially-named directory inside every directory that refers to the parent directory. So each reference to .. moves up a level in the filesystem tree (toward the root, not what we usually mean by up in a tree). The point here is that we can redirect our output, if we want, into a file that is far away from where we are running the command.

See Also

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.