CHECK SCRIPT SYNTAX

Hi everyone, i'd like someone chechk this script, i know it's very simple but it doesn't work good, let me tell you this script works over huge directories, maybe that's the problem, if somebody can give me other way to develop, or show me where it's the problem, i'll appreciate it.

PATH_ROOT="/ot/bean/923/domains/scape"

find $PATH_ROOT/servers/scape-admin/logs/*.log* -mtime +7 -exec rm {} \;
find $PATH_ROOT/servers/scape-services-ru/logs/*.log* -mtime +7 -exec rm {} \;
find $PATH_ROOT/servers/scape-services/logs/*.log* -mtime +7 -exec rm {} \;
find $PATH_ROOT/servers/scape-gui/logs/*.log* -mtime +7 -exec rm {} \;
find $PATH_ROOT/servers/scape-gui-ru/logs/*.log* -mtime +7 -exec rm {} \;

This may be due to the ARG_MAX... every server (OS) has its own kernel limit set to argument length if it goes beyond that (that might be the case your rm command facing)..

read below link..

How long can the command line be?

1 Like

i'm working with Red Hat 5 Enterprise, i understand what you mean, but how i can find out what to? My boss ask me for some script which do that, it was very simple, but i just a beginner.
Could you help me?

Do you have to scan directory and all subdirectories within?
If you are only removing old files at ONE level, consider using -prune option of find

1 Like

try checking XARGS man page might be usefull

1 Like

Putting the too many arguments into find hasn't stopped them from being too many arguments. You need to not give them too many arguments.

Let find handle the pattern matching. That way, you only give it one argument.

You can give find more than one folder, too, and handle it all at once.

PATH_ROOT="/ot/bean/923/domains/scape"

find    "$PATH_ROOT/servers/scape-admin/logs/"             \
        "$PATH_ROOT/servers/scape-services-ru/logs/"       \
        "$PATH_ROOT/servers/scape-services/logs/"          \
        "$PATH_ROOT/servers/scape-gui/logs/"               \
        "$PATH_ROOT/servers/scape-gui-ru/logs/"            \
        -name '*.log*' -mtime +7 -exec echo rm '{}' '+'

-name can match files like you were doing inside the shell, without having to match inside the shell, avoiding the 'too many arguments' problem. Putting it in single quotes prevents the shell from trying to expand it too soon.

Using '+' instead of ';' will cause it to put as many files as it can into each rm call instead of running rm 10,000 individual times for 10,000 individual files, which will make it more efficient.

Remove the echo once you've tested and are sure it does what you want.

2 Likes

Hi Corona, as usually, your solution are the best, i got the idea, but your way produce me others doubts, for example the "\" with that spaces at the end of each line, for what are they?
The other question, if i use this code just like you put it on, show, or print me, it works?
Just one line to delete at the end?
And the last one, why should i remove the echo?

PATH_ROOT="/ot/bean/923/domains/scape"

find    "$PATH_ROOT/servers/scape-admin/logs/"             \
        "$PATH_ROOT/servers/scape-services-ru/logs/"       \
        "$PATH_ROOT/servers/scape-services/logs/"          \
        "$PATH_ROOT/servers/scape-gui/logs/"               \
        "$PATH_ROOT/servers/scape-gui-ru/logs/"            \
        -name '*.log*' -mtime +7 -exec echo rm '{}' '+'   ...just this delete?

It lets you break one long line across several lines to make it easier to read and edit. It's the same as if it'd been crammed all onto one big line.

For example,

$ echo a \
b \
c

a b c 

$

When you run it, it should print things like rm file1 file2 file3 file4 ... to the screen.

It's not running rm file1 file2 file file4 , it's running echo rm file1 file2 file3 file4 which prints to the screen.

No, that line is important, it tells it what to do.

Because with the echo, it will run echo rm filename , printing filenames to the screen for you to check if it's finding the right files.

Once you remove the echo, it will be running rm filename , and actually trying to remove the files.

PATH_ROOT="/ot/bean/923/domains/scape"

find    "$PATH_ROOT/servers/scape-admin/logs/"             \
        "$PATH_ROOT/servers/scape-services-ru/logs/"       \
        "$PATH_ROOT/servers/scape-services/logs/"          \
        "$PATH_ROOT/servers/scape-gui/logs/"               \
        "$PATH_ROOT/servers/scape-gui-ru/logs/"            \
        -name '*.log*' -mtime +7 -exec echo rm '{}' '+'
1 Like

Corona.....Done!!! i really want to thanks you!!!!:D:D:D:D:D:D

---------- Post updated at 05:46 PM ---------- Previous update was at 05:45 PM ----------

Vidyadhar85
:wink:
Hi, could you show me how can i use this comand for with my original code?
I mean xargs comand.
Thanks!!!

1 Like

xargs transforms text into arguments. These two commands are equivalent, for instance:

cat a b c
echo a b c | xargs cat

If you leave -exec off of find it just does its default action instead, which is, print every filename on a separate line. If you feed that into xargs commandname, it will run commandname with the filenames as arguments. If it's too many filenames to fit into one call, it will run commandname several times.

So, find without xargs:

find ... -exec something '{}' '+'

find with xargs:

find ... | xargs something

The version with -exec is preferable if you can, because spaces and quote-characters in filenames and paths can confuse xargs. -exec has no such problem, to the point that find is a useful tool for removing files which somehow ended up with humanly untypable names.