I created directory test, created file 1.txt in test, wrote 'Before' in this file.Then I went cd ..and used the command:tar -cvzf ./test.tgz ./testThen I entered the test dir again. Opened the 1.txt file again. Changed content to After. I saved the file and changed chmod to read only by executing:chmod -w ./1.txtSo for now my 1.txt is read only. Then I go upcd ..and extract the test.tgz archive.tar -xvzf ./test.tgzThen I go again to test dir, docat 1.txtand get Before.It is not logical that this happens, since the file was set to be read-only.Why does it happen?
Tar overwrites read only files
permissions;files;tar;readonly
Tar didn't overwrite the existing read-only file, it removed it and then created a new file with the same name.  This is a consequence of the way -x works; it replaces existing versions of a file by design in order to accommodate the old incremental backup method of appending files to an existing archive.  A tar archive might have multiple versions of a file in it; only the last one will appear on disk after extraction is completed.  A side effect of this is that tar also removes existing copies of files even if they appear only once in the archive.