I want to get the first 20 or so characters from a number of files.

I've seen examples using cut but they all seem to get the first 20 characters of each line in the file, while I only want the first characters in the file itself (ie. from the first line), nothing more.

Is there a simple way to do this?

  • 3
    The -c switch in head assumes 1 byte = 1 character.
    – Thomas N
    Feb 20, 2017 at 18:20
  • 1
    Note that the -c option is a non-portable extension.
    – kdhp
    Feb 20, 2017 at 18:22
  • head -c worked on cygwin & centos. head -c brilliant when you need glimpse the contents of say an xml file with no linefeeds
    – zzapper
    Apr 16, 2018 at 11:10
  • I do not think this should be marked a duplicate, as the OP did not ask that cat (or even cut) be the tool used. OP simply noted that they had seen examples using cut. Jan 9, 2020 at 16:11

2 Answers 2


Complete command would be:

head -c 20 yourFile.txt
  • 1
    This specifies 20 bytes. Not 20 characters. If you have to handle multibyte characters, use gawk '{ print substr($1, 1, 20); }' < yourFile.txt or python3 -c 'print(input()[:20])' < yourFile.txt for example.
    – ynn
    Mar 24, 2023 at 5:10

Didn't realize the -c option for head was non portable. You can use dd to output the first 20 bytes like so:

dd if=/path/to/infile of=/path/to/outfile  bs=20 count=1

You can omit the of=/path/to/outfile part if you want your result to stdout.

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