GNU find following symbolic links and name pattern fails to execute remove (rm) - bash

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 \;

Related

Find file and cd into it

I am attempting to find multiple files, then quit after the first match and then cd into this match, I have attempted:
find `pwd` -iname 'tensorflow' -type d -exec echo {} \; -quit | xargs -I{} cd {}
However, this does nothing and it won't enter into that directory.
There is no /usr/bin/cd, it's not an executable. You have to run it in current shell, not in subshell as part of pipeline.
Do not use backticks. Prefer $(...).
find pwd? Just find ., you are already in pwd.
-exec echo {} \;? Just -print it.
dir=$(find . -iname 'tensorflow' -type d -print -quit)
cd "$dir"

find outputs result but -exec ignores it

I wrote a simple command to find folders older than a month and delete them.
Here is the command :
find . -type d -mtime +31 -exec bash -c 'rm -rfv "$0"' {} \;
It works fine in most cases, but sometime, the -exec "ignores" the result.
After running the command once, If I run the find without -exec, it still finds some folders older than a month which have not been removed.
I then tried with a simple echo and got no output :
$ find . -type d -mtime +31
./folder_A
./Folder_B
$ find . -type d -mtime +31 -exec bash -c 'echo "$0"' {} \;
<No output>
I found a workround using grep but I'm wondering why the -exec ignores some results.
Anyone knows ?
Here is the workaround :
find . -type d -mtime +31 | grep . --color=never | while read line ; do rm -rvf "$line" ; done
Your find commands seem a bit strange to me. I'd suggest changing:
find . -type d -mtime +31 -exec bash -c 'rm -rfv "$0"' {} \;
to:
find . -type d -mtime +31 -exec rm -rfv {} \;
or even better (increases performance):
find . -type d -mtime +31 -exec rm -rfv {} +
If you insist on calling Bash explicitly (although I can't think of any reason why you should as rm is not a Bash built-in), I'd suggest changing:
find . -type d -mtime +31 -exec bash -c 'rm -rfv "$0"' {} \;
to:
find . -type d -mtime +31 -exec bash -c 'rm -rfv {}' \;
Same goes for the find command containing echo.

How to print the deleted file names along with path in shell script

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

how to process linux find command didn't has result

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 {} '+'.

Bash Script OSX not deleting files it finds

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.

Resources