Varify extracted files from a tarball are not going to over write existing ones, and prompt user if it is going to occur

Posted on 2004-10-19
Last Modified: 2010-04-22

Does anyone know how to verify files being .  I am trying to implement this using the bash shell script.

Basically I know tar allows you to set the "-w" option to prompt users and it also has the "-k" option to fail when a overwrite occurs, but I am trying to basically do the following:

1) Check if files exist
    a) if they exist prompt user to overwrite
    b) if they don't exist just write them.

 "tar -vwkzxf My.tar.gz"

This commandline does not work how I need it to work.  It is does not even execute in the correct order and it prompts the user ever time regardless if the files exist in the directory.

Does any know of a way "tar" can return an error code or failure.

Best Regards,
Question by:jim_welch_se

Accepted Solution

NVRAM earned 125 total points
ID: 12350460
You could do it in multiple steps....

If you want to allow the user to accept all/none of the overwrites:
1) Get a listing of files in the archive,
2) Determine which of those files exist,
3) Prompt the user for overwriting the files,
4) Extract files if they user agreed.

Or, if you want to allow the user to accept individual overwrites:
1) Get a listing of files in the archive,
2) Divide that list into files that exist and don't exist.
3) Extract files that don't exist on the file system (w/o the "-w"),
4) Extract files that do exist (with the "-w").

tar -ztf ${FARCH} | sort | grep -v /$ > ${FLIST}.1
ls -1d $(<${FLIST}.1) 2>/dev/null >${FLIST}.2
comm -13 ${FLIST}.1 ${FLIST}.2 >${FLIST}.3

## Now $FLIST.1 lists the archive, $FLIST.2 is the existing
## files, and $FLIST.3 the non-existing files

--- Here (if you want) prompt for overwriting all files
--- and if they so choose, then just run:
   tar -xvzf ${FARCH}

--- Otherwise, if you want them to choose to overwrite files
--- individually, then you can just run:

# Overwrite (perhaps) the existing files...
tar -wxvzf ${FARCH} $(<${FLIST}.2)
# And then get the new files...
tar -kxvzf ${FARCH} $(<${FLIST}.3)

# Clean up...
rm -f ${FARCH}.[123]

Author Comment

ID: 12352444
Hello NVRAM,

I appreciate you taking the time to answer my question.  I think you are on the right track, but I tried your code snipets and they don't seem to do what I need...but with a little tweeking(see below)it seems to work fine for me.  Thanks Again.
tar -ztf ${FARCH} | sort | grep -v /$ > ${FLIST}.1
ls -1d $(<${FLIST}.1) 2>/dev/null >${FLIST}.2
comm -13 ${FLIST}.1 ${FLIST}.2 >${FLIST}.3

if [ -s ${FLIST}.2 ]
 echo" Archive contains dup files in directory"
 tar -wxvzf ${FARCH} --exclude-from=${FLIST}.3
 tar -xvzf ${FARCH} --exclude-from=${FLIST}.2
 echo "Archive Doesn't caont any dup files"
 tar -xvzf ${FARCH}

I modified your code in the following way.  When I run this

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Bash Script to find & Kill Multiple Process 3 102
OS & DB Recommendation 4 50
linux / python expert needed 3 102
Cron jobs 12 140
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question