2

I want to delete all files with a specific extension - ".fal", in a folder and its subfolders, except the one named "*Original.fal". The problem is that I want to delete other files that have the same extension:

  • *Original.fal.ds
  • *Original.fal.ds.erg
  • *Original.fal.ds.erg.neu

There are other ".fal"s that I want to delete as well, that don't have "Original" in them. Names vary all the time, so I can't delete specific names. The *Original.fal doesn't vary.

I can only get up to here:

$find /disk_2/people/z183464/DOE-Wellen -name "*.fal" \! -name "*Original.fal" -type f -exec echo rm {} \;

It would be great if the command can delete only in the folder (and it's subfolders) where it has been called (executed)

When I run the code it gives me an error:

  1. /disk_2/people/z183464/DOE-Wellen: is a directory
5
  • content.hccfl.edu/pollock/Unix/FindCmd.htm Commented May 21, 2013 at 8:17
  • Just rename the file you don't want to delete, then do a simple rm, then rename the file back again. Commented May 21, 2013 at 8:32
  • Your find solution should work fine. The error message seems just because you typed your one-liner on two lines. That -type f-stuff should be on the same line as the stuff above, then it should function. Hint: use -exec echo rm {} \; to see what it would do before actually executing it without the echo. Commented May 21, 2013 at 8:43
  • Yep, I got it back to the first line, but the first error still stays. How can I do the same, without specifying the folder, but do it instead in the folder that it's being executed (called) Commented May 21, 2013 at 8:51
  • Aside from the pointers others have given, I want to point out : ".fal" is not what you want. that gets files ending in .fal. In other words, only *Original.fal would match. You probably want ".fal.*" which will actually get all files that contain '.fal.' -- IOW, it will select all of the items you listed but not "*Original.fal". Commented May 21, 2013 at 8:51

2 Answers 2

3

If you do not want find to dive too deep, you can restrict it with -maxdepth.

Sign up to request clarification or add additional context in comments.

15 Comments

@Vihar: Well, the question changed :-) You have to backslash the exclamation mark, it is a special character in bash (related to history expansion).
Yes it did change :) I've tried with \ before and after the exclamation mark - still the same...
@Vihar: Putting a backslash after the exclamation mark quotes the space. Before shoudl be enough.
I was saying i tried both. I had a mistake, putting -type on the next line, so the second error is gone now. But the first one stays even with the backslash before the exclamation mark.
@Vihar: Move the -type condition before the negation.
|
0

You can use a simple for loop for that. This command shows all the files you might want to delete. Change echo with rm to delete them.

cd /disk_2/people/z183464/DOE-Wellen && for I in `find . -name "*.fal" ! -name "*Original.fal"`; do echo $I; done

With "find ... | grep ..." you can use regex too, if you need more flexibility.

1 Comment

When I run the code it gives me: /disk_2/people/z183464/DOE-Wellen: is a directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.