Shell Script to delete files from 5 different servers

Hello,

I'm new to shell scripting and need a quick note on how to write a shell script to perform deletion of files from 5 different hostnames in various locations.

Found out to delete files from one path by using below command and made it to work on cron job but need to do it in a shell script that should run on 5 servers at different file locations.

find /path/to/files -type f -name "*log*" -mtime +10 -print -delete

But here's is the trick there are files in different locations on each server. So how can i write a script to do in a single shot.
And each server got different hostname.
Thought of case could do me a favor.And ran out of ideas.

Please help me out.

Thanks,
Teja G

Welcome to the forum.

If you want more than just a few generic hints what could be done, your specification needs a way more precise problem description, and more details like input data & structure, network connectivity, servers' directory structures & file locations, dand possibly more.

Maybe some good starters are the links at the bottom left of this page under "More UNIX and Linux Forum Topics You Might Find Helpful".

1 Like

@RudiC

So here are the file locations where i needed my files to be deleted on my servers.
Static Servers - /app/logs/httpd
Login Servers - /app/login/jetty/logs
Oauth Serves - /app/oauth-v2/jetty/logs

I did created and wrote for static servers as following

find /app/logs/httpd -type f -name "*log" -mtime +10 -print -delete

And there are 2 servers in the static group. So placed the script in cron job and its running fine on every Sunday 00:00 hrs.

But need the script attaching the above with Login and Oauth servers at the location specified in the same cron job. So it can run on static,login and oauth serves at same time using the same script.

So please guide me where to start from and which will be easier."Case, if...else, while or for"

Thanks,
Teja

---------- Post updated at 06:26 PM ---------- Previous update was at 06:01 PM ----------

The whole goal of mine is writing a puppet module to run the cron job on Every sunday, using the shell script to clear logs on servers.

The servers are as follows:
Static Servers - 5 with hostnames as static-1p.xyz.com; static-2p.xyz.com; static-3p.xyz.com; static-4p.xyz.com; static-5p.xyz.com

Logs stored in static servers at /app/logs/httpd

Login Servers - 5 with hostnames as login-1p.xyz.com; login-2p.xyz.com; login-3p.xyz.com; login-4p.xyz.com; login-5p.xyz.com

Logs stored in login servers at /app/login/jetty/logs

OAuth-v2 Servers - 5 with hostnames as oauth-1p.xyz.com; oauth-2p.xyz.com; oauth-3p.xyz.com; oauth-4p.xyz.com; oauth-5p.xyz.com

Logs stored in Oauth servers at /app/oauth/jetty/logs

So now I need to delete these logs by using a shell script that can place in my cron job weekly dir and i'll specify that script to run every sunday at 00hrs.

And here is my puppet code that does the job.

class clearlogs {
        file {'/etc/cron.weekly/':
              ensure  => present,
              mode    => '0775',
              owner   => 'root',
              group    => 'root',
              source   => 'puppet:///modules/clearlogs/clearlogs.sh',
        }

        cron { 'clearinglogs':
                command   => 'sh /etc/cron.weekly/clearlogs.sh
                user           => 'root',
                weekday     => 'Sunday',
                hour           => '0',
                require       =>  File['/etc/cron.weekly/clearlogs.sh']
        }

}

And my clearlogs.sh looks like this and need to add the script for clearing logs on each server with this shell script

#!/bin/bash
echo "Deleting files in the location older than 10 days from now"

find /app/logs/httpd -type f -name "*log*" -mtime +10 -print -delete

echo "Successfully cleared logs!!!"

This is only for one server static server, but i'm trying to write/add the lines to clear logs on other servers too in the same script.
So, please help me out or guide me with a roadmap where to start with and with what specific arguments i can accomplish my task to be successful...

Thanks,
Teja