I've this find script that finds files in home dir more than 100 MB of size and delete if it is more than 10 days old. It is scheduled for everyday one time by cron job. find /home/ -mtime +10 -type f -size +100M -delete >/dev/null 2>&1Now, I want this script to remove directories recursively from where it removes files. As it leaves empty dirs.Could anyone pls advice or suggest what change needs to be done in this script.
find script should remove empty dir recursively after it removes files in it
linux;find
On a GNU system, you could do:find /home/ -mtime +10 -type f -size +100M -delete -printf '%h\0' |  awk -v RS='\0' '!seen[$0]++ {out = out $0 RS}                  END {printf %s, out}' |  xargs -r0 rmdirWe use awk to filter out duplicate while still keeping the order (leaves before the branch they're on) and also delay the printing until all the files have been removed so rmdir can remove empty directories.With zsh:files=(/home/**/*(D.LM+100m+10od))rm -f $filesrmdir ${(u)files:h}Note that those would remove the directories that become empty after files are removed from them, but not the parent of those directories if they don't have any of those files to delete and become empty as a result of the directories being removed. If you want to remove those as well, with GNU rmdir, you can add the -p/--parents option to rmdir.If you wanted to remove all empty directories regardless of whether files or directories have been removed from them or not, still with GNU find, you could do: find /home/ \( -mtime +10 -type f -size +100M -o -type d -empty \) -delete