LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-23-2005, 06:06 AM   #1
seriesx4
Member
 
Registered: Mar 2005
Location: Sweden
Posts: 64

Rep: Reputation: 15
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.
 
Old 10-23-2005, 09:11 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
 
Old 10-23-2005, 09:43 AM   #3
imitheos
Member
 
Registered: May 2005
Location: Greece
Posts: 441

Rep: Reputation: 141Reputation: 141
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Swap file & Boot Partition Resize surfer2k3 Linux - Newbie 4 07-17-2008 07:50 PM
Resize a file with a size of 0 smaudlin Linux - General 2 11-12-2005 12:33 AM
how to resize mepis david waldo MEPIS 1 06-04-2005 01:12 AM
Resize / ? Sugarat Solaris / OpenSolaris 4 12-06-2003 12:10 PM
resize a loopback file verigoth Linux - General 1 10-30-2002 09:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration