InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Checking a tar Archive for Unique Directories in bash
Bash

Checking a tar Archive for Unique Directories in bash

InfinityCoder February 19, 2017

You want to untar an archive, but you want to know beforehand into which directories it is going to write.

You can look at the table of contents of the tarfile by using tar -t, but this output can be very large and it’s easy to miss something.

Use an awk script to parse off the directory names from the tar archive’s table of contents, then use sort -u to leave you with just the unique directory names:

1
$ tar tf some.tar | awk -F/ '{print $1}' | sort -u

The t option will produce the table of contents for the file specified with the f option whose filename follows.

The awk command specifies a non-default field separator by using -F/ to specify a slash as the separator between fields.

Thus, the print $1 will print the first directory name in the pathname.
Finally, all the directory names will be sorted and only unique ones will be printed.
If a line of the output contains a single period then some files will be extracted into the current directory when you unpack this tar file, so be sure to be in the directory you desire.
Similarly, if the filenames in the archive are all local and without a leading ./ then you will get a list of filenames that will be created in the current directory.
If the output contains a blank line, that means that some of the files are specified with absolute pathnames (i.e., beginning with /), so again be careful, as extracting such an archive might clobber something that you don’t want replaced.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Finding All Your MP3 Files in bash
You have MP3 audio files scattered all over your filesystem. …

Finding All Your MP3 Files in bash

Getting Yes or No Input in bash
You need to get a simple yes or no input …

Getting Yes or No Input in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap