I have a unix command that does the following:
head -c 2048 > test.txtbasically its taking first 2kb of the test.txt file.
Can we do something similar in windows cmd prompt?
43 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 byteMore information here:
5From 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 lineIf neither of these work for you, you can alternatively install Cygwin and you can use cat or head.
To keep first 2048 bytes of test.txt:
FSUTIL file seteof test.txt 2048To keep a copy of test.txt, make a copy of it first.
Tested in Win 10