0
meridianjumper

Linux shell scripting question.

Recommended Posts

you probably want something like "rm -v filename >> logfile.txt"

The -v (verbose) option should force rm to list the files that it is deleting. The >> appends the ouput to the named file. If that doesn't work, try "rm -v filename 2>> logfile.txt"

Share this post


Link to post
Share on other sites
The company I work for has had a problem with an update file that is sent to it via cron job. However there is not a process yet that will remove the file after it is installed. I am building a script that I will use as a cron job and have it delete the files that I am wanting to delete because they can be a rather large hog on hard drive space. I just want to make sure with a log file that I can see what files were deleted just as a way of checking it was done.


Blue Skies,
Jeremy

Share this post


Link to post
Share on other sites
Try this:



#!/usr/bin/perl


@file_list = ('/tmp/fileone.txt', '/tmp/filetwo.txt', '/tmp/file3.txt');
$LOG = "/tmp/file_deletions.log";

$date = `date`;
chomp $date;

foreach $file (@file_list) {

if(-e $file) {
`rm $file`;
open(FILE, ">> $LOG") or die "Cannot open $LOG.\n";
print FILE "$date - Deleted $file\n";
close(FILE);
}
}



Change file_list and LOG to be what you want, chmod 700 the script and set cron to run it.

Share this post


Link to post
Share on other sites
Hahaha, classic!

But am I the only one who thinks that shell scripting means scripting done with a shell? Linux comes with several, and at least one of them is good for that stuff :P
--
"I'll tell you how all skydivers are judged, . They are judged by the laws of physics." - kkeenan

"You jump out, pull the string and either live or die. What's there to be good at?

Share this post


Link to post
Share on other sites
Quote

I am fairly new to Linux and I am writing a shell script and I want it to delete a few files that are recurring. I want to be able to have this script send the output of what was deleted to a log file. Any ideas?

Usually I would just do something similiar to:

rm -v filespec 2>&1 >~/.rmlog.txt

Basically it outputs both STDOUT and STDERR, and put the logfile into a separate directory, such as my home directory. Add -r and -f options as necessary (Danger, Danger, Will Robinson.)

The "2>&1" is shell specific though, this varies from shell to shell. (It redirects STDERR to STDOUT. STDERR being used for error messages, and STDOUT used for regular messages)

Share this post


Link to post
Share on other sites
Right, you make sure you are using bourne or korn shell if using that redirection syntax.

Why not just symlink the files to /dev/null? I assume you are concerned about them growing and filling up whatever volume they are on. That way, whatever program is opening them, and writing to them, can do so without error, but the results will end up in the bit bucket. That way, there is no fancy script to run from cron.

Share this post


Link to post
Share on other sites
Too C-ish. :)



#!/usr/bin/perl
@files = ('/file1', '/file2');
$log = 'logfile.log';
$date = `date`; chomp($date);

open(LOG, ">>$log") or die "blah\n";

map {
unlink($_) if(-e $_);
print LOG "$date Deleted $_\n";
} @files;

close(LOG);

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

0