LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-16-2004, 03:24 PM   #1
jrdioko
Member
 
Registered: Oct 2002
Distribution: Debian 6.0.2 (squeeze)
Posts: 944

Rep: Reputation: 30
Question "Undoing" a tar extract


I occasionally run across a .tar.gz file that decides to extract files all over the place rather than putting them in a single folder, so I download it to /usr/local/src, tar zxvf filename.tar.gz, and wind up with 100 individual files and extra folders directly in /usr/local/src/ rather than /usr/local/src/filename/. I was wondering if there is any way to make tar go through its list of files and folders and remove them instead of creating them, or some other easy way to remove all these files rather than going through the list manually. I'd like to "undo" the extraction, put the .tar.gz in its own folder, and try again. Thanks in advance.
 
Old 12-16-2004, 03:30 PM   #2
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
I don't think there is. I suggest you make a seperate folder everytime you want to untar something, move the tarred file into that seperate folder and untar from there. When I am using GNOME or KDE, I tend to use file-roller or ark to see the contents of the archive so that when I untar and unzip, I know exactly what I am untarring and where to untar it.
 
Old 12-16-2004, 03:42 PM   #3
jrdioko
Member
 
Registered: Oct 2002
Distribution: Debian 6.0.2 (squeeze)
Posts: 944

Original Poster
Rep: Reputation: 30
Hmm, I guess a quick list before actually untarring would be a good idea in the future. Well then can I list the contents of the archive into a file and make some kind of script to go through and delete all the entries in the file?
 
Old 12-16-2004, 03:50 PM   #4
Electro
LQ Guru
 
Registered: Jan 2002
Posts: 6,042

Rep: Reputation: Disabled
The correct command you should use is "tar -zxvf filename.tar.gz -Cdesire_directory". No need to copy it to the directory where you want it to be extracted.

The list the files that is in a tar file use "tar -tzf filename.tar.gz".

Stop saying folder. Its the wrong term in Windows and in Linux. Correct term is directory.

If you read the man pages for tar, you will figure it out.
 
Old 12-16-2004, 04:08 PM   #5
jrdioko
Member
 
Registered: Oct 2002
Distribution: Debian 6.0.2 (squeeze)
Posts: 944

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by Electro
Stop saying folder. Its the wrong term in Windows and in Linux. Correct term is directory.

If you read the man pages for tar, you will figure it out.
Excuse me for using incorrect terminology, and I did read the man page and search in the forums before posting, just to let you know.

On a more productive note...

Will the -C flag create the directory if it doesn't already exist? The man file states "change to directory DIR."
 
Old 12-16-2004, 04:09 PM   #6
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
Folder and directory are synonymous to most people. I am not a purist or a zealot so and as long as the other people understand what I mean, I don't see a problem with that.
 
Old 12-16-2004, 10:59 PM   #7
Electro
LQ Guru
 
Registered: Jan 2002
Posts: 6,042

Rep: Reputation: Disabled
If the tar files does not have a directory structure, you have to create the directory. Then extract it to that directory. If you created the tar file outside the directory where your files are, you will not have this problem. Sheesh, use some common sense.
 
Old 12-16-2004, 11:40 PM   #8
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
One thing you could do is make a directory, sat, /usr/local/src/tmp
put the tarball there and extract it, cp the contents where you need them, (even if it's jsut a directory above) them remove the contents of it.
 
Old 12-17-2004, 03:40 AM   #9
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
This issue has already been discussed here
It doesn't provide much help though.

Groetjes,

Kees-Jan
 
Old 12-17-2004, 09:19 AM   #10
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
Ugh, I have done this before... but once bitten, twice shy. I have learned to either check how the files will come out or create a temp folder to extract in.

If you are fairly certain that everything from the archive can be safely deleted... then you can try and create a script to delete the files. Something like (this is in the tcsh -- just type tcsh to start it):

foreach file (`tar tzf tarfile.tgz`)
rm -i $file
end

NOTE: Do not just run this. Test it first as I am at work right now an unable to create a sample test to run it on. I would create a tmp tarball in a non-important directory and run it there. But that is one way to undo it without sitting there and manually doing it.

Last edited by frob23; 12-17-2004 at 05:12 PM.
 
Old 12-17-2004, 01:25 PM   #11
jrdioko
Member
 
Registered: Oct 2002
Distribution: Debian 6.0.2 (squeeze)
Posts: 944

Original Poster
Rep: Reputation: 30
Thanks all for the help. I just assumed there would be an easy way to do this that I was overlooking but I see that there isn't. I'll be more careful about untarring in the future.
 
Old 08-24-2009, 05:07 PM   #12
infinity0
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Rep: Reputation: 2
sorry for reviving a long-dead thread, but this is currently one of the top results in google for "undo tar". here's a neat solution i came up with that does what you want:

tar tf archive.tar $SUBPATHS | sort -r | xargs rm -f 2>/dev/null
tar tf archive.tar $SUBPATHS | sort -r | xargs rmdir 2>/dev/null

the first line removes all the files, the second line removes all the directories.

$SUBPATHS is if you want to only remove some parts of the tar. if you want to undo the entire tar, just leave it blank.

any files in those paths that aren't part of the tar will be left alone. (if you want to see the directories of such files, remove the "2>/dev/null")

(the solution provided in the post below this one will not do this, and will remove all files that happened to be contained in a directory that was part of the tar, even if the file isn't part of the tar.)

Last edited by infinity0; 08-26-2009 at 06:16 AM.
 
1 members found this post helpful.
Old 08-24-2009, 05:24 PM   #13
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
There is a way, not straight, but it can be done with a bit of shell magic. I've done this in the past for the same exact reason.

tar can list the contents of an archive with the "t" flag. So, let's imagine that you just untarred a file on the current directory:

Code:
tar xf foo.tar.bz2
Then this command will list all the files:

Code:
tar tf foo.tar.bz2
All we need is to pipe that conveniently, there are many options, the most efficient one is probably to use xargs:

*code removed, look post above*

Voilą.

PS. Duh, I just noticed that the poster above had a similar solution. I didn't notice before, sorry. However, note that you don't need two separate commands, nor that sort stuff. Just use -rf.

Last edited by i92guboj; 08-26-2009 at 06:16 AM.
 
Old 08-26-2009, 06:06 AM   #14
infinity0
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Rep: Reputation: 2
no, it is absolutely VITAL you reverse sort the output and use rm and rmdir separately.

your solution will remove everything under the tar directory, INCLUDING files not part of the tar. If the tar contains /, you will effectively be doing rm -rf /

i ask that you edit your post to remove the faulty code in case newbies see it, don't read the whole thread, and run it.

Last edited by infinity0; 08-26-2009 at 06:08 AM.
 
1 members found this post helpful.
Old 08-26-2009, 06:15 AM   #15
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by infinity0 View Post
no, it is absolutely VITAL you reverse sort the output and use rm and rmdir separately.

your solution will remove everything under the tar directory, INCLUDING files not part of the tar. If the tar contains /, you will effectively be doing rm -rf /

i ask that you edit your post to remove the faulty code in case newbies see it, don't read the whole thread, and run it.
Ok.
 
1 members found this post helpful.
  


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
how to extract a "tar.bz2" file? deWin Linux - Newbie 14 08-09-2016 08:49 AM
"You don't have the right permissions to extract archives in the folder " please help redhatman13 Linux - Software 18 10-15-2008 05:52 AM
Dont get "extract here" choice on context menu eroica Mandriva 4 12-20-2005 01:38 PM
Unable to extract "tar.gz" file - please try for me Riddick Linux - Software 6 08-27-2005 04:57 PM
"how do I extract a number from a text file using shell command?" sdandeker Linux - Networking 3 02-12-2004 08:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:00 PM.

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