Skorpius
Skorpius

Reputation: 2255

Is there a way to specifically un-tar files by themselves in Unix?

I'm working on a script that serves to search a tar for strings and output files containing those strings to a folder- this is done so that I don't have to untar a big file and then search through that and then delete the unnecessary files

This is what I have so far:

#!/bin/bash

tarname=$1
pattern=$2

count=1
tar -tf  $tarname | while read -r FILE
do

tar -xf  $tarname $FILE

##grep the pattern with the file here...

done

So the problem I have with this is it assumes that the tar'd things from the tar file are all files, but they could just as easily be directories- is there a way to tar the files recursively with this code?

By the way, this is all in Unix, so some of the stuff might be outdated and may not have access to newer versions of commands

Upvotes: 1

Views: 65

Answers (2)

RBH
RBH

Reputation: 592

You can skip extracting directories by assuming all the directories have "/" in the end for example:

if [ $(echo $FILE|grep -c /$) == "1" ]; then

echo "skipping directory"

else 

tar -xf $tarname $FILE

fi

Upvotes: 0

DaveRead
DaveRead

Reputation: 3413

Yes, you can extract a file or directory individually using the following commands:

tar xvf archive_file.tar /path/to/file tar xvf archive_file.tar /path/to/dir/

From here: http://www.thegeekstuff.com/2010/04/unix-tar-command-examples/

Upvotes: 1

Related Questions