15

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

  • 1
    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
  • 1
    @slm I updated with the command I usually use to exclude files. – Tom Brito Jul 24 '13 at 20:18
  • No you haven't :-) Or, if you did, you removed it again – Mawg Oct 24 '16 at 9:43
17

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
  • @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. – user26112 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. – user26112 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. – user26112 Jul 24 '13 at 20:28
7

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.

1

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 {} +
-2

I had similar requirement to delete files from a path and its sub directories (filtering by time ) with out deleting the directory structure .

And i have used the below format which worked for me .

find /test123/home/test_file_hip/data/nfs -mtime +6 -type f -exec rm {} \;

Syntex : find (path of file) -mtime (greater than or less than days) -type f -exec rm {} \;

-type : Mention the type of file "f" for "d" directory -exec : execute command rm : remove {} : output of find command

Note : Do test it before using it . Please feel free to correct or update if i missed anything .

  • 1
    since this is an additional requirement that wasn't mentioned by the OP, and since the basic find ... rm structure has already been covered, I'm not sure this is a valuable contribution as a new Answer to this question. – Jeff Schaller Nov 2 '18 at 14:38

Your Answer

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

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