5

Say - what if I wanted to move every HTML file in several independent directories with the word "heavengames" in its filename (and as a second question, every HTML files with the word "heavengames" in its body text) to a new directory named "heavengames-threads"?

2 Answers 2

4

To move files with the word 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 move files 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 check that all is correct, add echo before mv at the first run.

2

In zsh or bash ≥4, based on the file name:

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

For more flexibility (for example, to recreate the directory hierarchy, look up zmv(there are many examples on this site).

Use grep to search the file contents. With recent versions of GNU utilities (i.e. on non-embedded Linux or Cygwin):

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

For more portable commands, use find. See Rush's answer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .