Why is this command exploding - bash

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

Related

How to use find to copy and remove directories?

I am trying to find the files which are older than some days and i want to copy the files to the specific folder and to remove the files from the source directory in a single command.
find . -type d -mtime +600 -exec cp -rf '{}' destination folder \; -exec rm -rf '{}' \;
Error:
find: ./one: No such file or directory
find: ./two: No such file or directory
find: ./three: No such file or directory
How can I resolve the error?
Try something like:
find . -type d -mtime -600 -exec cp -rf '{}' destination_folder \; -delete
Alternatively, you could even use
find . -type d -mtime -600 -print0 -exec cp -rf '{}' destination_folder \; | xargs -0 rm -rf
I'm not quite sure why you're doing cp and rm, rather than the much faster mv?
But, you can largely ignore that error, find pre-processes part of the tree, if you want to force it to ignore the removed subdirectories add -prune (you probably should quote destination dir)
find . -type d -mtime +600 -exec cp -rf '{}' 'destination folder' \; -exec rm -rf '{}' \; -prune
.
-prune True; if the file is a directory, do not descend into it. If -depth is given, false; no effect.
Because -delete implies -depth, you cannot usefully use -prune and -delete together.

find works, but find -exec doesn’t

I am trying to find all images in subfolders of a given folder, and move them somewhere else. I have tried the following in zsh (my default) and sh (what most tutorials seem to be using) on a Mac running OS X 10.9.3.
This prints out all the images in the subfolders of $someDir:
find "$someDir" -iname \*.jpg -o -name \*.png -o -name \*.gif
However, when I want to pass those images to another command, I can’t get it to work. As an exercise, I tried it with echo:
find "$someDir" -iname \*.jpg -o -name \*.png -o -name \*.gif -exec sh -c "echo hello {}" \;
It just returns silently, and the value of $? is 0.
I eventually want to do something along these lines:
find "$someDir" -iname \*.jpg -o -name \*.png -o -name \*.gif -exec sh -c "mv {} $destination" \;
But I can‘t even get the echo example to work. What am I doing wrong?
You need to put parentheses around all the name tests:
find "$someDir" \( -iname \*.jpg -o -name \*.png -o -name \*.gif \) -exec sh -c "echo hello {}" \;
Otherwise, the -exec is only done for files that match *.gif.
When you leave out the action, there's a default -print in each branch of the -o. But if there's any action option in the command, there's no default actions anywhere.
These should work (make sure $destination is defined)
find "$someDir" \( -iname \*.jpg -o -name \*.png -o -name \*.gif \) -exec echo hello {}
find "$someDir" \( -iname \*.jpg -o -name \*.png -o -name \*.gif \) -exec mv {} $destination \;

Searching Directories and Removing Folders and Files in Bash

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

Find and delete old files excluding some subdirectories

I have been searching for a while, but can't seem to get a succinct solution. I am trying to delete old files but excluding some subdirectories (passed via parm) and their child subdirecories.
The issue that I am having is that when the subdirectory_name is itself older than the informed duration (also passed via parm) the find command is including the subdirectory_name on the list of the find. In reality the remove won't be able to delete these subdirectories because the rm command default option is f.
Here is the find commmand generated by the script:
find /directory/ \( -type f -name '*' -o -type d \
-name subdirectory1 -prune -o -type d -name directory3 \
-prune -o -type d -name subdirectory2 -prune -o \
-type d -name subdirectory3 -prune \) -mtime +60 \
-exec rm {} \; -print
Here is the list of files (and subdirectories brought by the find command)
/directory/subdirectory1 ==> this is a subdreictory name and I'd like to not be included
/directory/subdirectory2 ==> this is a subdreictory name and I'd like to not be included
/directory/subdirectory3 ==> this is a subdreictory name and I'd like to not be included
/directory/subdirectory51/file51
/directory/file1 with spaces
Besides this -- the script works fine not bringing (excluding) the files under these 3 subdirectories:
subdirectory1, subdirectory2 and subdirectory3.
Thank you.
Following command will delete only files older than 1 day.
You can exclude the directories as shown in the example below, directories test1 & test2 will be excluded.
find /path/ -mtime +60 -type d \( -path ./test1 -o -path ./test2 \) -prune -o -type f -print0 | xargs -0 rm -f
Though it would be advisable to see what's going to be deleted using -print
find /path/ -mtime +60 -type d \( -path ./test1 -o -path ./test2 \) -prune -o -type f -print
find /directory/ -type d \(
-name subdirectory1 -o \
-name subdirectory2 -o \
-name subdirectory3 \) -prune -o \
-type f -mtime +60 -print -exec rm -f {} +
Note that the AND operator (-a, implicit between two predicates if not specified) has precedence over the OR one (-o). So the above is like:
find /directory/ \( -type d -a \(
-name subdirectory1 -o \
-name subdirectory2 -o \
-name subdirectory3 \) -a -prune \) -o \
\( -type f -a -mtime +60 -a -print -a -exec rm -f {} + \)
Note that every file name matches the * pattern, so -name '*' is like -true and is of no use.
Using + instead of ; runs fewer rm commands (as few as possible, and each is passed several files to remove).
Do not use that code above on directories writeable by others as it's vulnerable to attacks whereby the attacker can change a directory to a symlink to another one in between the time find traverses the directory and calls rm to have you delete any file on the filesystem. Can be alleviated by changing the -exec part with -delete or -execdir rm -f {} \; if your find supports them.
See also the -path predicate if you want to exclude a specific subdirectory1 instead of any directory whose name is subdirectory1.

Find and delete multiple argument directories

Say I have 3 directories .git, .hg and .svn somewhere in the folder called lol.
How can I find and remove all of these?
The following does so only for the last one (.svn):
$ find lol -type d -name .git -o -name .hg -o -name .svn -delete
E: This could be done with ls too but not without shopt -s globstar (since I'm not doing it with zsh):
$ shopt -s globstar
$ rm -r $(ls -d ceaw/**/.{git,hg,svn})
E2: Another solution woulda been:
$ find lol -type d -name .git -o -name .hg -o -name .svn | xargs rm -rf
Try using the -exec option instead.
find lol -depth -type d \( -name .git -o -name .hg -o -name .svn \) -exec rm -r '{}' \;

Resources