LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Creating a script that removes the oldest files on /tmp directory (https://www.linuxquestions.org/questions/linux-newbie-8/creating-a-script-that-removes-the-oldest-files-on-tmp-directory-844880/)

danndp 11-17-2010 12:19 PM

Creating a script that removes the oldest files on /tmp directory
 
Hello friends,

I'm trying to configure a script the deletes the file and directories with more than 10 weeks on my /tmp directory, this is what i'm trying to do:

Quote:


#!/bin/bash

#Script para borrado de ficheros y PDFs de /tmp

### Directorios/PDFs a borrar
DIRfaxjob="/usr/bin/find /tmp/faxjob* -mtime +70"

if `$DIRfaxjob`
then
result=$?
case $result in
0) /usr/bin/find /tmp -mtime +70 -exec rm -f {} \; ;;
*) echo_error "=> No existen archivos con mayor antiguedad de 10 semanas;;
esac
else
echo Intentalo luego
fi


Is this OK? i think i'm having problems with my "result=$?"

could you please let me know what can i do to make this more logical?

THANKS!

eco 11-17-2010 12:27 PM

Hi,

Why would you go through the if when you are using the simplest command in the if?

/usr/bin/find /tmp -mtime +70 -type f -name 'faxjob*' -exec rm -f {} \;

done!

or do something like:

Code:

#!/bin/bash

#Script para borrado de ficheros y PDFs de /tmp
for i in $(/usr/bin/find /tmp -type f -name 'faxjob*' -mtime +70); do
  rm -f $i
done


grail 11-17-2010 07:39 PM

I would warn on eco's second solution that if the names have spaces they will suffer word splitting in the for loop.

eco 11-18-2010 12:35 AM

Quote:

Originally Posted by grail (Post 4162503)
I would warn on eco's second solution that if the names have spaces they will suffer word splitting in the for loop.

Yup, that's always a pain. Thanks for pointing it out.

You could add the following before the for loop to fix that problem:

Code:

IFS=$'\n'

grail 11-18-2010 01:10 AM

Or use a while loop and forgo the pain :)

danndp 11-18-2010 09:23 AM

Quote:

Originally Posted by eco (Post 4162133)
Hi,

Why would you go through the if when you are using the simplest command in the if?

/usr/bin/find /tmp -mtime +70 -type f -name 'faxjob*' -exec rm -f {} \;

done!

or do something like:

Code:

#!/bin/bash

#Script para borrado de ficheros y PDFs de /tmp
for i in $(/usr/bin/find /tmp -type f -name 'faxjob*' -mtime +70); do
  rm -f $i
done


Thanks for your help my friend...finally and based on your help, i did this:

Quote:

#!/bin/bash

DAYS=90

# Mover /tmp/*.pdf y /tmp/faxjob_*/ a /var/tmp/fax/
set +e
mv /tmp/faxjob_* /var/tmp/fax/
mv /tmp/*.pdf /var/tmp/fax/

# Limpiar /var/spool/hylafax/pdfrec/*.pdf
set -e
find /var/spool/hylafax/pdfrec/ -name "*.pdf" -type f -mtime +$DAYS -exec rm {} \;

# Limpiar /var/tmp/fax/*/
set -e
find /var/tmp/fax/ -name "*" -mtime +$DAYS -exec rm -rf {} \; 2>/dev/null
Thanks!!!

i92guboj 11-18-2010 09:47 AM

You might want to take a look at tmpwatch, it fulfils the same purpose. :)

https://fedorahosted.org/tmpwatch/


All times are GMT -5. The time now is 12:54 AM.