LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   "Undoing" a tar extract (https://www.linuxquestions.org/questions/linux-newbie-8/undoing-a-tar-extract-267152/)

jrdioko 12-16-2004 03:24 PM

"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.

reddazz 12-16-2004 03:30 PM

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.

jrdioko 12-16-2004 03:42 PM

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?

Electro 12-16-2004 03:50 PM

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.

jrdioko 12-16-2004 04:08 PM

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."

reddazz 12-16-2004 04:09 PM

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.

Electro 12-16-2004 10:59 PM

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.

scuzzman 12-16-2004 11:40 PM

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.

kees-jan 12-17-2004 03:40 AM

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

Groetjes,

Kees-Jan

frob23 12-17-2004 09:19 AM

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.

jrdioko 12-17-2004 01:25 PM

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.

infinity0 08-24-2009 05:07 PM

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.)

i92guboj 08-24-2009 05:24 PM

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.

infinity0 08-26-2009 06:06 AM

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.

i92guboj 08-26-2009 06:15 AM

Quote:

Originally Posted by infinity0 (Post 3658024)
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.


All times are GMT -5. The time now is 10:50 PM.