J
Jarret W. Buse
Guest
Applications 30 – Uncommon Options of tar Part 6
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.
The option “--strip-components” will only list or extract the path at the specified level. For instance, if the following structure existed:
The Component levels are shown as:
Level 1: Folder1, Folder2, Folder6
Level 2: Folder3, Folder4
Level 3: Folder5
To strip the components to Number 2 would only extract Folder3 and Folder4. So, to extract Level 2 from an archive name “Levels.tar” to a folder named “Out”, use the command “tar -xf Levels.tar --strip-components=2 -C Out”.
When backing up files, the backup options use the “~” as a suffix. By using the “--suffix” option, you can specify a replacement.
If there is a list of files which needs to be archived, the file list can be used instead of typing each name on the command-line. If a file list to be used is named “Backuplist.txt” and the archive name is “FileBackup.tar”, the command would be “tar -cf FileBackup.tar -T Backuplist.txt”.
If a file is to be extracted and the file exists in the target location, the file can be deleted first using the “-U” option. To extract files and folders from “Documents.tar” into the “Data” folder while removing all existing files which are in the archive, use the command “tar -xUf Documents.tar Data/*”.
Allows the tar created to be run through an external compression program which is not recognized by tar. If the external program does not use “-d” parameter, then you need to use a script. In this example, I will use the program “pigz” to create a file name “Compresed.tar.gz” from the files in folder “Data”. The command would be “tar --use-compress-program=pigz -cf Compressed.tar.gz Data/*”.
When listing files, the modification dates are displayed in UTC format. To list the files in the archive “Data.tar” with modification dates in UTC, use the command “tar -tf Data.tar --utc”.
To see the version of tar that is on your system, run the command “tar --version”.
When dealing with multi-volume sets, the current volume being worked on can be stored in the file specified by the option “--volno-file”. By looking in the file, you can see the current volume being processed. The options can be used with scripts to ask for tape changes when creating or extracting archives.
In some cases, the files being archived may be important. It is imperative that the files are archived properly. Once a file is created, the files can be verified to be sure that they have been archived properly. To verify the archive contents after it is created, use the “-W” option. For example, to archive the “Data” folder to a file called “ImportantData.tar” and verify it, the command would be “tar -cWf ImportantData.tar Data/*”. Any verification errors will be shown after the file is created and it is tested. To see each file being verified, add the “-v” option.
By specifying the “--wildcards” option, you can the use wildcards with the “--exclude” option. Asterisks (*) represent multiple characters and question marks (?) represent a single character. Here, a slash (/) must be represented with a slash if needed.
By specifying the “--wildcards” option, you can the use wildcards with the “--exclude” option. Asterisks (*) represent multiple characters and question marks (?) represent a single character. Here, a slash (/) can be represented with a wildcard.
Files can be excluded when creating, listing, or extracting tar files.
If there is a list of files which needs to be excluded from an archive, the file list can be used instead of typing each name on the command-line. If a file list to be excluded is named “XBackuplist.txt” and the archive name is “FileBackup.tar”, the command would be “tar -cf FileBackup.tar -X Xbackuplist.txt”. This option works opposite to the “-T” option.
When creating or extracting a tar, the “compress” program is used to help conserve space.
NOTE: You need to have the compress program installed. If it is not, use the command “sudo apt-get install ncompress”.
As usual, practice these options so you are aware that they exist and hopefully can spot them on the Linux+ Certification Exam.
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.
- --strip-components NUMBER, --strip-path NUMBER
The option “--strip-components” will only list or extract the path at the specified level. For instance, if the following structure existed:
- Folder1
- Folder2
- Folder3
- Folder4
- Folder5
- Folder6
The Component levels are shown as:
Level 1: Folder1, Folder2, Folder6
Level 2: Folder3, Folder4
Level 3: Folder5
To strip the components to Number 2 would only extract Folder3 and Folder4. So, to extract Level 2 from an archive name “Levels.tar” to a folder named “Out”, use the command “tar -xf Levels.tar --strip-components=2 -C Out”.
- --suffix SUFFIX
When backing up files, the backup options use the “~” as a suffix. By using the “--suffix” option, you can specify a replacement.
- -T, --files-from F
If there is a list of files which needs to be archived, the file list can be used instead of typing each name on the command-line. If a file list to be used is named “Backuplist.txt” and the archive name is “FileBackup.tar”, the command would be “tar -cf FileBackup.tar -T Backuplist.txt”.
- --totals
- -U, --unlink-first
If a file is to be extracted and the file exists in the target location, the file can be deleted first using the “-U” option. To extract files and folders from “Documents.tar” into the “Data” folder while removing all existing files which are in the archive, use the command “tar -xUf Documents.tar Data/*”.
- --use-compress-program PROG
Allows the tar created to be run through an external compression program which is not recognized by tar. If the external program does not use “-d” parameter, then you need to use a script. In this example, I will use the program “pigz” to create a file name “Compresed.tar.gz” from the files in folder “Data”. The command would be “tar --use-compress-program=pigz -cf Compressed.tar.gz Data/*”.
- --utc display file modification dates in UTC
When listing files, the modification dates are displayed in UTC format. To list the files in the archive “Data.tar” with modification dates in UTC, use the command “tar -tf Data.tar --utc”.
- -v, --verbose
- -V, --label NAME
- --version
To see the version of tar that is on your system, run the command “tar --version”.
- --volno-file F
When dealing with multi-volume sets, the current volume being worked on can be stored in the file specified by the option “--volno-file”. By looking in the file, you can see the current volume being processed. The options can be used with scripts to ask for tape changes when creating or extracting archives.
- -w, --interactive, --confirmation
- -W, --verify
In some cases, the files being archived may be important. It is imperative that the files are archived properly. Once a file is created, the files can be verified to be sure that they have been archived properly. To verify the archive contents after it is created, use the “-W” option. For example, to archive the “Data” folder to a file called “ImportantData.tar” and verify it, the command would be “tar -cWf ImportantData.tar Data/*”. Any verification errors will be shown after the file is created and it is tested. To see each file being verified, add the “-v” option.
- --wildcards
By specifying the “--wildcards” option, you can the use wildcards with the “--exclude” option. Asterisks (*) represent multiple characters and question marks (?) represent a single character. Here, a slash (/) must be represented with a slash if needed.
- --wildcards-match-slash
By specifying the “--wildcards” option, you can the use wildcards with the “--exclude” option. Asterisks (*) represent multiple characters and question marks (?) represent a single character. Here, a slash (/) can be represented with a wildcard.
- --exclude PATTERN
Files can be excluded when creating, listing, or extracting tar files.
- -X, --exclude-from FILE
If there is a list of files which needs to be excluded from an archive, the file list can be used instead of typing each name on the command-line. If a file list to be excluded is named “XBackuplist.txt” and the archive name is “FileBackup.tar”, the command would be “tar -cf FileBackup.tar -X Xbackuplist.txt”. This option works opposite to the “-T” option.
- -Z, --compress, --uncompress
When creating or extracting a tar, the “compress” program is used to help conserve space.
NOTE: You need to have the compress program installed. If it is not, use the command “sudo apt-get install ncompress”.
- -z, --gzip, --gunzip, --ungzip
As usual, practice these options so you are aware that they exist and hopefully can spot them on the Linux+ Certification Exam.