How to automate dos2unix using shell script?

I have a bunch of xml files in the directory where the dos2unix command should be executed, and new files will be added so often. i Instead of manually executing the dos2unix command for each file every time, I would like to automate all this with a script. I have never even looked at a shell script in my life, but so far I have had this from what I read in several tutorials:

FILES=/tmp/testFiles/* for f in $FILES do fname=`basename $f` dos2unix *.xml $f $fname done 

However, I continue to output the "use" output. I think the problem is that I am not correctly assigning a new file name (fname). Can someone help.

Thanks Alan

+6
source share
4 answers

The reason you get a usage message is because dos2unix does not accept the extra arguments that you supply. However, it will accept multiple file names (also through globes). You do not need a loop if you are not processing more files than can be accepted on the command line.

 dos2unix /tmp/testFiles/*.xml 

It should be all you need if you don't need recursion:

 find /tmp/testFiles -name '*.xml' -exec dos2unix {} + 

(for GNU search)

+6
source

If all the files are in the same directory (no recursion is necessary), you are almost there.

 for file in /tmp/testFiles/*.xml ; do dos2unix "$file" done 

By default, dos2unix should convert to place and overwrite the original.

If you need recursion, you will also need to use find :

 find /tmp/testFiles -name '*.xml' -print0 | while IFS= read -d '' file ; do dos2unix "$file" done 

That will work with all files ending in .xml in /tmp/testFiles/ and in all its subdirectories.

If no other step is required, you can skip the shell loop completely:

nonrecursive:

 find /tmp/testFiles -maxdepth 1 -name '*.xml' -exec dos2unix {} + 

And for recursive:

 find /tmp/testFiles -name '*.xml' -exec dos2unix {} + 

In the original command, I see that you find the base name of each file name and try to transfer it to dos2unix , but your intentions are not clear. Later in the comment, you say that you just want to overwrite the files. My solution performs the conversion, does not back up and overwrites the original with the converted version. Hope this was your intention.

+2
source
 mkdir /tmp/testFiles/converted/ for f in /tmp/testFiles/*.xml do fname=`basename $f` dos2unix $f ${f/testFiles\//testFiles\/converted\/} # or for pure sh: # dos2unix $f $(echo $f | sed s@testFiles /@testFiles/converted/@) done 

The result will be saved in the converted/ subdirectory.

The construction ${f/testFiles\//testFiles\/converted\/} (thanks to Rush) or sed used to add converted/ before the file name:

 $ echo /tmp/testFiles/1.xml | sed s@testFiles /@testFiles/converted/@ /tmp/testFiles/converted/1.xml 
+1
source

It is unclear which dos2unix implementation you are using. Different implementations require different arguments. There are many different implementations.

In RedHat / Fedora / Suse Linux you can simply type

  dos2unix /tmp/testFiles/*.xml 

On SunOS, you need to specify the name of the input and output files, and the above command will destroy several of your files.

Which version are you using?

Regards, Erwin

0
source

Source: https://habr.com/ru/post/919495/


All Articles