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