Skip to Main Content
Ubuntu: Up and Running
book

Ubuntu: Up and Running

by Robin Nixon
April 2010
Beginner content levelBeginner
464 pages
12h 7m
English
O'Reilly Media, Inc.
Content preview from Ubuntu: Up and Running

Using Redirection

In addition to supporting pipes, Ubuntu allows you to redirect output using the > and < symbols. Using the first, you can, for example, send the output from a program directly to a file, whereas the second accepts input from a program.

Redirecting Output

The following command creates a file called files.txt in your home folder containing the output from ls -al:

ls -al > ~/files.txt

If files.txt already exists, it will be overwritten; otherwise, it will be created.

When you issue that command, you won’t see anything on the screen, because the output that would have been displayed has been redirected to a file. But you can verify that the command worked by entering the following, which displays the file’s contents:

cat ~/files.txt

The result of issuing this command will look something like Figure 7-15.

The result of displaying files.txt

Figure 7-15. The result of displaying files.txt

But what if you want to know which files and folders were created first? The answer would be to sort them by column 6, and you could use this command to do it:

ls -al | sort -k6 > ~/files.txt

Redirecting Input

If you need to keep the file sorted alphabetically but still wish to sometimes view the lines in date order, you can issue the following command on it instead:

sort -k6 < ~/files.txt

This opens up files.txt, reads it in, and passes its contents to the command immediately preceding the < symbol.

You could even extend that to use ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ubuntu Made Easy

Ubuntu Made Easy

Rickford Grant, Phil Bull
Ubuntu for Non-Geeks, 4th Edition

Ubuntu for Non-Geeks, 4th Edition

Rickford Grant, Phil Bull
CentOS Quick Start Guide

CentOS Quick Start Guide

Shiwang Kalkhanda

Publisher Resources

ISBN: 9781449382827Errata Page