Ankur Agarwal
Ankur Agarwal

Reputation: 24748

Getting contents of a particular file in the tar archive

This script lists the name of the file ( in a tar archive) containing a pattern.

tar tf myarchive.tar | while read -r FILE
do
    if tar xf test.tar $FILE  -O | grep "pattern" ;then
        echo "found pattern in : $FILE"
    fi
done

My question is:

Where is this feature documented, where $FILE is one of the files in the archive:

tar xf test.tar $FILE

Upvotes: 0

Views: 150

Answers (1)

osgx
osgx

Reputation: 94175

This is usually documented in man pages, try running this command:

man tar

Unfortunately, Linux has not the best set of man pages. There is an online copy of tar manpage from this OS: http://linux.die.net/man/1/tar and it is terrible. But it links to info man command which is command to access the "info" system widely used in GNU world (many programs in linux user-space are from GNU projects, for example gcc). There is an exact link to section of online info tar about extracting specific files: http://www.gnu.org/software/tar/manual/html_node/extracting-files.html#SEC27

I may also recommend documentation from BSD (e.g. FreeBSD) or opengroup.org. Utilities can be different in detail but behave same in general.

For example, there is some rather old but good man from opengroup (XCU means 'Commands and Utilities' of the Single UNIX Specification, Version 2, 1997): http://pubs.opengroup.org/onlinepubs/7908799/xcu/tar.html

tar key [file...]

The following operands are supported:

key -- The key operand consists of a function letter followed immediately by zero or more modifying letters. The function letter is one of the following:

x -- Extract the named file or files from the archive. If a named file matches a directory whose contents had been written onto the archive, this directory is (recursively) extracted. If a named file in the archive does not exist on the system, the file is created with the same mode as the one in the archive, except that the set-user-ID and set-group-ID modes are not set unless the user has appropriate privileges. If the files exist, their modes are not changed except as described above. The owner, group, and modification time are restored (if possible). If no file operand is given, the entire content of the archive is extracted. Note that if several files with the same name are in the archive, the last one overwrites all earlier ones.

And to fully understand command tar xf test.tar $FILE you should also read about f option:

f -- Use the first file operand (or the second, if b has already been specified) as the name of the archive instead of the system-dependent default.

So, test.tar in your command will be used by f key as archive name; then x will use second argument ($FILE) as name of file or directory to extract from archive.

Upvotes: 2

Related Questions