Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 would like to delete every file, but keep the folder structure. Is there a way?

NOTE: (I'm using GNU bash 4.1.5).

share|improve this question
How exactly do you mean to "exclude"? Are you using a command of some sort? If so please update your question to include this. – slm Jul 24 '13 at 20:12
I think you mean delete every file. – slm Jul 24 '13 at 20:17
@slm I updated with the command I usually use to exclude files. – Tom Brito Jul 24 '13 at 20:18
up vote 5 down vote accepted

Try this:

find . ! -type d -exec rm '{}' \;

This will delete every single file, excluding directories, below the current working directory. Be extremely careful with this command.

If the version of find on your machine supports it, you can also use

find . ! -type d -delete
share|improve this answer
@slm: I'm not sure how portable that is. I've added it in as a secondary option. I just realized that my edit made my answer virtually identical to yours. Sorry about that; it was not my intent. – Evan Teitelman Jul 24 '13 at 20:20
Does the '{}' guard against spaces in files? I see you using all the time and I don't think I'd ever seen it used before I saw you posting solutions with it. – slm Jul 24 '13 at 20:21
From the GNU find manpage: "Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation." I'm not sure when it's necessary though. I mostly do it out of habit. The find command never sees the quotation marks. It just sees the {} marker as an argument. – Evan Teitelman Jul 24 '13 at 20:22
I see it on the example, but the templates show this, "-exec command {} +" & this "-exec command ;". I've use the \; in the past just never the '{}'. – slm Jul 24 '13 at 20:26
@slm: The templates also don't show the escaping of the semicolon, which is generally necessary when using a shell to execute find. I think the templates are meant to show which arguments the find command needs and don't worry about anything shell-related. – Evan Teitelman Jul 24 '13 at 20:28

You can use the command find to locate every file but maintain the directory structure:

$ find /some/dir -type f -exec rm {} +

Per this Unix & Linux Q&A titled: gnu find and masking the {} for some shells - which?, escaping the {} with single ticks (') doesn't appear to be necessary anymore with modern day shells such as Bash.

share|improve this answer

The easy way to delete every regular file in the current directory and subdirectories recursively:

zsh -c 'rm **/*(.)'

Only zsh has globbing qualifiers to match files by type. However, the rm command doesn't work on directories, so in bash, you can use

shopt -s globstar
rm **/*

This doesn't work for commands other than rm though. In general, you can use find:

find . -type f -delete

or if your find doesn't support -delete:

find . -type f -exec rm {} +
share|improve this answer

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.