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 {} '+'.
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 want to remove all the files with '*.tar.gz' extension using a bash command.
I tried the following, but it didn't work.
find . -iname '*.tar.gz' | rm
Could you please suggest which command should I use in this case?
Also, could you please tell me why the above command doesn't work?
find itself can delete in some versions, so :
find . -iname '*.tar.gz' -delete
if you don't have this switch, use anubhava's solution.
Don't pipe rm to output of find output, use xargs or find -exec:
find . -iname '*.tar.gz' -exec rm {} +
OR:
find . -iname '*.tar.gz' -print0 | xargs -0 -I {} rm {}
filename=file.bin
extension=$(echo ${filename}|awk -F\. '{print $2}')
if [ ${extension} == "bin" ]; then
rm *.extenstion
fi
would something like this work how do I delete all files that have the same extention in a folder
You don't need to extract the extension yourself, this is what globbing is for. Simply do:
rm *.bin
Or recursively find ./ -name "*.bin" -exec rm -f {} \;
Aside from globbing, this is also doable with find.
find -type f -name "*.bin" -exec rm {} \;
Or more efficiently, with newer version of find:
find -type f -name "*.bin" -exec rm {} +
which is equivalent to
find -type f -name "*.bin" | xargs rm
Note: by default, find will do it recursively.
I'm trying to recursive erase all files that begin with "._" (aka mac dot files) on my server using SSH.
The files are listed with a ls -a but this won't work:
rm -rf ._*
I know there's a way. Mind to share?
Cheers!
find . -name ._\* -print0 | xargs -0 rm -f
find . -name ._\* -type f -delete
Specify that it's files and just call the find-delete on item directly.
find . -name ._\* -exec rm -f {} \;
by the way rm -rf is for removing directories recursively