user3559780
user3559780

Reputation: 363

Unable to create tar file of files from specific folder . Getting wrong path after extraction

Here I want to create tar file of bin folder only . My main.c code has location i.e. /home/amit/practice/datafill/main.c. datafill directory contains folders like bin, conf, config, data,lib.

There is one link system("cd <path>") in a C program But it doesn't solve my problem . with the use of combining 2 commands with && operator it is creating bin.tar from bin folder only ,but when it is extracted, it is containing full path link /home/amit/practice/datafill/bin/ which is wrong .Suppose there is one file abc.txt inside bin folder . So after extraction ,abc.txt should have path as bin/abc.txt only, not /home/amit/practice/datafill/bin/abc.txt

#include<stdio.h>
#include<unistd.h>
int main(void)
{
char Cmd[256];
/*Go to specific directory && create tar file*/
sprintf(Cmd,"cd /home/amit/practice/datafill/bin/ && sudo tar -cvf bin.tar *");
system(Cmd);
return 0;
}

Upvotes: 0

Views: 153

Answers (1)

Joshua
Joshua

Reputation: 43317

Your code creates the tar file in the bin directory, but it appears you checked an older attempt in the current directory.

This code is strange and unnatural. I would normally do something like this:

 `cd /home/amit/practice/datafill/bin/ && sudo tar -cvf - *`

which causes the tarball is written to the program's standard output. If you want to generate it to a specific path, this would be more reasonable

 `sudo sh -c '(cd /home/amit/practice/datafill/bin/ && tar -cvf - *) > bin.tar'`

Upvotes: 1

Related Questions