Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have the collection of text files containing/not the phrase (B core file).

Then I tried this:

sudo find / -type f -name core | xargs file | grep 'B core file' | sed 's/:ELF.*//g' | xargs rm -f 

To remove specific files with the phrase "B core file".

But that command won't work.

I'm hoping for the solution. Thank you very much.

share|improve this question

2 Answers 2

Only the find command will be run with root permissions. The rm will be run with your user id and because of this will fail. Put the whole command in a script and run the script with sudo.

You can also use an extra sudo for each command, which access the files, which are probably not readable or writable for you:

sudo find / -type f -name core | xargs sudo file | sudo grep 'B core file' | sed 's/:ELF.*//g' | xargs sudo rm -f 
share|improve this answer
    
Thank ceving. I'll give it a try. However, could you confirm that the regex 's/:ELF.*//g' is proper. I don't what ELF is stand for? –  Dzung May 9 '11 at 3:01
    
I tried: sudo find / -type f -name core | xargs file | grep 'B core file' | sed 's/:.*//g' | xargs rm -f. The regex ':.*' is to replace all chars following the colons which will then give me the path of the file only, but I still don't get it fully ( because ':*' didn't work). In my case, it's not permission issue. because the file was found is created by me. –  Dzung May 9 '11 at 3:11

That looks overly complicated.

try:

grep -l 'B core file' * | xargs rm

It will remove files containing 'B core file' from the directory you're standing in when running this.

share|improve this answer
    
Hi Mikael, I was about extract the path of filtered files based on file type, not the content of the files. Regards, –  Dzung May 10 '11 at 1:46
    
Oh ok. Your question and title isn't very clear. –  Mikael E May 10 '11 at 8:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.