3

I know how to change file format from dos to unix by use dos2unix, but how can I change ALL the files will under a directory tree. Can dos2unix change files recursively?

for example, I have some files like following:

TOPDIR
|
+-----dir1
|      |
|      +---file1,file2, file3
|
+-----dir2
       |
       +---file4,file5

How can I change them in one time, or use some shell scripts?

How Chen
  • 1,340
  • 2
  • 17
  • 37
  • 3
    You should start to play around with unix commands. There is no other way to learn how to use the power they offer. You have to try yourself and explore and conquer that environment step by step. Do it, it is worth it. – arkascha Feb 21 '13 at 06:32
  • Regarding that, it's always helpful to read the manpage or `$CMD --help`, since some programs (e.g. `grep`) have a recursive flag `-R` that does this for you. Also if the count of files is not too high (i.e. in the order of a few hundred) you can use the extended glob capabilities of shells like `zsh` to do `dos2unix **/*`. – filmor Feb 21 '13 at 08:12
  • @filmor, I only can use `bsh` on my server, and seems `dos2unix` has no `-R` option – How Chen Jan 22 '15 at 01:46

3 Answers3

7

better to do find /path -type -f -exec dos2unix '{}' \;

  • +1. I copy-paste from my example with *.c pattern, but for all files -type f is better. :-) – Sergei Nikulov Feb 21 '13 at 06:51
  • @Andrey Dmitriev, what is the different between: `find /path -type -f -exec dos2unix '{}' \;` and `find /path -type f -exec dos2unix {} +` – How Chen Feb 21 '13 at 07:00
  • The plus syntax spawns fewer jobs; with the semicolon it's one new process for every file. – tripleee Feb 21 '13 at 07:03
  • on debian (wheezy) i get `find: Arguments to -type should contain only one letter` so i had to use `find /path/ -type f -exec dos2unix '{}' \;` – Christoph Lösch Oct 01 '16 at 13:06
1

find /path -name '*' -type f -exec dos2unix {} \;

Unknown
  • 5,722
  • 5
  • 43
  • 64
  • 1
    Why did you add `-name '*'`? – filmor Feb 21 '13 at 06:45
  • In case you need to apply `dos2unix` to some files only, makes the example more easy to understand, imo (e.g. you can write `-name '*.c'`) – Unknown Feb 21 '13 at 07:21
  • ...Maybe the thinking behind this answer is that `-name '*'` would exclude dot files (`.*`)? Well, in any case, it doesn't (it used to in some cases, but now it doesn't -- so don't assume it does). For example, `.foo` files or files in `.git` directories would be processed. Those files would have to be excluded, e.g., `find path \( -name ".*" -prune \) -o \( -type f -print \)` (note: you couldn't start this with `find . (etc)` or else it would exclude everything) – michael Feb 21 '13 at 07:22
0
dos2unix -k `find . -type f`
find . -type f -exec dos2unix -k '{}' \;
find . -type f -print | xargs dos2unix -k

Any of above command can be used from TOPDIR

Sergei Nikulov
  • 5,029
  • 23
  • 36