Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 02-20-2007, 04:25 PM   #1
Registered: Aug 2005
Distribution: Suse
Posts: 53

Rep: Reputation: 15
delete files uncompressed into wrong directory safely

I uncompressed a tar into a directory that has some files I need. I'd like to just delete those uncompressed files and tried:
tar ftx ft.tgz > rm
and about a million other variants and can't get it to do what I want. Can someone give me a hint on how to delete ONLY the files that are in the tar?
Old 02-20-2007, 04:40 PM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Your idea is good, but here is my hint:
tar ztvf ft.tgz | gawk '{print "rm",$6}' >
then double check the resulting and when you are absolutely sure, execute
I'm not totally sure of $6 in the awk statement, but you can easily check that it matches the filename by issuing the command (alone)
tar ztvf ft.tgz
I guess the untarred files do not have a relative path, that is they don't look like "some/dir/filename" but only "filename", otherwise we can parse the output of the above command to change the directory name accordingly.

Last edited by colucix; 02-20-2007 at 04:44 PM.
Old 02-20-2007, 05:11 PM   #3
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
If the files were extracted into their own directory, then it would be easy to remove the contents. If not, one thing you could do is use the find command with a narrow -cmin range:

find top/extraction/dir -cmin +500 -cmax -504 >filestodeletelist

You could also use the tarball's file listing to compare so you don't delete files that have been replaced since the extraction. ( Using comm or grep -f ).
This would prevent deleting a file that had been subsequently replaced.

You also want to learn about the xargs command. This is the one you would use to take a list from another command and supply them to another command. If some of the arguments contain spaces or other whitespace then use the -print0 option when using find and the corresponding -0 argument for xargs.

For example:
find ./ -cmin +1200 -cmin -1205 -print0 | xargs -0 rm
tar tzf tarball.tar.gz | tr '\n' '\000' | xargs -0 rm

Last edited by jschiwal; 02-20-2007 at 07:08 PM.
Old 03-04-2007, 02:38 PM   #4
Registered: Aug 2005
Distribution: Suse
Posts: 53

Original Poster
Rep: Reputation: 15
The xargs worked like a charm. Thanks! (I didn't get to try the first suggestion since the second one worked the first time.)


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete files with 0k file size in a directory justgiver Linux - Newbie 4 01-28-2008 05:56 AM
Script to delete week old files in a directory ilyavishnyakov Linux - General 11 02-13-2007 05:37 PM
How to delete once whole files in a remote directory? backpacker Linux - Networking 1 03-10-2006 06:04 AM
rm can't delete 999999 files in one directory yelo Linux - Software 10 04-10-2004 03:00 PM
How can I delete a directory and its files? OrganicX Linux - General 6 09-01-2002 05:25 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:17 PM.

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