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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now