"find: missing argument to -exec" when executing a shell script - shell

I am trying to execute a shell script which has the following within it:
find /hana/shared/directory -type d -mtime +2 -exec rm -rf {} \;
This works on other SUSE Linux servers but on one. It keeps returning the following:
find: missing argument to -exec
If, however, I place the same syntax into a terminal and run it manually, it runs without issue.
I can see this is a common issue, but I believe I have tried many of the suggestions to no avail and I'm a bit stuck now.

Very carefully read find(1), proc(5), and the GNU Bash documentation.
You might want to run (this is dangerous; see below):
find / -type d mtime +2 -exec /bin/rm -f '{}' \;
(use at least -ok instead of -exec)
And you probably want to clean just your $HOME.
But you should avoid removing files from /proc/, /sys/, /dev/, /lib/, /usr/, /bin/, and /sbin/. See hier(7) and environ(7).

Related

Using fish shell builtins with find exec

I'm trying to source a file that I can get from the output of find using these commands:
find ./ -iname activate.fish -exec source {} \;
and
find ./ -iname activate.fish -exec builtin source {} \;
But both these commands give the error of the form find: ‘source’: No such file or directory or find: ‘builtin’: No such file or directory. Seems like exec of find is not able to recognize fish's builtins ?
What I basically want to achieve is a single command that will search for Python's virtualenv activate scripts in the current directory and execute them.
So doing something like -exec fish -c 'source {}; \ would not help. I've tried it as well and it doesn't error out but does not make the changes either.
Any ideas what can be done for this ?
Thanks!
Perhaps you need:
for file in (find ./ -iname activate.fish)
source $file
end
# or
find ./ -iname activate.fish | while read file
source $file
end
Command substitution executes the command, splits on newlines, and returns that list.
As mentioned in comments, seems like -exec does not run in or affect the current shell environment. So find -exec is not gonna work for my use case.
Instead, this will work:
source (find ./ -iname activate.fish)

How to concatenate string to -print in unix find command?

I have this command to run in Unix machine where I'm deleting all directories from specific folder if they are older then 1 day:
find /path_with_dirs_to_delete/* -type d -mtime +1 -prune -exec rm -rf {} + -print
It's currently printing out the folder names and i want to concatenate to the printing output the text " was deleted successfully".
How can I do that?
If using GNU tools, a superior solution is to add the -v (verbose) flag to rm.
For a POSIX solution you could do this:
find /path/* -type d -mtime +1 -prune -exec rm -rf {} \; -exec echo {} was deleted successfully \;
You were running a single rm to remove multiple directories. But if you want to test rm's success for each directory, it has to run once per directory (note ; instead of +). Note that this a bit less efficient.
-a ("and") is implied between find actions, meaning the second -exec action (echo) only runs if the previous one (rm) succeds. So this can be used to log a successful removal.
You will receive an error message from rm if a removal fails (eg. permission denied).
rm -f suppresses "does not exist" errors, and you wouldn't get a success message either, however find won't pass a non-existent directory.

Want to create a script that removes all screenshots from desktop

I want to have a script that I can run to remove all screenshots from my desktop.
All the screenshots have a name starting with "Screen Shot".
I tried using this;
find /Users/Mht/Desktop/ -name "Screen*" -exec rm{} -i \;
(My username is "Mht"...) this returns:
find: rm/Users/Mht/Desktop//Screen Shot 2017-05-09 at 19.08.59.png: No such file or directory
What am I doing wrong here?
You are missing a space between rm and {} (if you look closely at the error message this becomes apparent, but it's not immediately obvious, I admit).
I just tried the following and it worked for me:
find ~/Desktop -type f -name "Screen Shot*" -exec rm {} \;
You can always write a Python script for this, which is fairly easy:
import os
os.chdir('/Users/Mht/Desktop')
filenames = [filename for filename in os.listdir() if filename.startswith("Screen")]
for f in filenames:
os.remove(f)
Of course if you absolutely want to do it in a bash script you can always just use rm with a wildcard, as in rm -i /Users/Mht/Desktop/Screen*. Of course, as noted in a comment, it is always much safer to be as specific as possible, so it is even better to have something like rm -i /Users/Mht/Desktop/Screen\ Shot*.

Shell says find: missing argument to `-exec' and no alternatives working

A backup program I used recently made duplicates of whole bunch of files throughout my computer because of some setting that I've since changed.
When the backup program made a copy, it renamed the old one original1.thefilename.extension. I'm trying to automatically delete all of these unnecessary files with a simple shell command.
find -type f -name 'original1*' -exec rm {} \;
However, when I try to run this I get
find: missing argument to `-exec'
I've looked all over the web for a solution. I've found suggestions that I should try exec rm +, -exec rm {} +, -exec rm {} \;, -exec rm + etc. but none of them work. I am using Windows 8.1
I would really appreciate any help!
In Windows command shell, you don't need to escape the semicolon.
find -type f -name 'original1*' -exec rm {} ;
Your version of the command should work in a bash shell (like cygwin).
It's interesting that you get the gnu find to execute, because on my Windows 8.1 machine, I get Microsoft's find.

Shell Command to remove older files

I am making backups of a client's website on a remote FTP location. I have a script (usable without root access on cPanel) which is making backups on given cron and transfer it to remote ftp location. Now the real problem is starting; as we can't have unlimited gigabytes of disk space on any server so we have to limit the backups. I was finding shell command (which can be added to cronjob directly or by creating a bash script and call that script from cron. I want to keep 1 week's daily backups. I want to delete any backup from that directory which is older than 1 week. I found following command which looks promising
find /path/to/files -mtime +30 -exec rm {}\;
But when I ran this command (for testing I replaced 'rm' with 'ls -l') I got following error
find: missing argument to `-exec'
can anybody help to resolve this little issue?
I am running CentOS + cPanel
Thank You
May be you just have to put space after the right bracket:
find /path/to/files -mtime +30 -exec rm {} \;
I couldn't test on CentOS, but on my system it doesn't work if I don't put spaces around the brackets.
The semi-colon must be a separate argument (and a week is 7 days):
find /path/to/files -mtime +7 -exec rm {} ';'
Actually, you would probably do better to use the notation + in place of ; as that will combine as many files names as convenient into a single command execution, rather like xargs does but without invoking xargs. Hence:
find /path/to/files -mtime +7 -exec rm {} +
One other advantage of this is that there are no characters that must be protected from the shell.

Resources