Tar: Don't touch existing files

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.

Benjamin Cane
Principal Engineer, Vice President

Benjamin Cane is Principal Engineer at American Express. He has more than 16 years of experience with roles in both systems and software engineering. He leverages both his systems and software skills to build end-to-end platforms. Platforms, purpose built for performance and resiliency. Benjamin is also the author of Red Hat Enterprise Linux - Troubleshooting Guide (2015, Packt Publishing), and he has published many popular articles on topics such as Linux, Docker, Python, Go and Performance Tuning. Thoughts and Opinions expressed in my articles are my own.