Suppose I have these files in a directory.

├── __init__.py
├── __init__.pyc
├── __pycache__
├── recognizer.wsgi
├── requirements.txt
├── src
└── upld

In which I want to delete all but excluding the upld directory. Is there any quick and better way to do this, I wonder.

Thank You.

find . -path ./upld -prune -o ! -path . -print

This would use find to delete everything from the current directory, including subdirectories. However, the path ./upld is not deleted (note that this must be the full relative path of the thing you want to skip, from the initial search path).

Run this and check the output, then replace -print by -delete.

The safe option, from the parent of the directory in question:

mkdir newdir
mv olddir/upld newdir
rm -rf olddir
mv newdir olddir

That is, move the upld subdirectory out of the way for safe-keeping, then delete the old directory completely.


Assuming you are using bash, there at least to ways. I recommend you play with ls or echo first and when you are happy with the results you try rm.

1 explicit what you want to remove, for example:

ls -d [_a-s]*

2 explicit what you want to keep, for example:

shopt -s extglob # just once per session
ls -d !(upld)

You can find more info on bash pattern matching here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.