I want to "clean out" all of the files a directory including all files in subdirectories but I want to leave the subdirectories in place. My understanding of rm -r is that it will also delete the subdirectories themselves.

I do not want to delete hidden (dot) files.

How can this be done?

  • 1
    find -type f ! -name "[.]*" -delete is not suitable? – Costas Jan 30 '15 at 14:54
  • I don't know. Does that do what I'm asking? – Scott Wilton Jan 30 '15 at 14:56
  • Try without -delete - it just print filenames to delete. – Costas Jan 30 '15 at 15:01
  • Do you want to delete files (hidden or not) in hidden directories? – Stéphane Chazelas Jan 30 '15 at 15:22
  • What about symlinks to directories? – Stéphane Chazelas Jan 30 '15 at 15:22

Use find for that:

find . ! -name '.*' ! -type d -exec rm -- {} +
  • 1
    Not all find implementations have -delete – wurtel Jan 30 '15 at 15:00
  • 2
    @RomeoNinov please read OP question: I do not want to delete hidden (dot) files. – jimmij Jan 30 '15 at 15:03
  • 1
    @Costas, no, there's no shell that requires escaping + (at least not in the Bourne, csh or rc families). There's a legend that says that some old versions of csh required escaping {} but I've never come across those. – Stéphane Chazelas Jan 30 '15 at 15:25
  • 1
    @ScottWilton Hidden directories are included, but hidden files in hidden directories not. If you want to reject all files in hidden directories then find . ! -name '.*' ! -type d ! -path '*/.*' should do the job. – jimmij Jan 30 '15 at 16:36
  • 1
    @jimmy, more like find . -name '.?*' -prune -o ! -type d -exec rm {} + to avoid descending in those altogether. Note that -- is not necessary here (doesn't harm though) since paths start with ./. – Stéphane Chazelas Jan 31 '15 at 20:52

try something like:

find <path> ! -name '.*' -type f -exec rm {} \;
  • Any reason why to remove the files one at a time instead of speeding things up with .... -exec rm {} + ? – Anthon Jan 30 '15 at 15:29
  • I prefer to do the things one by one. Mass (and parallel) operations have some time strange (and unpleasant) side effects (as make huge load on the I/O subsystem) – Romeo Ninov Jan 30 '15 at 15:32
  • 1
    -exec rm {} + is not parallel, it just calls rm with several file names. It runs fewer rm invocations so is more resource-friendly. If it slightly less safe in that it enlarges the race window within which a directory component of the files to remove may be changed to a symlink. But only -delete or -execdir rm {} \; would fully address that. – Stéphane Chazelas Jan 30 '15 at 15:41
  • @StéphaneChazelas, non of these (-delete or -execdir) exist in UNIX. And I do not see in question to be mentioned linux! – Romeo Ninov Jan 31 '15 at 14:04
  • @RomeoNinov, yes I said in another comment here that only -exec is standard. -delete and -execdir come from BSDs though, GNU added them about 10 years later. In any case, that has nothing to do with Linux, which is just a kernel. – Stéphane Chazelas Jan 31 '15 at 18:13

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.