convert dos2unix line endings for all files in a directory

后端 未结 8 522
慢半拍i
慢半拍i 2021-01-31 09:42

Currently, I am using the following command to change dos2unix line endings, but this is for file-y file...

sed -i \'s/\\r//\' filename

Is ther

相关标签:
8条回答
  • 2021-01-31 10:19

    Just adding another reference ...

    On Ubuntu 14.04 the man dos2unix page provides this example, specifically for recusive conversion:

    find . -name *.txt | xargs dos2unix

    Quoting from the man page, this will

    ... convert all the .txt files in the directory tree under the current directory ...

    0 讨论(0)
  • 2021-01-31 10:36

    The simplest way to run dos2unix against an entire directory recursively is to just have the find command execute it for each file it finds based on your criteria.

    For example, to find all files (excluding directory names) in your current directory and all its sub-directories and have dos2unix do the default conversion on each of those files:

    find . -type f -exec dos2unix -k -s -o {} ';'
    

    This will output every single filename, but not necessarily change each of them. This works as follows:

    find .: Find anything in this directory, including its subdirectories, and anything in those subdirectories as well (recursion)
    -type f: Only return 'regular file' names. Exclude folder names from the results.
    -exec: Execute the following command for every result. Everything beyond this point should be treated as part of that command until the ; character is found.
    dos2unix: dos2unix will be executed with the following options...
    -k: Keep the date stamp of the output file the same as the input file
    -s: Skip binary files (images, archives, etc.). This option is included by default, but I use it anyway in case that default were different on some systems (e.g. OS X v. Debian v. CentOS v. Ubuntu v. ...).
    -o: Write the changes directly to the file, rather than creating a new file with the data in the new format.
    {}: This tells find to insert the filename it has found as a parameter of the dos2unix call.
    ';': Tell find that the params for dos2unix have ended. Anything beyond this point will again be treated as a parameter of find.

    0 讨论(0)
  • 2021-01-31 10:37
    for i in `find . -type f \( -name "*.c" -o -name "*.h" \)`; do    sed -i 's/\r//' $i ; done
    
    0 讨论(0)
  • 2021-01-31 10:37

    Another option: find your_dir type f -exec sed -i 's/\r//' {} \;

    Why aren't you using dos2unix command? If your'e doing just the sed above there might be files (depending on their source) that will have incorrect EOF (you can see it if you run cat and the prompt afterwards is concatenated to the file output)

    0 讨论(0)
  • 2021-01-31 10:37

    use

    for i in `ls directory_path`; do    sed -i 's/\r//' $i ; done
    

    for entire directory.

    0 讨论(0)
  • 2021-01-31 10:39
    find $(pwd) -type f | xargs -I xxx sed -i 's/\r//g' xxx
    
    0 讨论(0)
提交回复
热议问题