Deleting folders with false ownership.

One day I have faced a situation regarding space in backup drive. On verification I found that there are many backups with false ownership and those backups with false ownerships are of users who are deleted from the system in past. So I decided to write a one liner by which can delete/remove all those backups.

One liner:

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

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

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

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

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

1
2
3
4
for i in $(find /backup/serverbackup/home/* -maxdepth 0 -nouser); \
do \
grep ^$(basename $i): /etc/passwd &>/dev/null || rm -rvf $i ; \
done
Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: