LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-10-2016, 12:35 PM   #1
Miles Anthes
LQ Newbie
 
Registered: Feb 2016
Posts: 1

Rep: Reputation: Disabled
re-direction into a file


Under MS-DOS, when requesting a directory callout (dir) [ls] the answer can be redirected from the screen into a file (dir a > file)and the resulting ls output will be in the file on the hard drive (or disk). What is the manner to do this in Linux?
 
Old 03-10-2016, 12:38 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
It's the same
Code:
ls a > file
 
Old 03-10-2016, 12:58 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by suicidaleggroll View Post
It's the same
Code:
ls a > file
Yes.

And I always have to look this up, but my $0.02 are that you also can get stderr piped to that file by changing it to:
Code:
ls a &> file
Now someone chime in and tell me that I looked it up wrong
 
Old 03-10-2016, 01:01 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Yes that'll work. It's simply a shortcut for
Code:
ls a > file 2>&1
which redirects stdout to file and redirects stderr to stdout
 
Old 03-10-2016, 01:04 PM   #5
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by rtmistler View Post
You also can get stderr piped to that file by changing it to:
Code:
ls a &> file
As far as I am aware, that's not a portable solution (it's not in POSIX), but is accepted by many shells.

ls a > file - stdout (create new file)

ls a 1> file - stdout (create new file)

ls a >> file - stdout (append to existing file)

ls a 2> file - stderr

ls a >file 2>&1 - both stdout and stderr (as suicidaleggroll correctly points out)

If you want to silence either stream, redirect them to /dev/null

Last edited by hydrurga; 03-10-2016 at 01:13 PM. Reason: Added dual redirection for completeness
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Direction 2armz Linux - Newbie 2 09-08-2012 09:42 AM
Need some direction twistedpair Linux - Server 2 06-20-2010 02:36 PM
A little direction please heideL Programming 5 11-17-2008 03:03 AM
Need Direction Billn Linux - Newbie 1 09-11-2007 09:50 AM
Can someone point me in the right direction? Desai Linux - Networking 4 09-22-2004 08:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:42 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
Open Source Consulting | Domain Registration