LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-28-2009, 02:38 AM   #1
somae
Member
 
Registered: Nov 2006
Posts: 56

Rep: Reputation: 15
tar -k --keep-old-files extraction problem


I've been trying to extract an archive using tar and want to prevent it from overwriting any files with the same names using
Code:
tar -xkvf --keep-old-files (file.tar)
but it keeps giving errors.

Was wondering how to do it?

Thanks.
 
Old 01-28-2009, 02:47 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
First don't put --keep-old-files AND -k. They are two forms (long and short) of the same option. Second invert the groups of option and it should work. This is because the -f option accepts as argument the name of the archive, so that the part f --keep-old-files is interpreted as the name of the archive. In summary the solution is either one of the following
Code:
$ tar --keep-old-files -xvf file.tar
$ tar -xkvf file.tar
 
Old 02-02-2009, 01:44 AM   #3
somae
Member
 
Registered: Nov 2006
Posts: 56

Original Poster
Rep: Reputation: 15
Thanks.

One thing that confused me is that there are several alternatives for the "-k" option - some of which are the opposite of what I want.

I assume there is some interactive dialog that comes up when you use "-k"?

I just tried the
Code:
 tar --keep-old-files -xvf file.tar
method and got alot of
Quote:
Cannot open: File exists
tar: Skipping to next header
which ended with
Quote:
tar: Error exit delayed from previous errors
.

Last edited by somae; 02-02-2009 at 01:55 AM. Reason: adding more information
 
Old 02-02-2009, 01:57 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Not an interactive dialog, but just an error message. You can either chose --overwrite to force overwriting OR --keep-old-files to prevent this behavior. That is your choice is made prior to run the extraction of the archive, not in run time.

What do you mean for "there are several alternatives for the -k option"? To me the -k option is unique.
 
Old 02-03-2009, 12:31 AM   #5
somae
Member
 
Registered: Nov 2006
Posts: 56

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
What do you mean for "there are several alternatives for the -k option"? To me the -k option is unique.
The "tar --help" file says this:
Code:
 -k, --keep-old-files       don't replace existing files when extracting
      --keep-newer-files     don't replace existing files that are newer than
                             their archive copies
      --no-overwrite-dir     preserve metadata of existing directories
      --overwrite            overwrite existing files when extracting
      --overwrite-dir        overwrite metadata of existing directories when
                             extracting (default)
      --recursive-unlink     empty hierarchies prior to extracting directory
      --remove-files         remove files after adding them to the archive
It makes it look like these are all functions of "-k".
 
Old 02-03-2009, 01:41 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Quote:
Originally Posted by somae View Post
It makes it look like these are all functions of "-k".
Nope. It tells that the -k option is the short form of --keep-old-files, while the other options listed do not have a short form. Indeed they are all related to the behavior of tar in case of already existing files with the same name, but -k is --keep-old-files only.

I see now the reason why you used both -k and --keep-old-files in the same command line. Well, the help can be confusing, but keep in mind that an option may accept one or more arguments, but never accept another option as argument, that is a string beginning with - or --.
Code:
-k, --keep-old-files       don't replace existing files when extracting
The comma tells you they are two different alternatives of the same option.
 
Old 02-03-2009, 01:45 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Quote:
Originally Posted by somae View Post
tar: Error exit delayed from previous errors
This is the normal behavior. If one or more files already exist, the file(s) are not extracted from the archive and tar gives an error message and an exit status different from zero.
 
Old 02-27-2009, 02:34 PM   #8
ciotog
Member
 
Registered: Mar 2004
Location: Canada
Distribution: Slackware current
Posts: 728
Blog Entries: 2

Rep: Reputation: 43
If you've specified "keep" or "-k" then tar shouldn't complain that a file already exists, it should ignore it. I think that's the crux of the problem.
 
Old 02-27-2009, 02:51 PM   #9
ciotog
Member
 
Registered: Mar 2004
Location: Canada
Distribution: Slackware current
Posts: 728
Blog Entries: 2

Rep: Reputation: 43
I've been reading through the info pages, and this is what is says verbatum:
Quote:
To be even more cautious and prevent existing files from being replaced, use the `--keep-old-files' (`-k') option. It causes `tar' to refuse to replace or update a file that already exists, i.e., a file with the same name as an archive member prevents extraction of that archive member. Instead, it reports an error.
So evidently it's by design.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a tough question 4 u, problem in extracting tar & tar.gz files p_garg Linux - General 5 11-08-2010 11:02 AM
Text extraction from files TheEngineer Linux - General 4 11-28-2008 04:26 AM
Multi-part password protected .rar files extraction lx3000 Linux - Software 6 04-13-2006 09:02 AM
SuSE: Disk quota, tar extraction, Performance & Benchmarking colabus Linux - Newbie 1 11-14-2004 03:16 PM
SuSE: Disk quota, tar extraction, Performance & Benchmarking colabus SUSE / openSUSE 1 11-14-2004 12:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration