linux - Script to remove all directories older than x days but keep certain ones -
I am trying to write a Bash script to remove all directories and their files but to keep some people .
drwxr-xr-x 20 Ubuntu admin 4096 Jan 21 17:58. Drwxr-xr-x 8 Ubuntu Administrator 4096 November 21 16:45 .. Droxax-XR-X11 Ubuntu Administrator 4096 9 January 9 13:09 1763 DvxR-XR-X11 Ubuntu Administrator 4096 Jan 16 16:46 1817 DWXR-XR - X 11 ubuntu admin 4096 Jan 16 17:39 1821 Droxer-XR-X11 Ubuntu admin 4096 January 1910:15 1823 Druxcer-XR-X11 Ubuntu admin 4096 January 1957 11:57 1826 DvxR-XR-X 11 Ubuntu admin 4096 January 19, 14:55 1827 Draw XX-XR-X11 Ubuntu Administrator 4096 Jan 19 21:34 1828 DroxsR-XR-X11 Ubuntu Administrator 4096 Jan 20 13:29 1833 DrwoxR-XR-X11 Ubuntu Admin 4096 January 20 16:13 1834 drwxr-xr- X 11 Ubuntu admin 4096 Jan 21 10:06 1838 Droxer-XR-X11 Ubuntu admin 4096 Jan 21 12:51 1842 Droxer-XR-X11 Ubuntu admin 4096 Jan 21 15:20 1845 DroxsR-XR-X11 Ubuntu admin 4096 January 22 13:00 1848 Drew X-XR-X11 Ubuntu Administrator 4096 November 24 16:34 217 Druxc-XR-X11 Ubuntu Administrator 4096 2 December 20:44 219 Druxx-XR-X11 Ubuntu Administrator 4096 15 Dec 16:42 221 Drxx-XR- X11 Ubuntu admin 4096 Dec 16 12:04 225 DrwoxR-XR-X2 Ubuntu Administrator 4096 January 20 16:10 AppConf LlvaxrWoxServeVir 1 Ubuntu Administrator 19 January 21 17:58 Latest - & gt; / Opt / qudiniapp / 1848 In the above example, we want to clear all non-simulated folders except the AppConff folder.
This deployment is planned before my responsible deployment script can be triggered so that we can keep our server from filling it with build.
provided, all the directories, which are to be deleted, are only included in the numbers, this will be the only way Fix it:
cd / tempdir rm -rf $ (search-type D-name "[0-9] *" | grep -v "$ (latest readlink)") < / Code> Because it is a housekeepingjob, you should make a chronboat, which removes old directories regularly, will be included in the search command, for example, if the The time of modification is beyond several days:
rm -rf $ (find. -type d -mtime +20 -name "[0- 9] *" | grep -v "$ ( Latest readlink) ")
Comments
Post a Comment