Hari
Hari

Reputation: 397

Staying in another folder, how can i tar specific files from another directory?

Thanks for your support,

I have the following folder structure on my linux laptop

/home
     /A
     /B

In folder "B", I have files of type *.csv, *.dat.

Now from folder A, How can I create a tar file containing files *.csv in folder B. I am running the command in folder A

Here is the command, I have tried but its not working,

In /home/A folder, I am running the following command

tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 *.csv

and also tried with this,

tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 --wildcards *.csv

For both of the commands, I get the following error,

tar: *.csv: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

In the tar file, I dont want to include the whole folder structure and this is the reason, I am using option -C (capital)

Moreover, the following command works but it tars all *.csv and *.dat files.

tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 .

Upvotes: 0

Views: 690

Answers (2)

GrnMtnBuckeye
GrnMtnBuckeye

Reputation: 214

You can use the --exclude=PATTERN option:

tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 . --exclude=*.dat

Other "local file selection" options listed in the man page: http://linux.die.net/man/1/tar

Upvotes: 1

Mark Adler
Mark Adler

Reputation: 112209

You can edit the names in the tar command to remove the path. (Assuming that you have GNU tar.)

tar -cf /home/A/Sample1.tar --transform 's,.*/\([^/]*\),\1,' /home/B/ZSBSDP4/*.csv

Note that if you specify more source directories on the command, you could accidentally put more than one file with the same name in the tar file. Then when unpacking, the last one will overwrite those with the same name that precede it.

Upvotes: 2

Related Questions