Search code examples
linuxubuntusedhead

Print into file first two lines from all subfolders


I have the following Linux command:

head -2 * > output.txt

I want to run this command for all subfolders, and get the output into the same file.

Output example:

path_file1:
first two lines of file1

path_file2:
first two lines of file2

..
..
..

Is this possible as a Linux command? If so, how?


Solution

  • You can use the -exec flag on the find command to execute head -2.

    find . -type f -exec head -2 {} \; > output.txt