UNIX programming: Head command

396 views Asked by At

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.

4

There are 4 answers

1
AudioBubble On BEST ANSWER

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

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

You almost got it right. It's the ls that needs to be enclosed in backticks so that its results are fed as filename arguments to head. To get the first line of all the text files in the current directory you would use:

head -n 1 `ls -Q *.txt`

To get the files in subdirectories, you could use ls and grep like you have (just make ls recursive, and make grep search for .txt in the end of each line), but I suggest you take a look at the find command.

Edit: As mentioned by @jonathanleffler and @unhammer, it is unnecessary to use ls if the files are in one directory. Also, it is a bad idea to use ls in command substitution without the -Q option, because it breaks filenames with spaces in them, but option -Q is not available in most implementations of ls. If you need to search in subdirectories, use find (or **/*.txt if your shell supports it, as suggested by Jonathan).

Edit 2: Since the OP already got pretty close with the find command, I'll add my solution here:

find . -name "*.txt" -print -exec head -n 1 "{}" \;
1
Serge Ballesta On

In case you have files with spaces in their name, the following should resist :

for file in *.txt
do 
    echo "$i" 
    head -1 "$i"
done
0
unhammer On

I know I'm probably finishing someones homework here, but please don't do

head -n 1 `ls *.txt`

– it will fail if there are files containing spaces. And you can't quote it, because then it treats all the output as one file.

The correct way to show the first line of all the[1] .txt files in the current directory is:

head -n 1 *.txt

(for some other folder, head -n 1 /other/folder/*.txt). This will work with all kinds of strange file names because the shell itself correctly sends in the right file names to the head command.

And head is smart enough to tell that you want to see the file name in the output when you pass it several files.


[1] Well, actually it's head -n 1 *.txt .*.txt because plain *.txt doesn't include files that start with a dot.