Tony
Tony

Reputation: 2929

How I use the head command for the files in current directory and subdirectories?

If I use head *.txt | less, it will display the head of all files with *.txt in the current directory? What if I wish to display the head of all files with .txt residing in current directory and subdirectories.

Thanks for your help

Upvotes: 3

Views: 5809

Answers (4)

TroublesomeQuarry
TroublesomeQuarry

Reputation: 165

I was trying to do the same thing; here's what worked for me (I output the response to the file headtest.txt). Note that this will move through the current directory and the subdirectories.

find . -type f -exec head '{}' \; >> headtest.txt

Hope this helps.

Upvotes: 1

Jakob Borg
Jakob Borg

Reputation: 24435

If your shell supports it (zsh does, not sure about the rest), you can use ** syntax:

head **/*.txt

Upvotes: 1

Raj
Raj

Reputation: 43

Another option is to use -

find -name '*.txt' | xargs head

Please note that -name need not work in all environments In that case, you can use

find . | grep ".txt" | xargs head

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318508

You could use find:

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

Upvotes: 4

Related Questions