Want to create a script that removes all screenshots from desktop - bash

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*.

Related

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

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).

Check if file is in a folder with a certain name before proceeding

So, I have this simple script which converts videos in a folder into a format which the R4DS can play.
#!/bin/bash
scr='/home/user/dpgv4/dpgv4.py';mkdir -p 'DPG_DS'
find '../Exports' -name "*1080pnornmain.mp4" -exec python3 "$scr" {} \;
The problem is, some of the videos are invalid and won't play, and I've moved those videos to a different directory inside the Exports folder. What I want to do is check to make sure the files are in a folder called new before running the python script on them, preferably within the find command. The path should look something like this:
../Exports/(anything here)/new/*1080pnornmain.mp4
Please note that (anything here) text does not indicate a single directory, it could be something like foo/bar, foo/b/ar, f/o/o/b/a/r, etc.
You cannot use -name because the search is on the path now. My first solution was:
find ./Exports -path '**/new/*1080pnornmain.mp4' -exec python3 "$scr" {} \;
But, as #dan pointed out in the comments, it is wrong because it uses the globstar wildcard (**) unnecessarily:
This checks if /new/ is somewhere in the preceding path, it doesn't have to be a direct parent.
So, the star is not enough here. Another possibility, using find only, could be this one:
find ./Exports -regex '.*/new/[^\/]*1080pnornmain.mp4' -exec python3 "$scr" {} \;
This regex matches:
any number of nested folders before new with .*/new
any character (except / to leave out further subpaths) + your filename with [^\/]*1080pnornmain.mp4
Performances could degrade given that it uses regular expressions.
Generally, instead of using the -exec option of the find command, you should opt to passing each line of find output to xargs because of the more efficient thread spawning, like:
find ./Exports -regex '.*/new/[^\/]*1080pnornmain.mp4' | xargs -0 -I '{}' python3 "$scr" '{}'

Running a bash find with file cp parameter error python script

I'd like to copy a file_list to another location. This is being called in a python script. I have
find <sourceaddress> -exec cp '{}' <destaddress> | .* rm
but it tells me an exact parameter is missing. It runs though it gives a prompt from the command line and from the script just does nothing.
I think you are missing "\;" at the end. I am not sure what the .* rm does. Assuming you want to remove the files you can use the 'mv' command instead of 'cp'.
For copying files only from one directory to another ,
find <srcdirectory> -exec cp '{}' <destdirectory> \;
If you want to move the files, use 'mv' instead use below.
find <srcdirectory> -exec mv '{}' <destdirectory> \;

Remove all files with specified extension from network share on OS X using Terminal

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).

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.

Resources