Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"?

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.