tar command

Show me and explain 10 examples of the tar command.

The tar command in Unix-like operating systems is used for archiving files and directories into a single file and for extracting files from a tar archive. It is a versatile tool with numerous options and use cases. Here are 10 examples of the tar command with explanations:

  1. Create a Tar Archive: To create a tar archive of a directory or file, use the -c (create) option along with the -f (filename) option to specify the name of the archive file.

    bash
    tar -cf archive.tar directory/

    This command will create a tar archive named archive.tar of the directory/.

  2. Extract a Tar Archive: To extract the contents of a tar archive, use the -x (extract) option along with the -f option to specify the archive file.

    bash
    tar -xf archive.tar

    This command will extract the contents of archive.tar to the current directory.

  3. List Contents of a Tar Archive: You can list the contents of a tar archive without extracting it using the -t (list) option.

    tar -tf archive.tar

    This command will display a list of files and directories within archive.tar.

  4. Create a Compressed Tar Archive (gzip): To create a compressed tar archive using gzip, add the -z option.

    tar -czf archive.tar.gz directory/

    This command will create a tar archive named archive.tar.gz with gzip compression.

  5. Extract a Compressed Tar Archive (gzip): To extract a compressed tar archive with gzip, use the -xzf options.

    tar -xzf archive.tar.gz

    This command will extract the contents of archive.tar.gz to the current directory.

  6. Create a Tar Archive with Compression (bzip2): To create a tar archive with bzip2 compression, use the -j option.

    tar -cjf archive.tar.bz2 directory/

    This command will create a tar archive named archive.tar.bz2 with bzip2 compression.

  7. Extract a Tar Archive with Compression (bzip2): To extract a tar archive with bzip2 compression, use the -xjf options.

    tar -xjf archive.tar.bz2

    This command will extract the contents of archive.tar.bz2 to the current directory.

  8. Add Files to an Existing Tar Archive: You can add files to an existing tar archive using the -r (append) option.

    tar -rf archive.tar newfile.txt

    This command will add newfile.txt to the archive.tar.

  9. Extract Specific Files from a Tar Archive: To extract specific files from a tar archive, specify their names as arguments.

    tar -xf archive.tar file1.txt file2.txt

    This command will extract only file1.txt and file2.txt from archive.tar.

  10. Extract Tar Archive to a Different Directory: To extract a tar archive to a different directory, use the -C (directory) option.

    tar -xf archive.tar -C /path/to/directory/

    This command will extract the contents of archive.tar to the /path/to/directory/.

These examples cover various common use cases of the tar command, from creating and extracting archives to handling compressed archives and more. Remember to replace archive.tar and directory/ with your specific filenames and paths.

You should also read: