Removing all the files with a specific extension using bash - bash

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 {}

Related

Find for YML and YAML files on bash find

I am trying to find all .yaml and .yml
I tried
find . -name '*.{yml,yaml}' -exec echo "{}" \;
But no results
Neither in the following way
find . -name '*.yml' -name '*.yaml' -exec echo "{}" \;
Returns nothing.
Is it possible to use the find command to search for both extensions?
With GNU find find none or one a:
find . -regextype egrep -regex '.*ya?ml$'
or
find . -regextype egrep -regex '.*ya{0,1}ml$'
See: man find
Something like this.
find . \( -name '*.yaml' -o -name '*.yml' \)
See UsingFind
See Understanding-the-exec-option-of-find

find: missing argument to `-exec`

I'm trying to get my Shell script working, but I keep getting: find: missing argument to '-exec'
#!/bin/sh
echo Hello World
find ./public_html/var/session/ -type f -name "sess*" -mtime +30 -exec rm -f {} \;
echo DONE
I've been trying to find help in these solutions but sadly none of them have solved my problem.Shell script - find: missing argument to `-exec' find: missing argument to -exec
Another option not involving -exec is to pipe the result to xargs and then rm on it:
find ./public_html/var/session/ -type f -name "sess*" -mtime +30 | xargs rm -f
man xargs
Note:
xargs supports executing in parallel with -P command, see man page.

How to extract files with extension from directories?

I am currently extracting pictures from a large multi-levels directory using the following bash command:
find . -name \*.jpg -exec cp {} /newdir_path_.. \;
However, all pictures are stored under 3 versions:
xxx-LD.jpg
xxx-SD.jpg
xxx.jpg
I just want to extract the xxx.jpg pictures, not the LD and SD...
how should my command be modified to perform such extraction?
You can add more tests:
find . -name '*.jpg' -not -name '*-[LS]D.jpg' -exec cp {} /newdir_path_.. \;
-not is a GNU extension; you can use ! -name instead. In some shells, ! has to be escaped: \! -name.
This may also work for you considering your filenames only contain digits before .jpg:
find . -iregex '.*/[0-9]*\.jpg$' -exec cp {} /newdir_path_.. \;

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

Shell Scripting: Using bash with xargs

I'm trying to write a bash command that will delete all files matching a specific pattern - in this case, it's all of the old vmware log files that have built up.
I've tried this command:
find . -name vmware-*.log | xargs rm
However, when I run the command, it chokes up on all of the folders that have spaces in their names. Is there a way to format the file path so that xargs passes it to rm quoted or properly escaped?
Try using:
find . -name vmware-*.log -print0 | xargs -0 rm
This causes find to output a null character after each filename and tells xargs to break up names based on null characters instead of whitespace or other tokens.
Do not use xargs. Find can do it without any help:
find . -name "vmware-*.log" -exec rm '{}' \;
Check out the -0 flag for xargs; combined with find's -print0 you should be set.
find . -name vmware-*.log -print0 | xargs -0 rm
GNU find
find . -name vmware-*.log -delete
find . -name vmware-*.log | xargs -i rm -rf {}
find -iname pattern
use -iname for pattern search
To avoid space issue in xargs I'd use new line character as separator with -d option:
find . -name vmware-*.log | xargs -d '\n' rm

Resources