Page 1 of 1

script with multiple line of find, error find: missing argument to `-exec'

Posted: 2018/10/17 11:41:25
by reedone816
hello, i'm new to linux, and just trying to create script.
i have create script to delete files in several folders that is older than x days, so i create it like this:

Code: Select all

#!/bin/bash
find /pathtofolder/* -type f -mtime +7 -exec rm {} \;
find /pathtofolder/* -type f -mtime +7 -exec rm {} \;
the result is error:

Code: Select all

find: missing argument to `-exec'
find: missing argument to `-exec'
can anyone help?

Re: script with multiple line of find, error find: missing argument to `-exec'

Posted: 2018/10/17 14:08:21
by TrevorH
Use -delete instead of -exec rm {} \;

And you probably don't want the * on the end of the path either, find already traverses all subdirectories unless you tell it not to.

Re: script with multiple line of find, error find: missing argument to `-exec'

Posted: 2018/10/18 02:40:07
by reedone816

Code: Select all

find /path to folder/ -type f -mtime +7 -delete;
find /path to folder/ -type f -mtime +7 -delete;
created error: command not found

btw it works when i run the command perline directly in the terminal, but error when in the script

Re: script with multiple line of find, error find: missing argument to `-exec'

Posted: 2018/10/18 09:59:23
by tunk
Does it work without the semicolon? Or maybe use the full path of find, /usr/bin/find.

Re: script with multiple line of find, error find: missing argument to `-exec'

Posted: 2018/10/19 02:45:15
by reedone816
removing the semicolon produce error: 'usr/bin/find: unknown predicate `-delete

Re: script with multiple line of find, error find: missing argument to `-exec'

Posted: 2018/10/19 08:01:06
by TrevorH
What is the output from rpm -q centos-release and from uname -a ?

Re: script with multiple line of find, error find: missing argument to `-exec'

Posted: 2018/10/22 03:22:54
by reedone816
nevermind, I found the issue, it is the $'\r' error, after i use bash -x command.
this seems to happen because i create the script in windows using notepad++.
to fix that i need to convert it to linux format using menu: Edit -> EOL conversion -> Unix/OSX format.

thanks to the quick responses.