Search code examples
unixunix-head

UNIX programming: Head command


so I'm working on this assignment and I am having trouble with the head command in Unix. Our assignment is to find all of the .txt files within a directory and display the first line of text from each one the output should look like this:

fargo.txt
//first line on the text file here
turtle.txt
//first line on the text file here

and so on

I have the code to find the .txt file in a directory and its sub-directories, but I have to use the head command to display the line of text and I'm lost. using "man head" I don't get very much information about how to use it.

Here's what I got show far

ls ~/Main/* | grep '\.txt'

and I have also tried

ls ~/Main/* | grep '\.txt' `head -n 1`

Any ideas on how to use the "head" command to get the first line of text of these files.


Solution

  • Hey guys I figured out the answer; this is what the assignment was looking for:

    find ~/Main -name '*.txt' -head -exec {} \;