LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-17-2010, 01:19 PM   #1
danndp
Member
 
Registered: Nov 2010
Posts: 51

Rep: Reputation: 2
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!
 
Old 11-17-2010, 01:27 PM   #2
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
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

Last edited by eco; 11-17-2010 at 01:30 PM. Reason: error in code
 
Old 11-17-2010, 08:39 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
I would warn on eco's second solution that if the names have spaces they will suffer word splitting in the for loop.
 
Old 11-18-2010, 01:35 AM   #4
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
Quote:
Originally Posted by grail View Post
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'
 
Old 11-18-2010, 02:10 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Or use a while loop and forgo the pain
 
Old 11-18-2010, 10:23 AM   #6
danndp
Member
 
Registered: Nov 2010
Posts: 51

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by eco View Post
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!!!
 
Old 11-18-2010, 10:47 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

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

https://fedorahosted.org/tmpwatch/

Last edited by i92guboj; 11-18-2010 at 10:49 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sort oldest 5 files in a directory tree recursively based on timestamp Linux abhelp Programming 1 06-04-2010 05:58 AM
BASH script that removes sub-directories (not just files) but keeps the two newest. kennc Linux - Newbie 2 04-23-2009 02:09 PM
Files in /tmp directory Yeung Solaris / OpenSolaris 4 01-01-2009 08:10 AM
Auto Delete oldest files @ratex when directory is 98% full jmanjohn61 Linux - Software 1 04-05-2005 04:44 PM
/tmp/ directory and Instlation files sckain Linux - Newbie 1 05-03-2004 10:20 PM


All times are GMT -5. The time now is 12:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration