LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 04-28-2007, 01:49 PM   #1
DavidHB
Member
 
Registered: Feb 2006
Location: Chile
Distribution: Slackware 10.2
Posts: 53

Rep: Reputation: 15
Need script for cron to erase MT files


Hi,

I need a script (bash) for cron.hourly to erase empty file in a specific directory.

Help please.

Thx
dave
 
Old 04-28-2007, 04:16 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
#!/bin/bash
directory=/mydir
for file in `ls $directory`
do
if [ ! -s $directory/$file ]
then rm $directory/$file
fi
done

Replace "mydir" with the name of the directory that has the file. The above script would test all files to see if they are more than 0 bytes. Any that were not (and therefore "empty" would be deleted.

If you know the file always has the same name it would be safer to simply use that name and get rid of the for, do and done lines. Change "$file" to the real file name.

I tested this on /root after creating an empty file there with touch. I'd strongly suggest you do a test of a non-critical directory before you try it to verify it works. Remember this and all advice here is without any warranty (real or implied) and should be tried at your own risk.
 
Old 04-29-2007, 09:04 AM   #3
DavidHB
Member
 
Registered: Feb 2006
Location: Chile
Distribution: Slackware 10.2
Posts: 53

Original Poster
Rep: Reputation: 15
Thx jlightner,

I had to modify the sript slightly to get it past the syntax screens, perhaps different verions of bash I don't know.

Changes in lines 2 and 3

Mine came out as below:

#!/bin/bash
directoy='/mydir'
for file in $(ls $directory)
do
if [ ! -s $directory/$file ]
then rm $directory/$file
fi
done

But works like a charm

Thanks
 
Old 04-29-2007, 01:00 PM   #4
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
Glad I could help.

By the way it should have worked as written (and did for me). However, whatever works is always the "right" way to do it. Technically the way you did it is the "new" way and the way I did it is the "old style" (according to bash man page).

I wonder if perhaps you had an issue on the following line:

Code:
for file in `ls $directory`
Note that the character here is the backtick (`) rather than the single quote ('). Back tick usually being the lower case symbol on the same key as the tilde (~). Single quote being the the lower case on the same key as the double quote ("). Backtick is a/k/a "accent grave or backquote".

If you used the single quote instead of backtick you would have had an issue.

I doubt there is a version of bash that doesn't use the backtick special meaning. That would be a major change to functionality requiring rewrite of many scripts.
 
Old 04-30-2007, 07:51 AM   #5
DavidHB
Member
 
Registered: Feb 2006
Location: Chile
Distribution: Slackware 10.2
Posts: 53

Original Poster
Rep: Reputation: 15
Interesting

Thanks again,

Interesting tid-bits.

Suppose that's the next path IŽll be going down.... the 'scripts' path. Or would that be the `scripts` path.

Just another fork() in the road
Dave
 
  


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
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 09:09 AM
Erase files madmax1143 Linux - Software 5 08-05-2006 06:23 PM
software to erase corrupted files? karl_marx Linux - Software 1 05-22-2006 09:16 PM
Script creats temporary files... Cron won't run it spiffytech Linux - Software 5 01-11-2006 11:03 AM
How do you erase files in Terminal? darkarcon2015 Linux - Software 1 06-09-2004 06:56 PM


All times are GMT -5. The time now is 05:27 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration