I need to make a daily cleanup in a download directory.

I have saw this question and tested the answer

rm !("test 1"|"test 4")

It work great, but require to have cd to the directory before. Since my app need to be two direcltory higher in many part, I d like to don t have to make something like

cd /app/download
rm !("test 1"|"test 4")
cd ../..

I have tried

rm !("./app/download/test 1"|"./app/download/test 4")

But well, it delete the content of the higher directory, not download/


1 Answer 1


You were almost there:

shopt -s extglob   # ← Only needed if you haven't enabled it yet
rm ./app/download/!("test 1"|"test 4")

The !(…) will be expanded with the full path to all files except the ones in !(…). Since you prefix the arguments to rm with the path ./app/download/, no other files higher than that directory will be deleted.


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.