how to cut an existing file into pieces without space
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
how to cut an existing file into pieces without space
To: linux legend
I have a big mpg {dvb tuner recording} 100GB. I have 1GB of free space. I tried to split the file, but I then need another 100+GB of space, and a lot of time/disk activity to achieve it.
I'm hoping there is a tool to split an on disk {ext3} file into two parts at a specific offset. This could:
- adjust the first file entry to offset length
- create a new file entry
- set the new file start position {sector?} to be offset +1
- fix up the disk links
My search so far tried:
linux split file {split, cut {doh}, but it needs double the file size space}
linux split mpg file {tools like mpgtx ffmpeg, able to take a frame position, but also need the space}.
If there is such a thing, or script, perhaps it would also be possible to re-assemble two files into a single file {again without copying the bytes - just by adjusting the file pointers} ?
how about dd ?
There's a long discussion on here about that tool.
I have used dd a fair bit. Can you give me a hint on parameters I could use to achieve my problem ?
I {if I had 50GB of free disk space} could use dd to copy either the first half or the second half of the file to a new file on disk. At this point the disk would be full, so I wouldn't be able to use dd to copy out the other half of the file. In fact I only have 1GB free.
If it was a fat file system, and the file was continuous, it would be possible to create a new fat table entry, point it to the position in the on disk file that I need to be the second file start position. Then adjust the first files length to truncate the fat's view of the file. Is this possible on ext3 ?
Are there tools available to do it ?
will get portion beyond 100MB of original.mov into original.mov.part2
Then the second portion (shrink) the original file, I don't know about an existing command for truncating a file, but suppose you have it, you could then execute
Now we *do* have the unistd-provided function truncate.... so if we write the truncate program ourself,....
it requires make and gcc (probably also libc6-dev)
Code:
cat >truncate.c <<EOFTRUNCATECODE
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
void printUsage(void) {
printf("usage:\n");
printf("truncate <file> <size_in_byte>\n\n");
printf("Will *reduce* size of <file> to <size_in_byte>.\n\n");
printf("exitcodes:\n");
printf("\t0\tSUCCESS (<file> has now size exactly <size_in_byte>.\n");
printf("\t1\tinvalid arguments.\n");
printf("\t2\t<file> unexisting or too small.\n");
printf("\t3\tACCESS failure (cannot read or write <file>).\n");
}
int main(const int argc,
const char* const* const argv) {
int rv;
if (3 != argc) {
fprintf(stderr,"ERROR: Invalid number of arguments (%d).\n",argc);
printUsage();
rv = 1;
} else {
const char* const fn = argv[1];
int base = 10;
char* endpt;
long int truncsize;
truncsize=strtol(argv[2],&endpt,base);
if ((NULL == endpt) || (0 != *endpt) || (0 < strlen(endpt))) {
fprintf(stderr,"ERROR: (%s) invalid syntax.\n",argv[2]);
printUsage();
rv = 1;
} else {
FILE* f;
f = fopen(fn,"r");
if (!f) {
fprintf(stderr,"Could not open %s.\n",fn);
printUsage();
rv = 3;
} else {
long int size;
fseek(f,0L,SEEK_END);
size = ftell(f);
fclose(f);
if (size < truncsize) {
fprintf(stderr,"size of %s (%ld byte) is too small to truncate.\n",fn,size);
rv = 2;
} else if (0 != truncate(fn,(off_t)truncsize)) {
int errval;
errval=errno;
fprintf(stderr,"error truncating %s %d [%s].\n",fn,errval,strerror(errval));
rv = 2;
} else {
printf("file %s truncated successfully to %ld byte.\n",fn,truncsize);
rv = 0;
}
}
}
}
return rv;
}
EOFTRUNCATECODE
make truncate;
Searching for the word truncate has also been helpful: http://www.pixelbeat.org/scripts/truncate
is a short script using dd to trim a file, so it seems it can be done with a pair of dd commands after all as long as I have disk space exceeding the part that I want to split of the end of the file {I do not have that sort of space available}.
Not being that familiar with shell scripts, what is the 2>&1 and >&2 achieve, and also the "^0" ?
Code:
>&2
will output the default output of the execution into the standard error output pipe of the process.
Code:
grep -v "^0"
will look in it's input for all lines that do NOT start with "0"
why ??? or how ???
Code:
process >destination
and
Code:
process >>destination
will put default output of prcess execution into file destination. the former (>) will create a file (or overwrite an existing one), the latter (>>) will create a non existing one, or append to an existing one.
Code:
&2
and
Code:
&1
as destination in aformentioned code, will direct the output concerned into standard output (for &1) and into standard ERROR output pipe (for &2).
so the indication
Code:
process 2>&1
will output the standard error output (2) into the default output (&1)
if you do
Code:
grep -v searchstring
will return all results NOT matching the searchstring (-v stands for revert the condition).
Code:
grep "^0"
this will output all lines BEGINNING (special significance of ^ character in regular expressions) with "0"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.