Having the Busybox ash
shell at hand, I wish to cleanup the /opt
folder.
Cleanup means to remove every file and folder recursively except for a few exclusion paths that need to be left untouched.
Regression rm
# cd /opt && echo rm -rf \
./!(opt/etc/nginx|/opt/home|/opt/etc/config|/opt/usr/sbin|/opt/var/mlocate)
-sh: syntax error: unexpected "("
Regression find
This seems to do the job:
# cd /opt && find ./ ! -path "./etc" ! -path "./etc/config" ! \
-path "./etc/config/*" ! -path "./etc/nginx" ! -path "./etc/nginx/*" ! \
-path "./home" ! -path "./home/*" ! -path "./usr" ! -path "./usr/sbin" ! \
-path "./usr/sbin/*" ! -path "./var" ! -path "./var/mlocate" ! \
-path "./var/mlocate/*" -exec rm -f {} \;; cd -
Any suggestion how to improve?
Use -delete
instead of -exec rm -f {} \
?
Handle rm: can't remove '.' or '..'
?
How to delete the empty directories without recursion rm -rf
, in other words how to f.e. integrate rmdir
?
Note: it's Busybox 1.24.2 and there is no shopt
neither tac
available.
Defined functions:
[, [[, addgroup, adduser, ar, arping, ash, awk, base64, basename, blkid, blockdev, brctl, bunzip2, bzcat,
cat, chgrp, chmod, chown, chroot, clear, cmp, cp, crond, crontab, cryptpw, cut, date, dd, delgroup, deluser,
devmem, df, diff, dirname, dmesg, dos2unix, du, echo, egrep, env, expr, false, fgrep, find, free, fsync,
getty, grep, gunzip, gzip, halt, head, hexdump, hostid, hostname, id, ifconfig, insmod, iostat, ip, ipaddr,
ipcalc, iplink, ipneigh, iproute, iprule, iptunnel, kill, killall, klogd, less, ln, lock, logger, login,
losetup, ls, lsmod, lsusb, md5sum, mkdir, mkfifo, mknod, mkswap, mktemp, more, mount, mv, nc, netmsg,
netstat, nice, nslookup, ntpd, od, passwd, patch, pgrep, pidof, ping, ping6, pivot_root, poweroff, printf,
ps, pwd, readlink, reboot, renice, reset, rev, rm, rmdir, rmmod, route, run-parts, sed, seq, setconsole,
setserial, sh, sha1sum, sha256sum, sha512sum, sleep, sort, start-stop-daemon, stat, strings, stty, su,
swapoff, swapon, switch_root, sync, sysctl, syslogd, tail, tar, tee, telnet, telnetd, test, time, top, touch,
tr, traceroute, true, tty, udhcpc, umount, uname, uniq, unix2dos, unlink, unlzma, unxz, unzip, uptime,
usleep, vconfig, vi, wc, wget, which, xargs, xz, xzcat, yes, zcat
-delete
orrm -f {}\
? Write all parent paths or shorten it to the longest path and ignore that non-empty directories can't be deleted? – Pro Backup Sep 06 '18 at 23:42