remove files that are in find result - linux

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

Related

Stuck trying to copy files to a new directory under specific guidelines

I'm trying to copy all files from a folder that start with a capital letter into another folder.
So far i've used the find command to actually find the files
find /examplefolder -type f -name "[[:upper:]]*"
and i'm able to find them no problem
I tried to just replace the find command with cp and that does not work then i tried to pipe into the cp command and I failed yet again
Use the -exec option for find
find /examplefolder -type f -name "[[:upper:]]*" -exec cp {} /my/new/shiny/folder/ \;
I was able to do it by piping in to cp with xargs and the -I argument
find /examplefolder -type f -name "[[:upper:]]*" | xargs -I % cp % /copied_directory

Bash: numbered files in Find

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

How to write a unix command or script to remove files of the same type in all sub-folders under current directory?

Is there a way to remove all temp files and executables under one folder AND its sub-folders?
All that I can think of is:
$rm -rf *.~
but this removes only temp files under current directory, it DOES NOT remove any other temp files under SUB-folders at all, also, it doesn't remove any executables.
I know there are similar questions which get very well answered, like this one:
find specific file type from folder and its sub folder
but that is a java code, I only need a unix command or a short script to do this.
Any help please?
Thanks a lot!
Perl from command line; should delete if file ends with ~ or it is executable,
perl -MFile::Find -e 'find(sub{ unlink if -f and (/~\z/ or (stat)[2] & 0111) }, ".")'
You can achieve the result with find:
find /path/to/directory \( -name '*.~' -o \( -perm /111 -a -type f \) \) -exec rm -f {} +
This will execute rm -f <path> for any <path> under (and including) /path/to/base/directory which:
matches the glob expression *.~
or which has an executable bit set (be it owner, group or world)
The above applies to the GNU version of find.
A more portable version is:
find /path/to/directory \( -name '*.~' -o \( \( -perm -01 -o -perm -010 -o -perm -0100 \) \
-a -type f \) \) -exec rm -f {} +
find . -name "*~" -exec rm {} \;
or whatever pattern is needed to match the tmp files.
If you want to use Perl to do it, use a specific module like File::Remove
This should do the job
find -type f -name "*~" -print0 | xargs -r -0 rm

How to find in linux and delete directories that not match a name? !#Bash

I need to delete unpacked directories from my /source tree keeping the others with .tar and .patch extensions,
how to do please?
This should work:
find . -not -name "*.tar" -not -name "*.patch" -type f -exec rm {} \;
This is using only one command not using pipes.
Note. This will proceed recursively into subdirectories. If this is unwanted, use the maxdepth switch:
find . -maxdepth 1 -not -name "*.tar" -not -name "*.patch" -type f -exec rm {} \;
BACKUP YOUR DIRECTORY FIRST, I HAVE NOT TESTED THIS, AND IT HAS A BUG AS NOTED IN THE COMMENTS.
WHILE IN THE ACTUAL /source DIRECTORY:
ls|fgrep -v -e .tar -e .patch|xargs rm -rf
You probably want to use the "put echo after xargs" trick to see what this would actually do, before running it:
ls|fgrep -v -e .tar -e .patch|xargs echo rm -rf

Recursively remove files

Does anyone have a solution to remove those pesky ._ and .DS_Store files that one gets after moving files from a Mac to A Linux Server?
specify a start directory and let it go? like /var/www/html/ down...
change to the directory, and use:
find . -name ".DS_Store" -print0 | xargs -0 rm -rf
find . -name "._*" -print0 | xargs -0 rm -rf
Not tested, try them without the xargs first!
You could replace the period after find, with the directory, instead of changing to the directory first.
find /dir/here ...
find /var/www/html \( -name '.DS_Store' -or -name '._*' \) -delete
Newer findutils supports -delete, so:
find . -name ".DS_Store" -delete
Add -print to also get a list of deletions.
Command will work for you if you have an up-to-date POSIX system, I believe. At least it works for me on OS X 10.8 and works for others who've tested it on macOS 10.12 (Mojave).
Credit to #ephemient in a comment on #X-Istence's post (thought it was helpful enough to warrant its own answer).
Simple command:
rm `find ./ -name '.DS_Store'` -rf
rm `find ./ -name '._'` -rf
Good luck!
cd /var/www/html && find . -name '.DS_Store' -print0 | xargs -0 rm
cd /var/www/html && find . -name '._*' -print0 | xargs -0 rm
You could switch to zsh instead of bash. This lets you use ** to match files anywhere in a directory tree:
$ rm /var/www/html/**/_* /var/www/html/**/.DS_Store
You can also combine them like this:
$ rm /var/www/html/**/(_*|.DS_Store)
Zsh has lots of other features that bash lacks, but that one alone is worth making the switch for. It is available in most (probably all) linux distros, as well as cygwin and OS X.
You can find more information on the zsh site.
find . -name "FILE-TO-FIND"-exec rm -rf {} \;
Example to delete "Thumbs.db" recursively;
find . -iname "Thumbs.db" -print0 | xargs -0 rm -rf
Validate by:
find . -iname "Thumbs.db"
This should now, not display any of the entries with "Thumbs.db", inside the current path.
It is better to see what is removing by adding -print to this answer
find /var/www/html \( -name '.DS_Store' -or -name '._*' \) -delete -print
if you have Bash 4.0++
#!/bin/bash
shopt -s globstar
for file in /var/www/html/**/.DS_Store /var/www/html/**/._
do
echo rm "$file"
done
A few things to note:
'-delete' is not recursive. So if .TemporaryItems (folder) has files in it, the command fails.
There are a lot of these pesky files created by macs:
.DS_Store
._.DS_Store
.TemporaryItems
.apdisk
This one command addresses all of them. Saves from running find over and over again for multiple matches.
find /home/foo \( -name '.DS_Store' -or -name '._.DS_Store' -or -name '._*' -or -name '.TemporaryItems' -or -name '.apdisk' \) -exec rm -rf {} \;
This also works:
sudo rm -rf 2018-03-*
here your deleting files with names of the format 2018-03-(something else)
keep it simple

Resources