Applications 30 – Uncommon Options of tar Part 6

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.

  • --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
When creating an archive, the total bytes written can be displayed after the archive is created by using the “--totals” option. To create “Documents.tar” from the folder “Data” and show the bytes written, use the command “tar -cf Documents.tar --totals Data/*”. The speed in which the data is written will also be displayed.

  • -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
Get a verbose listing of files with any output generated by tar by using the option “-v”. To create “Documents.tar” from the folder “Data” and show verbose output, use the command “tar -cvf Documents.tar Data/*”. The “-v” option can be used when creating, listing or extracting files.

  • -V, --label NAME
The option “-V” will create a volume label in a new archive. When used with extracting files, only a file with a matching volume name can be used. To create “Documents.tar” from the folder “Data” and set a volume label as “DocumentData”, use the command “tar -cvf Documents.tar -V DocumentData Data/*”. To see the volume label in the tar file, use the command “tar -tvf Documents.tar”. The Volume label is at the beginning and listed as a “Volume Header”.

  • --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
To allow the user to be prompted for each action as it occurs, use the “-w” option. When creating a tar, each file added will require the user answer yes (“y”) to each file being added. When extracting, it will prompt the user to extract each file with a response of yes (“y”) or no (“n”).

  • -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
The “-z” option is used to run the tar file through the gzip compression utility and compress the tar. Be sure to make the output file extension “.tar.gz”. To archive and compress a folder called “Docs” to a file called “Documents.tar”, the command would be “tar -czf Documents.tar.gz Docs/*”.

As usual, practice these options so you are aware that they exist and hopefully can spot them on the Linux+ Certification Exam.
 

Attachments

  • slide.jpg
    slide.jpg
    32.9 KB · Views: 176,491

Staff online


Top