How do I find a bunch of files with a string in its filename (or body text) and then move all those files to a specific folder?

Say - what happens if I intended to relocate every HTML documents in numerous independent directory sites with words "heavengames" in its filename (and also as a 2nd inquiry, every HTML files with words "heavengames" in its body message) to a new directory site called "heavengames - threads"?

5
2022-06-07 14:40:04
Source Share
Answers: 2

To relocate documents with words in its name:

find /path/to/dir1 /path/to/dir2 /and/so/on -type f -iname "*heavengames*" \
-exec mv -t /path/to/heavengames-threads {} \+

To relocate documents with word in its body:

find /path/to/dir1 /path/to/dir2 /and/so/on -type f -exec grep -q heavengames {} \; \
-exec mv -t /path/to/heavengames-threads {} \+

ps. to examine that all is proper, add echo prior to mv at the first run.

4
2022-06-07 15:11:49
Source

In zsh or bash ≥ 4, based upon the documents name:

mkdir heavengames-threads
mv **/*heavengames*.html heavengames-threads/

For even more adaptability (as an example, to recreate the directory site power structure, seek out zmv (there are many examples on this site).

Usage grep to search the documents materials. With current variations of GNU energies (i.e. on non - ingrained Linux or Cygwin):

grep -RZ heavengames . | xargs -0 mv -t heavengames-threads/

For even more mobile commands, make use of find. See Rush's answer.

2
2022-06-07 14:47:41
Source