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.

user26732
  • 103
  • 2
  • 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 '20 at 15:47
  • | less +F? Could you suggest a proper syntax for less? https://unix.stackexchange.com/questions/197199/is-there-any-way-to-exit-less-follow-mode-without-stopping-other-processes-in – user26732 Dec 12 '20 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 '20 at 15:58
  • The issue is having to shut down the process. It takes forever; the GUI run caused the loss of trash functionality, no more space on the root file system and I don't know what else. – user26732 Dec 12 '20 at 16:00
  • 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' \)` – steeldriver Dec 12 '20 at 16:04
  • @steeldriver make this into an answer, please. – user26732 Dec 12 '20 at 20:06
  • @user26732 done - thanks for the feedback – steeldriver Dec 12 '20 at 20:18
  • @user26732: When somebody asks you what your question means, please [edit] your it to make it clearer and more complete.  We like questions and answers that are useful to other people in the future; that requires them to be understandable.  Right now your question is very hard to understand.  Are you using "doc" and "docname" interchangeably?  I guess you are using "doc" and "docname" as a placeholder for a name like ``Dec12`` or `violet` — am I right?  That's not clear. … (Cont’d) – G-Man Says 'Reinstate Monica' Dec 13 '20 at 01:06
  • (Cont’d) …  And you say "Some of these files are in the format: ``.doc.apple.quarantine`` ... But others are more difficult because they have the phrase "_AFP_Afpinfo" which may have been written after the dot..."  Which dot?  Does that mean `._AFP_Afpinfodoc.apple.quarantine`, `.doc._AFP_Afpinfoapple.quarantine` or `.doc.apple._AFP_Afpinfoquarantine`?  Etc. – G-Man Says 'Reinstate Monica' Dec 13 '20 at 01:07
  • There are three types of files that were created by the recovery program and littered throughout the drive: – user26732 Dec 13 '20 at 06:46
  • There are three types of files that were created by the recovery program and littered throughout the drive:1. "_AFP_Afpinfo" 2. ".apple.quarantine" 3. "_Mac_Metadata" So, let's say I have a file "001.doc" On the hard drive, this file will spawn four files: 1. A perfectly readable 001.doc. 2. 001.doc_AFP_Afpinfo, 3. 001.doc.apple.quarantine 4. 001.doc_Mac_Metadata In my question, doc refers to Word files. – user26732 Dec 13 '20 at 06:52
  • I edited the question, hopefully it is more clear now. – user26732 Dec 13 '20 at 06:58
  • 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 '20 at 09:39
  • @roaima What does -depth do? Does it need a flag? Or -prune for that matter. – user26732 Dec 14 '20 at 15:17
  • `man find` and search for `-depth` – roaima Dec 14 '20 at 15:28
  • RTFM is always a solution. You suggested -depth, why? – user26732 Dec 14 '20 at 15:38

2 Answers2

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.

steeldriver
  • 78,509
  • 12
  • 109
  • 152
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).
ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102