InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Saving Output from the ls Command in bash
Bash

Saving Output from the ls Command in bash

InfinityCoder January 12, 2017

You tried to save output from the ls command with a redirect, but when you look at the resulting file, the format is not what you expected.

Use the -C option on ls when you redirect the output. Here’s the ls command showing the contents of a directory:

1
2
3
$ ls
a.out cong.txt def.conf file.txt more.txt zebra.list
$

But when we save the output with the > to redirect it to a file, and then show the file contents, we get this:

1
2
3
4
5
6
7
8
9
$ ls > /tmp/save.out
$ cat /tmp/save.out
a.out
cong.txt
def.conf
file.txt
more.txt
zebra.list
$

This time we’ll use the -C option:

1
2
3
4
$ ls -C > /tmp/save.out
$ cat /tmp/save.out
a.out cong.txt def.conf file.txt more.txt zebra.list
$

Alternatively, if we use the -1 option on ls when we don’t redirect, then we get output like this:

1
2
3
4
5
6
7
8
9
$ ls -1
a.out
cong.txt
def.conf
file.txt
more.txt
save.out
zebra.list
$

Then the original attempt at redirection matches this output.

Just when you thought that you understood redirection and you tried it on a simple ls command, it didn’t quite work right. What’s going on here?
The shell’s redirection is meant to be transparent to all programs, so programs don’t need special code to make their output redirect-able.

The shell takes care of it when you use the > to send the output elsewhere.

But it turns out that code can be added to a program to figure out when its output is being redirected.

Then, the program can behave differently in those two cases—and that’s what ls is doing.
The authors of ls figured that if your output is going to the screen then you probably want columnar output (-C option), as screen real estate is limited.

But they assumed if you’re redirecting it to a file, then you’ll want one file per line (the minus one -1 option) since there are more interesting things you can do (i.e., other processing) that is easier if each filename is on a line by itself.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Seeing All Variable Values in bash
How can I see which variables have been exported and …

Seeing All Variable Values in bash

Adding the Current Directory to the $PATH in bash
Having to type ./script is tedious and you’d rather just …

Adding the Current Directory to the $PATH in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap