I have a directly called /cache which has about a million files.

I restructured my caching system to put cache files into different folders, inside the /cache dir. new cache files are already being written to those sub-folders. So I want to remove all the files inside the /cache dir that are not in any subfolder.

How would I do this in SSH?


You can do so with the find command.

find /cache -maxdepth 1 -type f -exec rm -rf {} \;

I suggest to take a look to the list of files which would be deleted before executing find in combination with -exec rm:

find /cache -maxdepth 1 -type f

This will give you the list of files without deleting them. Again; use find in combination with -exec rm with caution, I can't be clear enough :).


You want to remove all files in cache folder without removing files in directory in cache folder ? If it is, you can use find : find /cache -maxdepth 1 -type f -delete -maxdepth limits the depth and don't remove files in folders

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy