Bash: numbered files in Find - linux

I was writing a bash script that finds and deletes the .blend1 and .blend2 files that blender creates as you edit and save .blend files. It contains lines in pairs like this:
find Documents -name "*.blend1" -exec rm -rf {} \;
find Documents -name "*.blend2" -exec rm -rf {} \;
This works just fine, though I was curious if it is at all possible to combine those two find commands some way, so that it would be just one command that finds and deletes both .blend1 and .blend2 files.
Not super-important, I'd just prefer it if my script were a little more compact.

find Documents -name '*.blend[12]' -delete
(-delete is a GNU find extension.)
Other ways:
find Documents '(' -name '*.blend1' -o -name '*.blend2' ')' -delete
find Documents -name '*.blend*' -delete

Here's another way...
find Documents -name '*.blend[12]' -exec rm -rf {} \;

Yes you can use regex to match more that one patterns into one:
find Documents -regextype posix-egrep -regex '.*\.(blend1|blend2)$' -exec rm -rf {} \;
Or with newer find versions:
find Documents -regextype posix-egrep -regex '.*\.(blend1|blend2)$' -delete
Without regex you can do:
find Documents \( -name "*.blend1" -o -name "*.blend2" \) -delete

Related

remove files that are in find result

I want to remove all files that have ".json" extension. so I run
find . -name "*.json"
this will result in some files
x.json
y.json
z.json
then I do rm for each file, but this is a very tedious task when you have many files.
is there any way to mix rm with find result
If using GNU find, it's dead simple:
find directory/ -name "*.json" -delete
You can use -exec option with find.
find . -name '*.json' -exec rm -f {} \;
You can use xargs which will give found files to another command.
The advantage on -exec is that does not create one new process per file:
find . -name "*.json" | xargs rm

FInd patternf for multiple pattern of files

I need to search for multiple pattern of files and check their mtime and if it morethan 30 days then delete all the files. I am using the below command but it's deleting only one pattern of file and not all. Kindly let me know where is the mistake in my command.
find /root -type f \( -name "*.tgz" -o -name "*.bz2" \) -mtime +30 -print -exec rm '{}' +
Try escaping parentheses in the command and adding a wildcard character:
find /root -type f \( -name "*.tgz" -o -name "*.bz2" \) -mtime +30 -exec rm {} \+

Remove files in subdirectories older than 1 day with Linux command

I am honestly nowhere near to be a decent bash scripter, but I made a little research and found a command that seems to be useful
find /path/to/files* -mtime +1 -exec rm {} \;
The question is if this line will remove directories? Because I want to only remove files that are images (actually in a *.jpeg format)
No, rm without the -r flag does not remove directories.
It looks like you want to add some more filters:
-type f to match only files
-name '*.jpeg' to match only files ending with .jpeg
Lastly, instead of -exec rm {} \;, you could use the much simpler -delete.
Putting it together, this looks more appropriate for you:
find /path/to/files* -mtime +1 -type f -name '*.jpeg' -delete
Then narrow your search results to *.jpeg files:
find /path/to/files* -mtime +1 -type f -name "*.jpeg" -exec rm {} \;
It's always better to remove the exec parameter to do a dry run before delete:
find /path/to/files* -mtime +1 -type f -name "*.jpeg"
Each line will be passed to rm command, and nothing more.

How to recursively delete multiple files with different extensions?

I am trying to write a command to remove recursively several files with different extensions (*.extension1, *.extension2 etc) from the current directory and all its related sub-directories.
So far I got this command from another post but I couldn't workout how to adapt it to more than one file in the same command line:
find . -name "*.extension1" -type f -delete
Is it as simple as below?
find . -name "*.extension1";"*.extension2" -type f -delete
Just as a side note, these are all output files that I do not need, but not all are necessarily always output so some of the extensions might not be present. This is just as a general clean-up command.
find . \( -name "*.extension1" -o -name "*.extension2" \) -type f -delete
find Documents ( -name ".py" -o -name ".html" ) -exec file {} \;
OR
find . -regextype posix-egrep -regex ".*\.(extension1|extension2)$" -type f -delete
Just add more options. A regex solution can also apply but this one's better and safer.
find . \( -name '*.ext1' -or -name '*.ext2' \) -type f -delete
Edit: You probably need -or as well. Before deleting, test it first without the -delete option. (2) Added a pair of () as suggested by gniourf_gniourf.
Maybe regexp will help you
find . -regextype posix-awk -regex "(.*.ext1|.*.ext2)" -type f -delete
Another solution using rm:
rm -rf ./**/*.{txt,nfo,jpg,jpeg,png,exe,url}
If you want to delete other files too for e.g. those starting with sample. just add that too:
rm -rf ./**/*.{txt,nfo,jpg,jpeg,png,exe,url} ./**/*/sample.*
This simple command will delete all the files with extension1 and extension2 recursively in that directory.
rm find . -name *.extension1 -o -name *.extentions2

Linux: Delete every file older than a date with one exceptional file

I am able to delete lets say all regular files in a folder older than 7 days via:
find /path/to/dir -type f -mtime +7 -exec rm {} \;
with a single problem. There is a file here (.gitignore) which I want to keep. I tried using regex but apparently findutils regex does not have support for negative lookahead (?!gitignore)
Any other ideas?
Use ! -name .gitignore
find /path/to/dir ! -name .gitignore -type f -mtime +7 -exec rm {} \;
You can group multiple arguments within escaped parentheses. Example, to remove all files except .gitignore and javascript files (ending in .js):
find /path/to/dir ! \( -name ".gitignore" -o -name "*.js" \) -type f -mtime +7 -exec rm {} \;
-o means or

Resources