How can I shorten a file from the command line?

How can I shorten a file from the command line?

Tengo un archivo xml de 150 GB que me gustaría acortar (es decir, truncar) a aproximadamente 1 GB. ¿Existe algún comando simple (bash o similar) que pueda usar o tengo que seguir la ruta programática (editarlo en vi o emacs? ¿Es una pesadilla incluso en los grandes sistemas de hierro)?

(No estoy particularmente preocupado por la pérdida de información, quiero un archivo más corto para poder probar un software sobre esto y no esperar muchas horas por la respuesta, un archivo más corto me permitirá hacerlo).

Respuesta1

Suponiendo que desea truncar y extraer los primeros 1 GB del archivo de 150 GB:

Con head:

head -c 1G infile > outfile

Tenga en cuenta que el Gsufijo se puede reemplazar con GBpara alinear con 1000 en lugar de 1024.

O con dd:

dd if=infile of=outfile bs=1M count=1024

O como en la respuesta de Wumpus Q. Wumbley, ddse puede truncar en su lugar.

Respuesta2

Para truncar un archivo a 1 gigabyte, use el truncatecomando:

truncate -s 1G file.xml

The result of truncation will likely not be a valid XML file but I gather that you understand that.

Documentation for the GNU version of truncate is here and documentation for the BSD version is here

Respuesta3

Where possible, I'd use the truncate command as in John1024's answer. It's not a standard unix command, though, so you might some day find yourself unable to use it. In that case, dd can do an in-place truncation too.

dd's default behavior is to truncate the output file at the point where the copying ends, so you just give it a 0-length input file and tell it to start writing at the desired truncation point:

dd if=/dev/null of=filename bs=1048576 seek=1024

(This is not the same as the copy-and-truncate dd in multithr3at3d's answer.)

Note that I used 1048576 and 1024 because 1048576*1024 is the desired size. I avoided bs=1m because this is a "portability" answer, and classic dd only knows suffixes k, b, and w.

Respuesta4

You can use the split command.

split -C 1G <filename>

For more details take a look at this stackoverflow answer

información relacionada