How to recursively delete hidden .directory files on Linux

How to recursively delete hidden .directory files on Linux

Hidden files from file managers, thumbnail creation and applications can be very annoying, especially because they stay forever. In most ".gitignore" files at least the ".DS_Store" (MacOS finder) is listed, but what about all the other hidden Linux application files?

In a recent file system clean up, I noticed that a lot of folders are containing a .directory file. They were created by Dolphin, a KDE file manager, that I used almost 10 years ago.

[Dolphin]
Sorting=1
Timestamp=2010,12,16,20,25,48

Thankfully, getting rid of hidden files is extremely easy on Linux with find. Open up your terminal, navigate to the folder you want to check and do a search first:

$ find . -name '.directory' -type f
./directory
./subfolder/.directory
...

The find command has a different syntax than most other Linux commands. Written-out expressions just contain one dash and the structure is the following:

find path [expression]

You can not only search for files, but use all kind of search criteria, like size, modified date etc. This makes find a really powerful tool and it also has the ability to take actions, like delete, on the result.

Deleting Dolphins ".directory" files

Deleting files with find is really easy, just add the -delete expression to the search.

Warning: Always do a search first and be very careful with the wildcard (*) expression.

$ find . -name '.directory' -type f -delete
...

find can also be used for deleting other files too. Just replace .directory with the file name you want to delete. If you want to delete for example all ".tmp" files use `find . -iname '*.tmp' -type f -delete'.

Preventing Dolphin from creating .directory files

If you are still using Dolphin, you can also disable the .directory file creation, else the .directory file will be created once you navigate to the folder again.

In Dolphin, open the configuration ("Configure Dolphin") and navigate to "General" > "Behavior" and select "Use common properties for all folders".