I have this bash script here
#!/bin/bash
find /Users/ -name "*.mov" -o -name "*.flv" -o -name "*.mp4" -o -name "*.avi" -o -name "*.wmv" -o -name "*.mpeg" -o -name "*.avi" -o -name "*.wmv" -o -name "*.f4v" -o -name "*.m4v" -o -name "*.mxf" -o -name "*.ts" -type f -mtime +7 -exec rm -rf {} \;
It finds all the files that are older than 7 days, and that works fine, but when I want it to remove the result set that I found it doesn't delete any of the files. Is there something I'm doing wrong? This is on Mac OSX 10.6
Any help would be great. Thanks!
Instead, of -exec rm -rf {}\;, try the -delete option if it's available on your version of the find command. This will show an error message after each failed attempt to delete. That might give you more information what's going on.
$ find . -name "*.foo" -type f -mtime +7 -delete
find: -delete: unlink(./four.foo): Permission denied
find: -delete: unlink(./one.foo): Permission denied
find: -delete: unlink(./three.foo): Permission denied
find: -delete: unlink(./two.foo): Permission denied
Neither find is returning the actual exit code from the delete/rm command. You may want to do something like this:
find . -name ... -type f -mtime +7 | while read file
do
if rm -fr $file
then
echo "Successfully deleted $file"
else
echo "Error deleting file: Exit code $?"
fi
done
That might give you a better understanding of what's going on.
Maybe you should run the command with sudo ? You may not have full access to all directories as a normal user.
The find command is in /usr/bin, which isn't in the default PATH for cron jobs. Either run it as /usr/bin/find, or set PATH at the beginning of your script.
Related
I tried to remove files which might be located in symlinked directories. I recognized for certain basenames, that find fails with the error No such file or directory.
Here is what I tried. Am I doing something wrong? Thanks!
touch a_b.c.d
touch a_b.d
find -L . -type f -name '*.c.d' -exec bash -c 'rm $(basename {} .c.d).d' \;
This fails, too
touch a_b.c.d
touch a_b.d
find -L . -type f -name '*.c.d' -exec rm a_b.d \;
But this works
touch a_x_b.c.d
touch a_x_b.d
find -L . -type f -name '*.c.d' -exec bash -c 'rm $(basename {} .c.d).d' \;
or this, respectively
touch a_x_b.c.d
touch a_x_b.d
find -L . -type f -name '*.c.d' -exec rm a_x_b.d \;
I am deleting the files in all the directories and subdirectories using the command below:
find . -type f -name "*.txt" -exec rm -f {} \;
But I want to know which are the files deleted along with their paths. How can I do this?
Simply add a -print argument to your find.
$ find . -type f -name "*.txt" -print -exec rm -f {} \;
As noted by #JonathanRoss below, you can achieve an equivalent result with the -v option to rm.
It's not the scope of your question, but more generally it gets more interesting if you want to delete directories recursively. Then:
a simple -exec rm -r argument keeps it silent
a -print -exec rm -r argument reports the toplevel directories you're operating on
a -exec rm -rv argument reports all you're removing
I have a bash script that goes into a components/ and runs the following command:
cp -R vendor/* .
I then have a second command that traverses any folder, accept the vendor folder , inside the components directory lookinf got .git/, '.gitignore' and Documentation/ and removes them. How ever:
I don't thinks it's recursive
It doesn't actually remove those files and directories either because of the top point or because of permissions (should I add a sudo)?
A directory copied from vendor might look like:
something/
child-directory/
.git/ // -- Should be removed.
The command in question is:
find -name vendor -prune -o \( -name ".git" -o -name ".gitignore" -o -name "Documentation" \) -prune -exec rm - rf "{}" \; 2> /dev/null || true
Now if it is a permission error, I wont know about it because I want it to ignore any errors and continue with the script.
Any thoughts?
I think the problem is in the option -prune. Anyways, this might work for you...
find vendor -name '.git' -o -name '.gitignore' -o -name 'Documentation' | xargs rm -rf
So I got this as an answer to a previous question as an answer for looking recursively through files in a directory and deleting the files and directories if found:
find \( -name ".git" -o -name ".gitignore" -o -name "Documentation" \) -exec rm -rf "{}" \;
There are two problems with this:
One:
find: `./adam.balan/AisisAjax/.git': No such file or directory
because of this error the rest of the script doesn't execute. Now I don't want to have to check for any of the files or folders. I don't care if they exist or not, I want to suppress the error on this.
The second is that I am also getting the error on a directory that needs to be excluded from this search: vendor/
find: `./vendor/adam.balan/AisisAjax/.git': No such file or directory
I do not want it searching vendor. I want it to leave vendor alone.
How do I solve these two problems? Suppression and ignoring.
The problem is that you're deleting a directory that find then tries to descend into. You can use -prune to prevent that:
find \( -name ".git" -o -name ".gitignore" -o -name "Documentation" \) -prune -exec rm -rf "{}" \;
To ignore all errors, you can use 2> /dev/null to squash the error messages, and || true to avoid set -e making your script exit:
find \( -name ".git" -o -name ".gitignore" -o -name "Documentation" \) -prune -exec rm -rf "{}" \; 2> /dev/null || true
Finally, to avoid descending any directory named 'vendor', you can use -prune again:
find -name vendor -prune -o \( -name ".git" -o -name ".gitignore" -o -name "Documentation" \) -prune -exec rm -rf "{}" \; 2> /dev/null || true
find . -name "*.pyc" -print0| xargs -0 rm
i use this command delete python compiled file but if current directory didn't have any *.pyc
file this cmd will not work
print out the error with rm command need operator args
how to handle this work if current directory didn't have *.pyc file this situation?
Using find -exec:
find -name '*.pyc' -exec rm {} \;
or the discard output technique:
find . -name "*.pyc" -print0| xargs -0 -I{} rm {} &> /dev/null
If you can assume GNU find, then you can use find . -name '*.pyc' -delete.
Alternatively, find . -name '*.pyc' -exec rm -rf {} '+'.