This question already has an answer here:

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?

marked as duplicate by don_crissti, GAD3R, Archemar, Jeff Schaller, G-Man Feb 21 '17 at 5:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    The -c switch in head assumes 1 byte = 1 character. – Thomas N Feb 20 '17 at 18:20
  • Note that the -c option is a non-portable extension. – kdhp Feb 20 '17 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 '18 at 11:10

Complete command would be:

head -c20 yourFile.txt

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 or ask your own question.