Skip to content
Advertisement

how to delete all files in directory except one folder and one file?

I have application which has one folder called vendor and one file called .env. When ever i automatically publish my source code files to folder, all old files should get deleted except these two.

How can i do this in linux by using shell?

PS : I am trying to implement rollback mechanism in Jenkins. I will copy artifacts from old build and transfer them to server using ssh. But this will be a copy operation. So I want to delete previous files before starting copy using SSH.

Advertisement

Answer

You can use find:

find ! ( -name 'name1' -o -name  'name2' ) -exec rm -r {} +
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement