Friday 8 March 2013

Deleting folders with false ownership


The following one liner script will list all those backups in /backup/serverbackup/home/ with false ownership.

for i in $(find /backup/serverbackup/home/* -maxdepth 0 -nouser); \
do \
grep ^$(basename $i): /etc/passwd &>/dev/null || echo $i; \
done

The following one liner script will list all those backups in /backup/serverbackup/home/ with false ownership and disk space used information of the same.

for i in $(find /backup/serverbackup/home/* -maxdepth 0 -nouser); \
do \
grep ^$(basename $i): /etc/passwd &>/dev/null || du -sh $i; \
done

The following one liner script will delete all those backups in /backup/serverbackup/home/ with false ownership.

for i in $(find /backup/serverbackup/home/* -maxdepth 0 -nouser); \
do \
grep ^$(basename $i): /etc/passwd &>/dev/null || rm -rvf $i ; \
done