Solved

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
2
228 Views
Last Modified: 2010-04-22
Hello,

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,
-Jim
0
Comment
Question by:jim_welch_se
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
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").

====================================================================
FLIST=/tmp/list.$$
FARCH=My.tar.gz
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]
0
 

Author Comment

by:jim_welch_se
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.
#*****************************
FLIST=/tmp/list.$$
FARCH=My.tar.gz
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 ]
then
 echo" Archive contains dup files in directory"
 tar -wxvzf ${FARCH} --exclude-from=${FLIST}.3
 tar -xvzf ${FARCH} --exclude-from=${FLIST}.2
else
 echo "Archive Doesn't caont any dup files"
 tar -xvzf ${FARCH}
fi
#****************************

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

718 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