LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Resize a file (https://www.linuxquestions.org/questions/linux-general-1/resize-a-file-375991/)

seriesx4 10-23-2005 06:06 AM

Resize a file
 
Is there a common command to crop a file?

For example: I have a 10 MB file, and I want to remove the first 2 MB and the last 2 MB thus creating a new file of 6 MB.

MensaWater 10-23-2005 09:11 AM

Combine the "head" and "tail" commands. Both have a "-c" options to specify number of bytes.

For your 10 MB example:

tail -c 8192 <filename>
Prints the last 8 MB (i.e. omit the FIRST 2 MB - 8192 bytes = 8 MB)

head -c 8192 <filename>
Prints the first 8 MB (i.e. omit the LAST 2 MB)

So to combine them in a pipeline you'd have to be cognizant of the difference (that is whichever command you did first would reduce the output to 8 MB so you'd need to change your byte count to be 8 MB - 2 MB = 6 MB:

tail -c 8192 <filename> |head -c 6144
The tail prints the last 8 MB and pipes it into head which prints only the first 6 MB of the 8 MB generated by tail.

Make sure you do the output to a test file before overwriting your original to be sure it contains what you want.

imitheos 10-23-2005 09:43 AM

You can also use dd to do it

dd if=filename of=new_filename bs=1024 skip=2048 count=8192

if=input filename
of=output filename
bs=1024 says that every read/write operation will copy 1024 bytes (1KB)
skip=2048 tells dd to skip 2048 reads (2048 * 1024 = 2MB) from the input filename
count=8192 tells dd to copy 8192 reads (8192 * 1024 = 8MB)

Try "man dd" to see the full options of dd

dd is a very powerful command and you can do many things with it, but you can get confused easily and do the wrong thing
so be careful


All times are GMT -5. The time now is 10:37 PM.