Windows Equivalent of the 'head -c' command

I have a unix command that does the following:

head -c 2048 > test.txt

basically its taking first 2kb of the test.txt file.

Can we do something similar in windows cmd prompt?

4

3 Answers

Simplifying this answer because of @chubbsondubs' comment.

-TotalCount will count lines if reading in text, so always force it to read the file as bytes, then the -TotalCount will only refer to bytes and you can get an account count.

Get-Content test.txt -Encoding byte -TotalCount 2KB | Set-Content test1.txt -Encoding byte

More information here:

5

From what I can tell you can't print out by size in a native way; there is the type command which will output a whole text file, but you can't specify how much you want to output.

There is also the more command, which will allow you to print out lines of a file. These are some of the flags from more /?:

/E Enable extended features
/C Clear screen before displaying page
/P Expand FormFeed characters
/S Squeeze multiple blank lines into a single line
/Tn Expand tabs to n spaces (default 8) Switches can be present in the MORE environment variable.
+n Start displaying the first file at line n
files List of files to be displayed. Files in the list are separated by blanks.
If extended features are enabled, the following commands
are accepted at the -- More -- prompt:
P n Display next n lines
S n Skip next n lines
F Display next file
Q Quit
= Show line number
? Show help line
<space> Display next page
<ret> Display next line

If neither of these work for you, you can alternatively install Cygwin and you can use cat or head.

4

To keep first 2048 bytes of test.txt:

FSUTIL file seteof test.txt 2048

To keep a copy of test.txt, make a copy of it first.

Tested in Win 10

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like