Tar: Don't touch existing files

Written by Benjamin Cane on 2011-07-14

Tar is a handy dandy command, Its function is to create and extract archive files known as tarfiles. While tar can be used in various situations, one situation is when you need to move files from one server to another while maintaining permissions. Tar will retain the permissions and allow you to transfer 1 big file rather than many files.

In this situation there could be a time where you don't want tar to overwrite files that already exist. Well tar can take care of that as well by adding the -k flag.


[[email protected] play]$ echo 1 > 1.txt   
[[email protected] play]$ tar -xzf files.tgz 1.txt  
[[email protected] play]$ cat 1.txt   
[[email protected] play]$ echo 1 > 1.txt   
[[email protected] play]$ tar -xzf files.tgz 1.txt -k  
[[email protected] play]$ cat 1.txt   

Also note the placement of the -k matters, if it is mixed with the -xzf it will not work.

Picture of Benjamin Cane

Benjamin's specialty is keeping the lights on for mission critical systems. He is currently building applications that enable high concurrency financial transactions.

Recently Benjamin published his first book; Red Hat Enterprise Linux Troubleshooting Guide. In addition to writing, he has several Open Source projects focused on making Ops easier. These projects include Automatron, a project enabling auto-healing infrastructure for the masses.


Identify, capture and resolve common issues faced by Red Hat Enterprise Linux administrators using best practices and advanced troubleshooting techniques

What people are saying:
Excellent, excellent resource for practical guidance on how to troubleshoot a wide variety of problems on Red Hat Linux. I particularly enjoyed how the author made sure to provide solid background and practical examples. I have a lot of experience on Red Hat but still came away with some great practical tools to add to my toolkit. - Amazon Review