Saturday, October 20, 2018

linux - Delete all files & directories in a folder except one file



I want to delete all files & directories in a folder say 'A'. But except one file in that folder say .keep. I have tried the following command.





find A ! -name '.keep' -type f -exec rm -f {} +




But above command also deletes folder A. I do not want that. There are several answers related to this. But they all mentions going into that directory. I want to mention that directory in the command without cd-eing into the directory.


Answer



find A ! -path A/.keep -a ! -path A -delete


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...