Frage

I have a script that I run every week via a cron that delete loads of temp files on my web servers but I've just notice that the script is not removing the folders which are still taking up space.

Whats the best to amend the script to delete the folders as well?

find /my/path/ -type d –ctime +5 –exec rm -r {} \;

#!/bin/bash
# Deletes temp files older then 5 days
DOCS='/var/www/user/docs/temp'
SCRATCH='/var/www/user/temp/scratch'

if [ -d "$DOCS" ]; then
find $DOCSJS -type f -mtime +5 -exec rm -rf {} \; > /tmp/docs.txt
fi
if [ -d "$SCRATCH" ]; then
find $SCRATCH -type f -mtime +5 -exec rm -rf {} \; > /tmp/docs.txt
fi

My script actually has -rf as a flag to delete all folders.

One point to note is that the folders aboves are symbolic links to /mnt/sdb1/docs/js and /mnt/sdb1/temp/scratch - would the script follow the symbolic link or should I hard code this to the mount?

War es hilfreich?

Lösung

change this

if [ -d "$SCRATCH" ]; then
find $SCRATCH -type f -mtime +5 -exec rm -rf {} \; > /tmp/docs.txt
fi

into

if [ -d "$SCRATCH" ]; then
find $SCRATCH -type d -mtime +5 -exec rm -rf {} \; > /tmp/docs.txt
fi

you only search for files using the -type f flag; use -type d for directories

Andere Tipps

You need to pass additional options to rm commands in your scripts. Actually you need rm -rf. At all I'd recommend to go man 1 rm.

change this:

find $DOCSJS -type f -mtime +5 -exec rm

to:

find $DOCSJS -type f -mtime +5 -exec rm -Rf

Currently you cannot delete directories as the output of find is limited to files. You should drop this option so that both old files and directories are passed to rm -rf

find $DIR -mtime +5 -exec rm -rf {} \;

This will preserve old directories which have new files in them (but it will remove the old files), but get rid of files and directories where both are old.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top