What is an easy way I can compare the first 20 lines (or n lines) of two files?

I had set up an automated pg_dump, but it turns out the dumps being created are corrupt and now won't restore.

I still have a good dump file from a year ago, and I want to compare the first 20 lines between the two files.

What's an easy way of doing this?

I'm on Manjaro Linux.

1 Answer 1


Using a shell with process substitutions (<(...)), e.g. bash or zsh:

diff <( head -n 20 file1 ) <( head -n 20 file2 )

This run head -n 20 on each file to get the first 20 lines of each, in two separate process substitutions. Each process substitution will be expanded to the pathname of a file where the output of the command within may be read from (these files are temporary and are removed later).

The diff utility is then called to compare these two sets of data.

Without a process substitution:

head -n 20 file1 >file1.short
head -n 20 file2 | diff file1.short -
rm -f file1.short

This creates a separate file from the 20 first lines of one file, and uses that with diff while the 20 first lines of the other files are read from standard input.

You may want to use -c or -u or some other option with diff in the commands above to get the diff format of your choice (see the diff manual).

If the files are compressed, then you will have to uncompress the data:

diff <( gzip -d -c <file1 | head -n 20 ) <( gzip -d -c <file2 | head -n 20 )

or, without process substitutions:

gzip -d -c <file1 | head -n 20 >file1.short
gzip -d -c <file2 | head -n 20 | diff file1.short -
rm -f file1.short
  • Thanks for the quick reply @Kusalananda, does diff work with binary files? all I get is: Binary files /proc/self/fd/11 and /proc/self/fd/12 differ
    – J86
    Mar 13, 2021 at 11:11
  • @J86 Are your files not database dumps in text format? Are they possibly compressed?
    – Kusalananda
    Mar 13, 2021 at 11:12
  • No, they're definitely not in text format, they were created using pg_dump -Fc
    – J86
    Mar 13, 2021 at 11:13
  • Thanks, would this only work if the files are compressed? Cause right now I'm getting gzip: stdin: not in gzip format
    – J86
    Mar 13, 2021 at 11:44
  • 3
    @J86 Yes, the last part of my answer would only work if the files are compressed using a gzip compression. If you can't get it to work with my code, then you will have to be a whole lot more explicit about what you file are, what format they are on, how they were created, etc., because nobody would be able to work with totally unknown files that they can't see.
    – Kusalananda
    Mar 13, 2021 at 11:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .