Move all .log except those generated in the last 5 minutes

RHEL 5.8
In the directory /u03/pkms/app_logs I have several hundreds of log files as shown below.

$ pwd
/u03/pkms/app_logs

$ ls -alrt *.log | tail -50
-rw-r----- 1 oracle dba  9439232 May  4 13:57 mvtpcem_1_722892404_94157.log
-rw-r----- 1 oracle dba  9227264 May  4 13:57 mvtpcem_1_722892404_94158.log
-rw-r----- 1 oracle dba  9216000 May  4 13:57 mvtpcem_1_722892404_94159.log
-rw-r----- 1 oracle dba  9056256 May  4 13:57 mvtpcem_1_722892404_94160.log
-rw-r----- 1 oracle dba 10231296 May  4 13:57 mvtpcem_1_722892404_94161.log
-rw-r----- 1 oracle dba  9932800 May  4 13:57 mvtpcem_1_722892404_94162.log
-rw-r----- 1 oracle dba 10440192 May  4 13:57 mvtpcem_1_722892404_94163.log
-rw-r----- 1 oracle dba 10412032 May  4 13:57 mvtpcem_1_722892404_94164.log
-rw-r----- 1 oracle dba  9217024 May  4 13:57 mvtpcem_1_722892404_94165.log
-rw-r----- 1 oracle dba 10384384 May  4 13:57 mvtpcem_1_722892404_94166.log
-rw-r----- 1 oracle dba  9237504 May  4 13:57 mvtpcem_1_722892404_94167.log
-rw-r----- 1 oracle dba  9022464 May  4 13:57 mvtpcem_1_722892404_94168.log
-rw-r----- 1 oracle dba  9708032 May  4 13:57 mvtpcem_1_722892404_94169.log
-rw-r----- 1 oracle dba  9963008 May  4 13:57 mvtpcem_1_722892404_94170.log
-rw-r----- 1 oracle dba 10061312 May  4 13:58 mvtpcem_1_722892404_94171.log
-rw-r----- 1 oracle dba 10278400 May  4 13:58 mvtpcem_1_722892404_94172.log
.
.
.
<snipped for readability>
.

-rw-r----- 1 oracle dba   304128 May  4 14:13 mvtpcem_1_722892404_94199.log
-rw-r----- 1 oracle dba  1370112 May  4 14:21 mvtpcem_1_722892404_94200.log
-rw-r----- 1 oracle dba   337920 May  4 14:25 mvtpcem_1_722892404_94201.log
-rw-r----- 1 oracle dba    18944 May  4 14:26 mvtpcem_1_722892404_94202.log
-rw-r----- 1 oracle dba     5632 May  4 14:26 mvtpcem_1_722892404_94203.log
-rw-r----- 1 oracle dba     1024 May  4 14:26 mvtpcem_1_722892404_94204.log
-rw-r----- 1 oracle dba     1024 May  4 14:26 mvtpcem_1_722892404_94205.log
-rw-r----- 1 oracle dba     1024 May  4 14:26 mvtpcem_1_722892404_94206.log

I want to move all .log files to another location /u05/nmbs/may13, but skip those .log files which was
generated within the last 5 minutes. How can I do this ?

Hii

Below command will help you in this task.

find /u03/pkms/app_logs/*.log -mmin +5 | xargs -I '{}' mv {} /u05/nmbs/may13/ 
1 Like

Thank you Sod.
Do you prefer xargs over -exec ?
What is -I option in xargs ? Couldn't find the details about xargs in find's man page.

Because it's in the xargs man page :wink:

1 Like

Oh Ok. I just noticed the pipe symbol. Thank you Scott, Sod

Do you prefer xargs over -exec for doing things within find command?

Is there a term for 'doing things within' another in command in shell scripting terminology ?

Generally, I strongly prefer find -exec over xargs. It can process any valid filename with less overhead (-exec ... {} +) than xargs.

People often treat xargs as if it can handle any type of filename, but that is not the case. Depending on the options in effect, quotes, whitespace, and a logical EOF character can be mishandled. To be fair, in practice, these limitations are seldom relevant when dealing with local, UNIX files. However, when handling files from other filesystems, whitespace isn't uncommon. When dealing with p2p files, even quotes aren't unheard of.

Piping to xargs also adds overhead:
(1) Data needs to be copied back and forth between kernel and userspace twice as often.
(2) If find and xargs must run on the same CPU, more context switches are required.

Unless some xargs functionality is required, I would not use it.

Regards,
Alister

I agree with Alister.

Hi Kraljic,

Alister is correct, exec will be sufficient for this job with find which required less CPU. Since you may run this command mutiple times in day to move the files it can slightly affect the system performance. So please see the modified one.

find /u03/pkms/app_logs/*.log -mmin +5 -exec mv {} /u05/nmbs/may13/ \;

what does the below code do??

xargs -I '{}' mv {} /u05/nmbs/may13/

can you explain the above code??