LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-13-2014, 07:16 AM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Print first "n" lines of all files within a directory


Having identified a directory, it is desired to print the first 4 lines of every file within that directory.
The chosen directory contains many files and a few sub-directories.
find works recursively, and that's desirable.

This code ...
Code:
DoI='/home/daniel/Desktop/LQfiles'  # DoI=Directory of Interest
find $DoI        \
|xargs  head -4  \
>$Work2
... produces the desired result but also flashes error messages to the screen, such as ...
Code:
head: error reading `/home/daniel/Desktop/LQfiles': Is a directory
head: error reading `/home/daniel/Desktop/LQfiles/LQ8': Is a directory
head: error reading `/home/daniel/Desktop/LQfiles/LQ6': Is a directory
head: error reading `/home/daniel/Desktop/LQfiles/LQ7': Is a directory
How may these undesirable messages be eliminated?

Is there an option for find which should be used?
(I looked but did not find one.)
Is there a way to redirect the error messages to a different file?
(I tried to do that but botched it.)

Please advise.

Daniel B. Martin
 
Old 10-13-2014, 07:59 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
i think you can append
Code:
2 > /dev/null
to the command to mask the problem.

maybe try exec within find:
Code:
find $DoI -type f -exec head -n 4 '{}' \;

Last edited by schneidz; 10-13-2014 at 08:01 AM.
 
Old 10-13-2014, 09:02 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Thank you, schneidz, for the suggestions.

This code ...
Code:
find $DoI |xargs head -4 >$Work2 2 > /dev/null
... produced the same on-screen error messages.

This code ...
Code:
find $DoI -type f -exec head -n 4 '{}' >$Work3
... produced this error message ...
Code:
find: missing argument to `-exec'
I'm running Ubuntu 10.04, a back level. Could this be a problem?

Daniel B. Martin
 
Old 10-13-2014, 09:05 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
i doubt version would be a problem.

sorry, there should be no space between 2 and >:
Code:
2> /dev/null
for the find command you copy-pastad wrong:
Code:
find $DoI -type f -exec head -n 4 '{}' \;

Last edited by schneidz; 10-13-2014 at 09:08 AM.
 
1 members found this post helpful.
Old 10-13-2014, 09:11 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Success! Thank you, schneidz, for the guidance.

This code ...
Code:
find $DoI |xargs head -4 >$Work2 2> /dev/null
... runs cleanly.

This code ...
Code:
find $DoI |xargs head -4 >$Work2 2> $Errors
... also runs cleanly and preserves the error messages for reference.

SOLVED!

Daniel B. Martin
 
Old 10-13-2014, 09:14 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
this is ideal since it explicitly tells find to limit its searching to type files (and ignore directories so that head doesnt try to print them (which doesnt make much sense anyways)):

Code:
find $DoI -type f -exec head -n 4 '{}' \;
the previous code will mask any problems making debugging difficult
 
  


Reply

Tags
errors, find, redirect



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
[SOLVED] Delete lines from a text file not starting with "http://" or "https://" georgi Programming 4 10-04-2013 03:00 AM
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
[SOLVED] Perl: Condition?print "Is true":print "Is false"; angel115 Programming 2 09-17-2010 06:28 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
Sed/Awk: print lines between n'th and (n+1)'th match of "foo" xaverius Programming 17 08-20-2007 11:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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