J
Jarret W. Buse
Guest
Applications 28 – Uncommon Options of tar Part 4
This article continues the uncommon options of the tar command.
Recall the basic syntax for using tar is:
tar <operation> [option] [folder/file]
The non-common options for tar are not often used, but you should have an idea of what else can be done with tar.
When multiple files with the same filename occur in a tar archive, you can have specific commands carried out on an occurrence of the file. The options which work with the “--occurrence” option are “--diff”, “--delete”, “--extract” and “--list”. For example, if a tar file named “archive.tar” had multiple occurrences of the file named “data.db” and you wanted to extract the fifth file with the name, the command would be “tar -xf archive.tar --occurrence 5 data.db”. The file would be extracted to the current folder.
When extracting files from an archive, it may be needed that existing files are overwritten. To overwrite files, use the “--overwrite” option. If I had a tar file, named “Docs.tar”, containing documents and I need to extract the files to a folder called “Docs” overwriting all the files, the command would be “tar -xf Docs.tar --overwrite -C Docs/”. The option overwrites existing file permissions with those in the arhive.
To extract files and change the owner of the file at the same time, use the “--owner” option. To extract a file called “Data.db” from the “Database.tar” archive and make the new owner “Jarret”, use the command “tar -xf Database.tar --owner Jarret Data.db”.
If files are archived from the root, such as “/usr/share/”, by extracting the files they will start back at the root because of the leading “/” and go into “usr”. To make the files relative to the current folder, use the “-P” option. The leading “/” will be removed and a folder called “usr” will be placed in the local directory. So, to extract files to the local folder which were backed up to a file called “User.tar” of the “/usr/share/” directory, use the command “tar -xf User.tar -P”.
The “--pax-option” option is for POSIX archives to handle extended header keywords. The Keyword List is a list of POSIX header keywords separated by commas.
On some systems, a file has permissions set by an Access Control List (ACL). If the ACL needs to be kept with the archived files, use the “--acls” option. If you need to create an archive of the files in the “data” folder (named “ACLOut.tar”) and include the ACL, use the command “tar -cf ACLOut.tar --acls data/*”.
To prevent the storage or extraction of ACLs, use the “--no-acls” option. For example, to not extract the ACLs from the previous example, the command would be “tar -xf ACLOut.tar --no-acls”.
Permissions for SELinux are stored in the archive with the files. If you need to create an archive of the files in the “data” folder (named “SELinuxOut.tar”) and include the SELinux permissions, use the command “tar -cf SELinuxOut.tar --selinux data/*”.
When needing to archive or extract files with extended attributes, use the “--xattrs” option. The option is equivalent to both the “--acls” and “--selinux” options together.
The “--no-xattrs” option does not store or extract extended attributes. The option is the same as combining the “--no-acls” and “--no-selinux” options.
As usual, practice the options and understand them.
This article continues the uncommon options of the tar command.
Recall the basic syntax for using tar is:
tar <operation> [option] [folder/file]
The non-common options for tar are not often used, but you should have an idea of what else can be done with tar.
- --occurrence[=NUMBER]
When multiple files with the same filename occur in a tar archive, you can have specific commands carried out on an occurrence of the file. The options which work with the “--occurrence” option are “--diff”, “--delete”, “--extract” and “--list”. For example, if a tar file named “archive.tar” had multiple occurrences of the file named “data.db” and you wanted to extract the fifth file with the name, the command would be “tar -xf archive.tar --occurrence 5 data.db”. The file would be extracted to the current folder.
- --overwrite
When extracting files from an archive, it may be needed that existing files are overwritten. To overwrite files, use the “--overwrite” option. If I had a tar file, named “Docs.tar”, containing documents and I need to extract the files to a folder called “Docs” overwriting all the files, the command would be “tar -xf Docs.tar --overwrite -C Docs/”. The option overwrites existing file permissions with those in the arhive.
- --overwrite-dir
- --owner USER
To extract files and change the owner of the file at the same time, use the “--owner” option. To extract a file called “Data.db” from the “Database.tar” archive and make the new owner “Jarret”, use the command “tar -xf Database.tar --owner Jarret Data.db”.
- -p, --same-permissions, --preserve-permissions
- -P, --absolute-names
If files are archived from the root, such as “/usr/share/”, by extracting the files they will start back at the root because of the leading “/” and go into “usr”. To make the files relative to the current folder, use the “-P” option. The leading “/” will be removed and a folder called “usr” will be placed in the local directory. So, to extract files to the local folder which were backed up to a file called “User.tar” of the “/usr/share/” directory, use the command “tar -xf User.tar -P”.
- --pax-option KEYWORD-LIST
The “--pax-option” option is for POSIX archives to handle extended header keywords. The Keyword List is a list of POSIX header keywords separated by commas.
- --posix
- --preserve
- --acls
On some systems, a file has permissions set by an Access Control List (ACL). If the ACL needs to be kept with the archived files, use the “--acls” option. If you need to create an archive of the files in the “data” folder (named “ACLOut.tar”) and include the ACL, use the command “tar -cf ACLOut.tar --acls data/*”.
- --no-acls
To prevent the storage or extraction of ACLs, use the “--no-acls” option. For example, to not extract the ACLs from the previous example, the command would be “tar -xf ACLOut.tar --no-acls”.
- --selinux
Permissions for SELinux are stored in the archive with the files. If you need to create an archive of the files in the “data” folder (named “SELinuxOut.tar”) and include the SELinux permissions, use the command “tar -cf SELinuxOut.tar --selinux data/*”.
- --no-selinux
- --xattrs
When needing to archive or extract files with extended attributes, use the “--xattrs” option. The option is equivalent to both the “--acls” and “--selinux” options together.
- --no-xattrs
The “--no-xattrs” option does not store or extract extended attributes. The option is the same as combining the “--no-acls” and “--no-selinux” options.
As usual, practice the options and understand them.