2

I have a problem with my backup program which means that I need to delete all files and folders in a specific directory tree where the filename contains special characters, in my case the German ones such as öäüß and their capital equivalents.

I'd be quite happy to do this using the terminal but none of the commands I have tried has matched any of the files. Can anyone suggest a command I could use which would match these files?

For example, I've tried

find -name *ü*

however none of the files are matched. Any help would be appreciated.

3
  • Did you try to put the parameter in quotes: find -name ’*ü*' -print ?
    – nohillside
    Feb 13 '12 at 23:12
  • The finder and smart folders should allow you to select all files in a subfolder or volume and trash them in a graphical manner with great ease. Is there a reason why you must use terminal to perform this operation? (I suppose if the list of characters becomes more like 10 than 4 - that would be a decent reason now that I think of it)
    – bmike
    Feb 14 '12 at 0:11
  • 1
    @patrix, yes I've tried using quotes - this also matches no files or folders. Using the finder (@bmike) doesn't match any files or folders either. I believe it is due to the fact that I am searching for special characters, because when I search for normal characters, files/folders are matched in both cases.
    – Ham
    Feb 14 '12 at 9:29
1

A somewhat dangerous solution is this, from the commandline:

find . -type f ! -regex '.*/[ -.0-~]*' -exec rm {} +

Replace the lone . with the name of the top directory if you haven't changed to the relevant directory first. To be safe, however try first the shorter command

find . -type f ! -regex '.*/[ -.0-~]*'

and ensure that it only lists files you wish to delete. The regular expression (regexp, or regex) here will match any pathname that ends in a slash followed by any combination of printable ASCII characters excluding /, the space characters being the first such and ~ the last, while . and 0 surround / in the ASCII sequence.

One caveat among many: I don't know for sure if your current locale might change the collating sequence of characters, and hence perhaps change the meaning of the regexp. I don't think it does, but if it does, running the commands as

LC_COLLATE=C find …

should remove the danger.

Yet another caveat: Please ensure you have a backup before you try this. I will not take the blame for any loss of data if you get it wrong. The commandline is a great tool for shooting yourself in the foot! Sometimes just a misplaced space can spell disaster. (In this case, for example, missing the single space after the left bracket is deadly.)

1
  • Thanks very much @Harald, your regex worked a treat. In the end I used the following command: find . -type f ! -regex '.*/[ -.0-~]*' -delete after previously checking without -delete that the correct files were matched!
    – Ham
    Feb 15 '12 at 22:11
1

It seems that your problem is related to the difference of display of characters in both the terminal and the Finder. I'd suggest going into the directory where any of the files you want to remove resides and issue an ls to find if the filename in the UNIX environment matches that of the Finder's.

I find the above suggested solution a bit confusing. So my version of the find command would take two steps:

find . -name "*ü*" -print
This would print out the files that match.

find . -name "*ü*" -delete
This would delete the files that match after you check that none of the files you need are listed in the previous print out.

You should also take into account the various LC_* environment settings that are involved here, as per @Harald Hanche-Olsen, and what kind of effect it has on the find command. Not to mention the Terminal display encoding settings.

2
  • Thanks for the response @ismail, however these find commands did not seem to match my files unfortunately.
    – Ham
    Feb 15 '12 at 22:13
  • Did you check for file name consistency across Finder and Terminal.app?
    – ismail
    Feb 17 '12 at 2:27
0

You can try another way by using 'A better Finder Rename' (Link). It has the benefit of a Preview Pane, Presets and Multi-Step Actions. You can use the free Trial to experiment.

Screenshot

1
  • Thanks @J.C. looks like a good program, but didn't need it in the end!
    – Ham
    Feb 15 '12 at 22:23

You must log in to answer this question.

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