#!/bin/sh if [ $# -eq 0 ]; then echo "dos2unix whole directory, keeping timestamps" echo "" echo "$0 " echo "it will copy contents of to changing new lines from dos to unix" exit 0 fi # reference dir: DIR0="${1}" # new dir: DIR1="${2}" if [ -d "${DIR1}" ]; then echo "directory ${DIR1} exists, do you want to continue? [y/N]" read ANS if [ "${ANS}" == "y" ] || [ "${ANS}" == "Y" ]; then continue else exit 0 fi fi cd "${DIR0}" SCRIPT="d2u-dir-XXXXX.sh" echo '#!/bin/sh' > "../${SCRIPT}" RM_TRAILING="y" # comment or set empty string if you want to keep trailing spaces if [ $RM_TRAILING ]; then TRS=' | sed -e "s/\s*$//"' else TRS="" fi # extensions to be excluded from processing (binary) # binaries/executables EXCL_EXT='\.a$|\.dsp$|\.dsw$|\.exe$|\.lib$|\.pyc$|\.res$|\.sfx$' # crlf text EXCL_EXT="$EXCL_EXT"'|\.dsp$|\.dsw$' # images EXCL_EXT="$EXCL_EXT"'|\.bmp$|\.cur$|\.gif$|\.ico$|\.jpg$|\.jpeg$|\.png$|\.tga$' # archives EXCL_EXT="$EXCL_EXT"'|\.7z$|\.bz2$|\.gz$|\.lz$|\.rar$|\.tar$|\.xz$|\.zip$|\.zstd$' # documents EXCL_EXT="$EXCL_EXT"'|\.chm$|\.doc$|\.docx$|\.hlp$|\.odf$|\.odg$|\.odp$|\.ods$|\.odt$|\.pdf$|\.xls$|\.xlsx$' # audio EXCL_EXT="$EXCL_EXT"'|\.flac$|\.mp3$|\.ogg$|\.wav$|\.wv$' # video EXCL_EXT="$EXCL_EXT"'|\.3gp$|\.avi$|\.m4a$|\.mp4$|\.ogv$|\.webm$' find . -type d -exec echo "mkdir -p ${DIR1}/{}" ";" >> "../${SCRIPT}" echo "" >> ../${SCRIPT} find . -type f -exec echo "d2u ${DIR0}/{} $TRS > ${DIR1}/{}; touch -r ${DIR0}/{} ${DIR1}/{}" ";" | grep -iEv "$EXCL_EXT" >> "../${SCRIPT}" find . -type f -exec echo "cat ${DIR0}/{} > ${DIR1}/{}; touch -r ${DIR0}/{} ${DIR1}/{}" ";" | grep -iE "$EXCL_EXT" >> "../${SCRIPT}" echo "" >> ../${SCRIPT} find . -type d -exec echo "touch -r ${DIR0}/{} ${DIR1}/{}" ";" >> "../${SCRIPT}" echo "" >> ../${SCRIPT} cd .. sh ${SCRIPT} rm ${SCRIPT} # delete or keep script for debugging