LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2007, 10:09 PM   #1
martino4262
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Rep: Reputation: 0
remove folder above the file if that contains "failure"


i have folders,

ab/cd/ef/001/p.text
ab/cd/ef/002/p.text
ab/cd/ef/003/p.text
ab/cd/ef/004/p.text

These p.text files are not same. Some p.text have line containing "failure". I need a linux command to remove the folder if the file in that folder contains "failure" otherwise don't delete.

Basically want to delete,
001/p.text
002/p.text
003/p.text
004/p.text
only if p.text contains line with word "failure"

Last edited by martino4262; 10-31-2007 at 09:33 AM.
 
Old 10-31-2007, 12:24 AM   #2
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Code:
$ find . -name p.text -exec grep -q failure {} \; -print|while read i;do echo $(dirname "$i");done
This will give you a list of directories that match. Verify that it's working fine, then replace 'echo' with 'rm -r'

Please double check that everything is fine before actually deleting, also make sure to backup!

Last edited by angrybanana; 10-31-2007 at 12:25 AM.
 
Old 10-31-2007, 12:43 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have GNU grep
Code:
grep -l "failure" /path/ab/cd/ef/00*/p.text |xargs -i  cp '{}' /destination
 
Old 10-31-2007, 09:34 AM   #4
martino4262
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Hi,

thanks for your reply..

I tried but it said illegal name?? do i need to define i?

$ find . -name f.dat -exec grep -q nopace {}\; -print | while read i ; do echo $(dirname "$i" ) ; done
Illegal variable name.

Quote:
Originally Posted by angrybanana View Post
Code:
$ find . -name p.text -exec grep -q failure {} \; -print|while read i;do echo $(dirname "$i");done
This will give you a list of directories that match. Verify that it's working fine, then replace 'echo' with 'rm -r'

Please double check that everything is fine before actually deleting, also make sure to backup!
 
Old 10-31-2007, 09:38 AM   #5
martino4262
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Original Poster
Rep: Reputation: 0
thanks for your reply. i got following reply?

$ grep -l "nopace" /path/test2/white/0*/f.dat | xargs -i cp '{}' /temp/
grep: No match.



Quote:
Originally Posted by ghostdog74 View Post
if you have GNU grep
Code:
grep -l "failure" /path/ab/cd/ef/00*/p.text |xargs -i  cp '{}' /destination
 
Old 10-31-2007, 03:34 PM   #6
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Quote:
Originally Posted by martino4262 View Post
$ grep -l "nopace" /path/test2/white/0*/f.dat | xargs -i cp '{}' /temp/
grep: No match.
The message says this: The shell tries to expand /path/test2/white/0*/f.dat before passing the argument list to grep, but there's no matching file or directory. maybe you should adapt the pattern; e.g., using p.text.

You might not like the result if the above line worked: It would copy all "f.dat" files that contain "nopace" into "/temp/".
  • I think you're looking for "failure" not "nopace"
  • You typically don't have "/temp", only "/tmp".
  • As they all have the same name, every "f.dat" would overwrite the previous one. You'd end up with a copy of just the last matching in /temp/f.dat
  • Well, it doesn't remove anything, certainly not not the parent directories :-)

You didn't tell us enough about your folders. If there are just the 4 you mention,
Code:
grep -l failure ab/cd/ef/00[1-4]/p.text | cut -d/ -f-4 | xargs rm -ri
Adapt the pattern to your situation. Before unlinking, rm will ask you for each file. When you're sure it does the right thing, you can omit the "i" option.
 
  


Reply



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 issue "ls" command start with the largest file size in a folder? fjkum Linux - Newbie 5 07-27-2006 03:28 AM
remove folder that stats with a "special" character mago Linux - General 3 06-27-2006 04:40 PM
kmail "could not remove folder" errors GoBillsBN Linux - Software 1 08-03-2005 07:58 PM
rename file with "mv" not working for folder? Niceman2005 Linux - Newbie 2 03-13-2005 09:34 PM
Cannot "Copy To" .jpg file to wallpaper folder LeanPudLou SUSE / openSUSE 5 11-08-2004 08:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:05 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