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.
Related
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).
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*.
After connecting to a network share on OS X I am looking to delete all files that have a particular extension (filename.exte for example) from all folders within the share. If i'm in the very top folder is there a command in terminal I can run that will do this?
If this it not possible, is there any other way to achieve this?
You can use find:
$ find /Volumes/whatever -type f -name \*.exte -exec rm -f {} \;
However you need to be very careful - one slip and you could delete a lot of files uninintentionally - I usually do a "dry run" first:
$ find /Volumes/whatever -type f -name \*.exte -exec echo "rm -f {}" \;
(this will just list the files that would be deleted with the first version, but will not actually delete anything).
I've got a script that uses find. So far I've been fine using -exec to get what I need done, but I'm running into a bit of a roadblock now.
If files are found, I want to execute a command once (so -exec {} \; isn't what I'm looking for), and it's a command that doesn't take any files as parameters, so -exec {} \+ won't work (or at least it doesn't with the version of find on my FreeBSD 10.1 system.
What's the best way to go about doing this?
You need to pipe the filenames to xargs.
I think I missed an important clue here: your command is not interested in the filenames. So you just want to run a command if files were found? In that case, you could try something like:
find -name nosuchfile | if read; then echo "hello"; fi;
The -quit primary can be used in conjunction with -exec, -quit is present in both the BSD and GNU find implementations. The example below is constructed to ensure that find terminates after the first match, regardless of the exit status of the utility.
find path... [expression] \( -exec utility \; -quit -or -quit \)
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.