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
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
225 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
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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