LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 02-20-2007, 03:25 PM   #1
petersk
Member
 
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?
Regards,
Kurt
 
Old 02-20-2007, 03:40 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Your idea is good, but here is my hint:
Code:
tar ztvf ft.tgz | gawk '{print "rm",$6}' > script.sh
then double check the resulting script.sh and when you are absolutely sure, execute
Code:
bash script.sh
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)
Code:
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 03:44 PM.
 
Old 02-20-2007, 04:11 PM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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
or
tar tzf tarball.tar.gz | tr '\n' '\000' | xargs -0 rm

Last edited by jschiwal; 02-20-2007 at 06:08 PM.
 
Old 03-04-2007, 01:38 PM   #4
petersk
Member
 
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.)
Kurt
 
  


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 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 04:56 AM
Script to delete week old files in a directory ilyavishnyakov Linux - General 11 02-13-2007 04:37 PM
How to delete once whole files in a remote directory? backpacker Linux - Networking 1 03-10-2006 05:04 AM
rm can't delete 999999 files in one directory yelo Linux - Software 10 04-10-2004 02:00 PM
How can I delete a directory and its files? OrganicX Linux - General 6 09-01-2002 04:25 AM


All times are GMT -5. The time now is 05:10 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration