I have a folder with quite a few files in it. I want to display the following:
filename_1
first line of file1
second line of file1
third line of file1
filename_2
first line of file2
second line of file2
third line of file2
filename_3
first line of file3
second line of file3
third line of file3
etc. How can I do that?
You use the
head
command to do this, with the -n argument with the number of lines from each file, like this:or
This also works for a single file:
head
You use
head
with the-n
option.head -n 10 FILE
This will print the first ten lines of a file.
Another useful variation would be
-n -NUMBER
.head -n -10 FILE
This will print all but the last ten lines of a file.
To solve your problem and get your desired output you can do the following.
or
This will get you following output:
This will do what you want, hopefuly:
-print
will show the filename and the rest (from -exec
) will show the first 3 lines of each fileChange the number according to your needs...
To add the filename to the head output use 'head -v'. So 'head -vn 3 *.html' gives me: