pbreault pbreault - 5 months ago 13
Linux Question

Linux command (like cat) to read a specified quantity of characters

Is there a command like

cat
in linux which can return a specified quantity of characters from a file?

e.g., I have a text file like:

Hello world
this is the second line
this is the third line


And I want something that would return the first 5 characters, which would be "hello".

thanks

Dan Dan
Answer

head works too:

head -c 100 file  # returns the first 100 bytes in the file

..will extract the first 100 bytes and return them.

What's nice about using head for this is that the syntax for tail matches:

tail -c 100 file  # returns the last 100 bytes in the file