Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
233 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 500 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 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