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?
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: https://stackoverflow.com/questions/888063/powershell-to-get-the-first-x-mb-of-a-file
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
.
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