Question

So far i have this script. my folder structure for now is /root/test/ inside test a folder gets created each month named May Jun July based on (date +%B)

i want the script to delete all sub directory's minus the directory that matches this months (date +%B) and keeping its contents.

currently it deletes everything apart from the sub directory matching. May is completely empty. any ideas?

#!/bin/bash

LinkDest=/root/test
m_date=$(date +%B)

find $LinkDest/ -not -name May -xdev -depth -mindepth 1 -exec rm -Rf {} \;
Was it helpful?

Solution

You can use:

find $LinkDest/ -not -path "*$m_date*" -xdev -depth -mindepth 1 -exec rm -Rf '{}' \;

OTHER TIPS

Try running the find without the -exec to see what's going to be removed. The problem is that -name tries to match the whole name, not a part of it. You need -path:

find -not -path "*/$m_date" -not -name $m_date
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top