12

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?

  • 1
    The -c switch in head assumes 1 byte = 1 character. – Thomas N Feb 20 '17 at 18:20
  • 1
    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
  • 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. – bballdave025 Jan 9 at 16:11
19

Complete command would be:

head -c20 yourFile.txt
| improve this answer | |
2

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.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.