0

I want to run the dos2unix command on all files inside a folder. The issue here is the folder contains files and sub-folders. The sub-folder again contains files and folders. So if I simply run user/bin/dos2unix <folder>, it will throw an error since there are sub-folders and folders present in the main folder.

Any idea on how I can bypass this so that the dos2unix command would run on the files inside each of the folders?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Ban Midou
  • 63
  • 2
  • 10

3 Answers3

4

just run the below command from where you want to execute your dos2unix command. Change the below "*.txt" as per your file extension type. If they are different in different directory location then make it "*.*"

find . -name "*.txt" | xargs dos2unix

Here . is your current directory and "*.txt" are all yourdos` files.

How will the above command work? It will find all the dos files *.txt and input each file as parameter to dos2unix command and which will convert that dos format file to unix format.

If you want to run it from any location just provide full path in place of . like below:-

find /path/from/where/you/want/to -name "*.txt" | xargs dos2unix
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
1
find <folder> -type f | xargs dos2unix

Explanation:

  • find <folder> -type f will list all the regular files inside <folder> and its sub-folders
  • xargs dos2unix will apply the dos2unix command to each of the files found by find
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
1

xargs isn't needed here; just use the -exec primary to find:

find -type f -exec dos2unix {} +
chepner
  • 497,756
  • 71
  • 530
  • 681