There is a folder, "transfer". In that transfer folder there are user folders "user1", "user2", etc. I want to (periodically) delete the content (i.e. all files and folders in the user folders) but I do not want to delete the "transfer" or user folders.

How can I do that using as shell script/command without manually adding a call for each new user folder every time I add a new user?

3 Answers 3


You can do it using following find command:

find /path/to/transfer -mindepth 2 -delete

-mindepth 2 parameter tells find to ignore first two level of directories: searched directory itself, and all files and folders that are directly in it.

-delete parameter just simply tells find to delete all files.

You can always add more parameters (for example -mtime) according to your needs.

  • 3
    +1 for mentioning -mtime. I would not periodically delete all files under such a directory structure. I would only delete files over a certain age, to avoid taking out a file someone had just put there and hadn't finished transferring to its final destination. Nov 13, 2019 at 19:32

A failed answer:

rm -R transfer/user*/.


$ rm -R transfer/user*/.
rm: refusing to remove '.' or '..' directory: skipping 'transfer/user1/.'

Eliminates the contents of all of the users, but leaves empty user* directories. Although the "dot"/"period"/"current directory" won't be removed, it can be used as a starting point for recursive deleting under that location.

Update: Unfortunately, I need to retract any recommendation on using this answer. I actually did test this before posting, but upon hearing that it didn't work, I re-tested and it failed. Simply, nothing got deleted. Somehow, my earlier test must have been done incorrectly. I did find a way to handle this, but decided the update was so significantly different that the existing comments of this answer would not match the update well at all, so I posted the other (again, tested) solution as a different answer.

  • It's ok as soon as all users actually named user1, user2, user3. I suppose it was just example and each user has its own very unique name.
    – mrc02_kr
    Nov 13, 2019 at 8:21
  • 1
    @mrc02_kr rm -R transfer/*/.
    – TOOGAM
    Nov 13, 2019 at 8:25
  • 1
    @TOOGAM Sorry, doesn't work for me. It gives the mentioned "refusing..." error message for each user folder and does not delete anything. Nov 13, 2019 at 17:46
  • 1
    This command will never likely succeed without the -f flag to rm - but then that removes the user* directories which is not wanted. @Silicomancer I believe the . at the end is extraneous, putting a * in its place accomplishes the objective. rm -R transfer/user*/*
    – shalomb
    Nov 13, 2019 at 20:59
  • @shalomb - will that delete a folder named .ssh? (transfer/user*/* won't glob to any transfer/user*/.*)
    – TOOGAM
    Nov 15, 2019 at 22:35

for x in transfer/user* ; do cd $x ; echo rm -R $( ls -1a | grep -v ^./$ | grep -v ^../$ ) ; cd ../.. ; done

Verify this looks very good before re-running the command without the word echo.

I would wish to thoroughly test this before relying on it. I recall an instance where something like this led to .. matching something to delete (recursively), causing the unwanted incident of a larger amounts of data being lost. In particular, if the cd $x fails (maybe due to a permissions issue), but then the cd ../.. works, and then rm -R runs on a higher-level directory, then you could easily end up recursively deleting from the wrong area. That's why I would be extremely hesitant to blindly trust this until it has been well-proven.

This also worked on one system I tested it on, which was using bash on Debian. On that system, ls -1a showed the . and .. directories with a slash after them. If you have a different ls command or a shell which acts different, then this may need some adjustment.

So, I definitely recommend considering whether it is feasible to do a manual review before proceeding. (And, of course, make sure you have suitable backups ready to go. I recall hearing of a situation where somebody had a delete-user script which removed /users/$1 but then somebody else ran that script without parameters, hoping to be shown syntax. Data from all users started being deleted. Fortunately, the staff member who did this was surprised at how long the command was taking, so he sought help, and backups were readily available and able to be successfully utilized.)

You must log in to answer this question.

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