Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 6 down vote accepted

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 :).

share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.