Unix find and head command help

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Unix find and head command help
# 1  
Old 04-22-2012
Unix find and head command help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
I've been stuck on this problem for 2 days now
What command would you enter to list the first lines of all text files within your UnixCourse directory or within any directory inside there?


2. Relevant commands, code, scripts, algorithms:
i know i'm supposed to used 'find' '-exec' and 'head'
the question also says:
But you really don't want to type that command out for every file or even every directory. What command would you give to produce a listing of the names of all the text files in ~/UnixCourse or its subdirectories (possibly nested several layers deep) followed immediately by the first line of text within that file? E.g., to produce a listing looking like this:

Code:
/home/yourname/UnixCourse/foo.txt
When in the course of human development
/home/yourname/UnixCourse/Quotations/bar.txt
Curiouser and curiouser!



Note that the output format must look like the example above. (That's not unreasonable, as one of the things you should have learned from this lesson is that the output of one command is often fed into later commands, which must be able to read it.) In particular, solutions that add extreneous characters around the file name (e.g., << or ==>) are not correct.


3. The attempts at a solution (include all code and scripts):
here's what i've tried so far:

Code:
find ~/UnixCourse -type file -exec head -1 {} \;
find ~/UnixCourse -name "*.txt" -print -exec head -n1 {} \;
find ~/UnixCourse.*.txt !-type d -print -exec head -n1 {} \;
find ~/UnixCourse. -name "*.txt" ! -type d head -n1 {} \
find ~/UnixCourse -name "*.txt" -print -exec head -n1 {}\;

none of those have worked and it doesn't really give me any feedback other than saying it's not the right answer. any help would be greatly appreciated as I am at my wits end.
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
odu, chesapeake, va, zeil, cs252


Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
Moderator's Comments:
Mod Comment Link: How to use code tags

Last edited by Scrutinizer; 04-22-2012 at 02:13 AM..
# 2  
Old 04-22-2012
Given this is homework, I'll only hint. First, I think you are on the right track with these:

Code:
find ~/UnixCourse -type file -exec head -1 {} \;
 find ~/UnixCourse -name "*.txt" -print -exec head -n1 {} \;

You want to find all regular files in the directory, print the filename to stdout, and display the first line. So your solution would be the combination of these two commands that you've experimented with that do just that.

The first command probably isn't listing the file name, and the second command isn't considering all files.
# 3  
Old 04-22-2012
This an interesting puzzle because I for one cannot come up with a simple solution which works on all modern unix and Linux O/S (the O/S was not stated) ... and which uses "find .... -exec".

In the commercial world we would use a construct which lets us do a sequence of commands within a do-done loop:
Code:
find /home/yourname/UnixCourse/ -type f -name "*.txt" -print | while read filename
do
     echo "${filename}"
     head -1 "${filename}"
done

I realise that this does not match you requirement because there is no "-exec" on the "find".

Ignoring those versions of the "find" command which support more than one command with "-exec", one must look at what "-exec" could run.
Hmm it could run a simple Shell script which displayed the filename and then displayed the first line of the file in question.
Just an idea, other posters may have better ideas.

Last edited by methyl; 04-22-2012 at 05:30 PM.. Reason: typos
# 4  
Old 04-22-2012
thanks for all the help. I'm still not quite getting it. what I've gathered is:
find ~/UnixCourse -name
is how the command should begin and it should end with:
-print -exec head -n1 {} \;
however, i'm not quite sure if the syntax is right on the above, or what goes in the middle.
# 5  
Old 04-22-2012
I don't think you want to use the -name option. It sounded, to me at least, that you want to find all files. Using -name would limit what the find command would output. I do think you want to use the -type f option so as to have find print only regular file names, and not directory names.

As was implied by methyl, what operating system and shell are you using?
# 6  
Old 04-22-2012
Amending my "find" to include ~UnixCourse:

find ~UnixCourse -type f -name "*.txt" -print

The parameters read as follows:

Start directory: ~UnixCourse
File type (a normal file) : -type f
File name: "*.txt" : Any file where the name end in ".txt".


Footnote: This is unix not MSDOS, and file extensions have no special meaning.


@agama
From the original post: list the first lines of all text files. Though I know that unix does not define file types by file extension, in this example the O/P has posted -name "*.txt in one example ... and I have copied it!


I can't vouch for the -exec command posted, but do try it on your system and see what happens.

Last edited by methyl; 04-22-2012 at 06:17 PM.. Reason: layout
This User Gave Thanks to methyl For This Post:
# 7  
Old 04-22-2012
Quote:
Originally Posted by methyl
@agama
From the original post: list the first lines of all text files. Though I know that unix does not define file types by file extension, in this example the O/P has posted -name "*.txt in one example ... and I have copied it!
Some how I managed to get "all" stuck in my head, even though both examples and attempts referenced "*.txt". Good call, and I just need to read things a bit more closely. Thanks.
This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Head command queries

we have a file as below AREA,COUNTRY,RANK A,MX,1 A,MX,2 A,MX,5 A,MX,8 A,IN,7 A,IN,5 A,IN,21 B,CN,6 B,CN,2 B,CN,8 B,CN,0 we need the TOP 2 RANK records for the combination of Area, Country as below. i know head -2, which gives top 2 records from file but not sure it lists based on... (7 Replies)
Discussion started by: JSKOBS
7 Replies

2. Homework & Coursework Questions

UNIX head command not working?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a script that allows the user to print the first n lines or the last n lines of every file in the... (18 Replies)
Discussion started by: lukefrost96
18 Replies

3. HP-UX

How to find the head cleaning history on HP HP MSL4048 1 LT0-4 Ultrium 1840?

Please guide me how to get the head cleaning history on HP HP MSL4048 1 LT0-4 Ultrium 1840. (0 Replies)
Discussion started by: marunmeera
0 Replies

4. Shell Programming and Scripting

Diff/head - not sure if this is the right command to use

Hi, I need some advise on whether there is a better way of doing what I am currently planning to do. Perhaps I should be using arrays instead of re-directing output to files? I need to use a tool/program named ADRCI provided by Oracle to remove trace files that it generates. Honestly it is... (1 Reply)
Discussion started by: newbie_01
1 Replies

5. UNIX for Dummies Questions & Answers

find and head -1

i have lots of files in /law/prod and /law/dev, such as AP20PD, AP20WS, AP20.scr, AP20.rpt if i am in /law DIR find . -name AP20PD, found in /law/prod and /law/dev i want to head -1 AP20PD from both location and >> /tmp/test.log can i use find and head in one line ? ----------... (1 Reply)
Discussion started by: tjmannonline
1 Replies

6. Shell Programming and Scripting

head command with more than one file

Hi, I have the following problem. I have files with one column of data (let's say file1.dat, file2.dat...file6.dat), and I would like to record the first value of the column of each file into another file (let's name it fileall.dat), which would have the the six values, one in each column. I use to... (4 Replies)
Discussion started by: josegr
4 Replies

7. UNIX for Dummies Questions & Answers

alternative for head command

Hi friends,I am new to unix and this is really a dummy question.but please help me out. How to simulate head command without using head command??? also tail command too,also more command. it is given as a homework to do....please tell me how to do (2 Replies)
Discussion started by: nikhilneela
2 Replies

8. Shell Programming and Scripting

Little bit weired : Find files in UNIX w/o using find or where command

Yes , I have to find a file in unix without using any find or where commands.Any pointers for the same would be very helpful as i am beginner in shell scritping and need a solution for the same. Thanks in advance. Regards Jatin Jain (10 Replies)
Discussion started by: jatin.jain
10 Replies

9. Shell Programming and Scripting

head command

Hi All, How can the head command be used to extract only a particular line. By default head -n filename displays the first n lines. I want only the nth line. I couldn't get it from forum search. Thanks, Sumesh (6 Replies)
Discussion started by: sumesh.abraham
6 Replies
Login or Register to Ask a Question