Unable to execute find command from inside a shell script

I have a shell script with 775 permission as below

/app/script/test.sh

#!/bin/bash
/usr/bin/find /app/Jenkins/home/jobs/test1/builds -type d -mtime 1 |  xargs rm -rf
/usr/bin/find /app/Jenkins/home/jobs/test2/builds -type d -mtime 1 |  xargs rm -rf

When i execute the script it simply runs without the find command getting executed and the files being deleted.

Both the find commands work fine when i execute them manually from the terminal.

I want the script to run both the find commands and perform their respective tasks.

i tried to put backticks `` around the find command but still no luck.

Can you please suggest ?

Why use a pipe when it can be done within find command?
look at the man pages of man, especially after how to use multi criteria and the the exec

1 Like

I tried

#!/bin/bash
/usr/bin/find /app/Jenkins/home/jobs/test1/builds -type d -mtime 1 -exec rm -rf {} \; 
/usr/bin/find /app/Jenkins/home/jobs/test2/builds -type d -mtime 1 -exec rm -rf {} \;

This works and deletes the folders but i get several error messages stating No such file or directory under "/app/Jenkins/home/jobs/test2/builds". Can you please explain why and what is a good approach to avoid such warning messages ?

I'm also curious to learn why exec would work while pipe xargs would not !!

because that is how it was designed to work with find, and it is not xargs the culprit but rm command
in other words many commands e.g. accept a - to take for input what comes from a pipe, rm does not...
but you could try (in ksh...)

rm -rf $(/usr/bin/find /app/Jenkins/home/jobs/test2/builds -type d -mtime 1)

To question one, usually I would say you had links that were deleted by the first line, or temp files at the time of the find that were no more there when it came to rm

I would suggest a few improvements (optimizations and error avoidance):

#!/bin/bash
find /app/Jenkins/home/jobs/test[12]/builds -type d -mtime 1 -depth -exec rm -rf {} +

Note also that is a frequent case where find works but pipe xargs fails: that's when file or directory names contain embedded spaces.