13

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?

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

2 Answers 2

11

Use find for that:

find . ! -name '.*' ! -type d -exec rm -- {} +
12
  • 1
    Not all find implementations have -delete
    – wurtel
    Jan 30, 2015 at 15:00
  • 3
    @RomeoNinov please read OP question: I do not want to delete hidden (dot) files.
    – jimmij
    Jan 30, 2015 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. Jan 30, 2015 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, 2015 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 ./. Jan 31, 2015 at 20:52
0

try something like:

find <path> ! -name '.*' -type f -exec rm {} \;
5
  • Any reason why to remove the files one at a time instead of speeding things up with .... -exec rm {} + ?
    – Anthon
    Jan 30, 2015 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) Jan 30, 2015 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. Jan 30, 2015 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! Jan 31, 2015 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. Jan 31, 2015 at 18:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .