T
T
Tortway2014-08-24 20:41:20
linux
Tortway, 2014-08-24 20:41:20

How to delete everything in a folder except for a specific folder?

Hello, I need to delete all files and folders in a folder except for a specific folder , which also has files.
For example, there is a main folder general:

general{
    file-1
    file-2
    dir-1{
        file-1-1
        file-1-2
    }
    dir-2{
        file-2-1
        file-2-2
    }
}

What command on Linux can delete everything inside the general folder except for the dir-2 folder and its files?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tortway, 2014-08-24
@Tortway

cd /path/to/general; ls | grep -v 'dir-2' | xargs rm -rf

K
Konstantin Luntovsky, 2019-09-03
@Conekt

cd /path/to/general; rm -rf !(dir-2|dir-3)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question