I have many *.c
and *.h
files in a directory. I want to convert all these files to Linux
compatible format. I've attempted following script and it runs but nothing get converted.
And I also need to check if everything get converted successfully. Therefore I filter and compare its output to the original file in the directory.
How could I fix it?
#!/bin/bash
function converting_files() {
cd "/path/to/dir" && find . -type f -print0 | xargs -0 | dos2unix
}
function success_of_converting_files() {
FILES=colsim_xfig/xfig.3.2.5c/*
#There are 250 files in the dir and not all but most of them are .c and .h
for i in {000..249} ; do
for f in "$Files" ; do
#I think *.txt line ending is fine as we only want to compare
dos2unix < myfile{i}.txt | cmp -s - myfile{i}.txt
done
done
}
function main() {
converting_files
success_of_converting
}
I basically need to convert all files to LF
line endings.
p.S: Total number of files in the directory is 249. The number of files in the directory is not fixed so, it would be better if I could have an arbitrary number of arguments instead of just 249.
In the command
you are piping a null-separated list of filenames to
xargs
, but not supplying a command to run on them. In this case,xargs
defaults to executing/bin/echo
on them: in other words, it just outputs a space-separated list of filenames on standard output, which you then pipe todos2unix
. The result is that instead of converting the files to Unix format, you just convert the list of filenames.Presumably what you intended was
However you could achieve the same more compactly using the
find
command's-exec
or-execdir
e.g.or (to restrict to
.c
and.h
files)