LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 04-24-2016, 03:52 PM   #1
PleaseHelpMeUnderstand
Member
 
Registered: Apr 2016
Posts: 32

Rep: Reputation: Disabled
Redirection and piping


I want to write a command (one command) that would take a long listing of the /etc directory, but select only the last 5 entries (last 5 lines) in the directory AND save the results to a file named: etclisting in my current directory.

Would my command ls -l /etc | tail -5 >> etclisting be sufficient for this?
 
Old 04-24-2016, 04:38 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Jessie, Linux Mint 17
Posts: 7,751

Rep: Reputation: 272Reputation: 272Reputation: 272
Yes, that will work.

-------------------------
Steve Stites
 
Old 04-24-2016, 04:53 PM   #3
bleb
LQ Newbie
 
Registered: Apr 2016
Posts: 2

Rep: Reputation: Disabled
Note that >> is for appending to a file; > completely replaces the file if it exists. So if you run ls -l /etc | tail -5 >>etclisting twice, then you will have two copies of the same 5 entries in your file. On the other hand ls -l /etc | tail -f >etclisting is idempotent.
 
1 members found this post helpful.
Old 04-24-2016, 11:30 PM   #4
maybebaby
LQ Newbie
 
Registered: Apr 2016
Location: Virgin Islands
Posts: 23

Rep: Reputation: Disabled
Quote:
Originally Posted by jailbait View Post
Yes, that will work.

-------------------------
Steve Stites
Quote:
Originally Posted by bleb View Post
Note that >> is for appending to a file; > completely replaces the file if it exists. So if you run ls -l /etc | tail -5 >>etclisting twice, then you will have two copies of the same 5 entries in your file. On the other hand ls -l /etc | tail -f >etclisting is idempotent.
so in this scenario which is better to use? > or >> and what verbiage can you pinpoint to tell you which you're supposed to be using, also?

Last edited by maybebaby; 04-24-2016 at 11:32 PM.
 
Old 04-25-2016, 03:47 AM   #5
bleb
LQ Newbie
 
Registered: Apr 2016
Posts: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by maybebaby View Post
so in this scenario which is better to use? > or >> and what verbiage can you pinpoint to tell you which you're supposed to be using, also?
I don't really know what OP is trying to accomplish so it's hard to say. You just have to decide which to use in each case based on whether you want it to overwrite an existing file or append to it. If you know the file you're redirecting to is a new one, the behavior is the same, but people generally use >. As for verbiage the book I like to recommend for this sort of stuff is The Unix Programming Environment by Kernighan and Pike.
 
Old 04-25-2016, 09:28 AM   #6
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,182
Blog Entries: 10

Rep: Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980
http://www.tldp.org/LDP/abs/html/io-redirection.html
is a good reference and is applicable to all 5 of your threads on this same subject.

Last edited by Habitual; 04-25-2016 at 10:11 AM.
 
1 members found this post helpful.
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux piping vs Windows piping emanabela Linux - Newbie 1 05-10-2009 01:58 PM
piping | jezz Linux - General 2 05-04-2008 05:28 AM
Piping ls Kcghost Linux - General 9 03-05-2008 08:24 PM
Piping... disruptive Programming 3 02-12-2008 02:09 PM
Piping + redirection teddyZA Linux - General 6 04-10-2007 02:43 PM


All times are GMT -5. The time now is 02:21 AM.

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