Server Fault is a question and answer site for system and network administrators. 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

Is there such an option so that dos2unix can be applied recursively to matching files ?

share|improve this question
up vote 8 down vote accepted

You can use find to select the files which you want to alter. And use xargs to pass filenames to dos2unix.

find . -iname '*.tpl' | xargs dos2unix

If you want to limit the search to the two directories ww1 and ww2 you can use the following command

find /var -iname '*.tpl' -regex '/var/ww[1,2]/.+' | xargs dos2unix
share|improve this answer
How to restrict the directories to /var/ww1 and /var/www2? – yoyo Nov 24 '10 at 8:39
find /var/www1 -iname '*.tpl' | xargs dos2unix and so on – Tom O'Connor Nov 24 '10 at 8:43
Also, have a look at the man page for find man find and see if you can teach yourself to fish. – Tom O'Connor Nov 24 '10 at 8:44
@yoyo: find /var/ww[12] -iname "*.tpl" -print0 | xargs -0 dos2unix – Dennis Williamson Nov 24 '10 at 14:16

If you are are using zsh you can simply do:

dos2unix **/*.tpl

Which will recursively search all .tpl files for you without the need to rely on find.

share|improve this answer
This might cause problems depending on how much *.tpl files the poster has. If there are more files than ARG_MAX this will result in an error. This problem can be avoided by using xargs. – pacey Nov 24 '10 at 9:57

In addition to the xargs solution, you can do find . -iname '*.tpl' -execdir dos2unix {} +. (Or just -exec on older versions of find — the end effect is the same.)

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.