0

I have a hard drive that contains about 300,000 x 3 files that were written by a recovery program.

Some of these files are in the format:

.doc.apple.quarantine

These are easy to remove:

find . -type f -name '*.apple.quarantine' -delete

But others are more difficult because they have the phrase

_AFP_Afpinfo which may have been written after the file extension, e.g.

happybirthday.mp3._AFP_afpinfo 

or just

happybirthday_AFP_Afpinfo

I would like to delete the third type of file, i.e.:

happybirthday_AFP_Afpinfo  

so my question is whether this Unix command will delete all files that contain the characters AFP_Afpinfo?

find . -type -f 'AFP_Afpinfo' -delete

I tested

-find . -type -f -name 'epub' -print

and no files displayed.

I tried

-find -type -f -name 'epub' -print

and files displayed. So the use of the period after find was incorrect.

After running 1/2 hour or so, running:

find -type f -name '_AFP_Afpinfo' 

the following error was displayed:

find: './.Trash-1000/files': Input/output error

I checked that directory and it's full of AFP_Afpinfo files.

16
  • 3
    A good practice is to always run the find command without the -delete beforehand and see what files show up in the run (pipe to less if the list is big).
    – ojs
    Dec 12, 2020 at 15:47
  • | less +F? Could you suggest a proper syntax for less? unix.stackexchange.com/questions/197199/…
    – user26732
    Dec 12, 2020 at 15:53
  • Sorry, I wasn't clear enough. I meant run something like find . -type f -name EXPRESSION -print which just prints the name of the files, and if the list of files is long you can pipe it to less and review if you are finding any files you should not be finding and adjust your expression accordingly.
    – ojs
    Dec 12, 2020 at 15:58
  • 2
    So you want to find files whose names end in _AFP_Afpinfo but only where it is not preceded by a period? if so, you can use -name '*[^.]_AFP_Afpinfo' or if you prefer \( -name '*_AFP_Afpinfo' ! -name '*._AFP_Afpinfo' \) Dec 12, 2020 at 16:04
  • 1
    @ojs, technically -delete should be replaced with -depth -print, since -delete implies -depth whether you want it or not. (As a result I have found there are occasions with complex filters where -exec rm -f {} + is necessary instead of -delete, for example with -name … -prune)
    – roaima
    Dec 14, 2020 at 9:39

2 Answers 2

3

find . -type -f 'AFP_Afpinfo' -delete is not correct, or even valid - the AFP_Afpinfo would need to be preceded by either -name or -iname, and would then match files whose whole name matched exactly AFP_Afpinfo (case-insensitively, in the case of -iname).

If you want to find files whose names end in _AFP_Afpinfo but only where it is not preceded by a period, you can do so either as

find -type f -name '*[^.]_AFP_Afpinfo'

where [^.] means any character except . or using

find -type f \( -name '*_AFP_Afpinfo' ! -name '*._AFP_Afpinfo' \)

They are not exactly equivalent - the latter expression would match _AFP_Afpinfo whereas the former requires at least one non-period character before the string.

In either case, I strongly advise testing with -print first in place of -delete.

0

No it is not.

Tips:

test it

Try it with with -print in place of -delete.

. in file name is not special

The . is not special: it is just a regular character. In DOS and CP/M it is special, but you are not using these. In MS-Windows CMD globs ending in .* are special:

  • n.* does n
  • so n.*.* does n.*, and *.*.* does *.*
  • this all comes from the CP/M / DOS legacy, where *.* meant all files (all files had a . in them.
  • this culture is still about, and a lot of folk think that the . is special on Unix. It is not, it is just a character (except if it is the first character of a file-name).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .