LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-30-2009, 10:22 PM   #1
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 91
redirection of "ls" command to file gives chaotic content


When I want to save the output of the "ls" command I do
this:
ls > file.txt
but when I try to read the resulting file with "less" it
says
"file.txt" may be a binary file: See it anyway?
and if I reply with yes the file contains ESC[0m or some
other similar garbage at the beginning and end of every
line. I have searched using Google and have not figured
out how to work around this weirdness. I shelled into
another computer running SUSE 11 and it did the same thing.
I have used the ls command like this many times in the past
and never ran into this until lately. Can somebody point
me in the right direction to fix this? Thanks.
 
Old 07-30-2009, 10:51 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986
Your ls command probably has "--color=always" aliased to it. That's what the esc sequences are--ascii color codes.

If you set the alias to "--color=auto" it won't send the codes through pipes or redirects, but then you'll also lose color when you pipe it into less or such.

You can bypass the alias and use the original ls command directly by either backslashing the command "\ls" or enclosing it in quotes.

Edit: Oh, and it looks like less is complaining because you don't have color code support enabled for it either. Alias less to "less -R" and it will interpret and display your piped results in color (assuming the input program is sending the codes).

Last edited by David the H.; 07-30-2009 at 11:03 PM.
 
Old 07-30-2009, 11:23 PM   #3
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Original Poster
Rep: Reputation: 91
Quote:
Originally Posted by David the H. View Post
Your ls command probably has "--color=always" aliased to it. That's what the esc sequences are--ascii color codes.

If you set the alias to "--color=auto" it won't send the codes through pipes or redirects, but then you'll also lose color when you pipe it into less or such.

You can bypass the alias and use the original ls command directly by either backslashing the command "\ls" or enclosing it in quotes.

Edit: Oh, and it looks like less is complaining because you don't have color code support enabled for it either. Alias less to "less -R" and it will interpret and display your piped results in color (assuming the input program is sending the codes).
Wow, thanks. I did "alias ls=ls" and it works fine. Your
information is VERY helpful, just can't thank you enough.
 
Old 07-31-2009, 03:13 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986
I suspected that would be the problem as soon as I saw the title of your question.

Incidentally you can also use "unalias ls" to unset it for that terminal. Of course it will only be temporary unless you remove or alter the line in your bash startup scripts that sets it in the first place. But I generally find it easier to just backslash-escape the command when I need to than to alter or unset it and have to reset it again later.


P.S: Another major program that has optional color support is grep.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
Command "mail" returns "panic: temporary file seek" kenneho Linux - Software 5 12-23-2008 03:27 AM
squirrelmail "error in login page"" when content advisor is enabled simi_virgo Linux - Networking 4 08-21-2008 01:25 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
bash redirection "$ cat << EOF > file" (how does this work) ninmonkeys Linux - General 1 11-09-2004 03:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration