Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent overwrite existing files when using tar command [duplicate]

Tags:

linux

bash

tar

I made a mistake that forgot to assign argument of the file name when using tar command like below:

[john@foobar foo]$ ll
total 0
-rw-rw-r-- 1 john john 0  7月  4 19:20 2018 file1
-rw-rw-r-- 1 john john 0  7月  4 19:20 2018 file2
-rw-rw-r-- 1 john john 0  7月  4 19:20 2018 file3
[john@foobar foo]$ tar -cvzf file1 file2 file3
file2
file3
[john@foobar foo]$ ll
total 4
-rw-rw-r-- 1 john john 130  7月  4 19:21 2018 file1
-rw-rw-r-- 1 john john   0  7月  4 19:20 2018 file2
-rw-rw-r-- 1 john john   0  7月  4 19:20 2018 file3

When forget to assign archive file name, tar overwrites and creates the archive file1.

I checked man tar, but it seems there is no option such as cp shows a prompt when same name file already exists.

To create a foolproof script is a possible way?

like image 878
Rohan Kishibe Avatar asked Mar 22 '26 01:03

Rohan Kishibe


1 Answers

From man tar:

-k, --keep-old-files
       don’t replace existing files when extracting, treat them as errors

--skip-old-files
       don’t replace existing files when extracting, silently skip over them
like image 182
Poshi Avatar answered Mar 23 '26 17:03

Poshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!