How to remove trailing whitespace of all files recursively?

前端 未结 15 1948
难免孤独
难免孤独 2020-12-07 06:58

How can you remove all of the trailing whitespace of an entire project? Starting at a root directory, and removing the trailing whitespace from all files in all folders.

相关标签:
15条回答
  • 2020-12-07 07:57

    Two alternative approaches which also work with DOS newlines (CR/LF) and do a pretty good job at avoiding binary files:

    Generic solution which checks that the MIME type starts with text/:

    while IFS= read -r -d '' -u 9
    do
        if [[ "$(file -bs --mime-type -- "$REPLY")" = text/* ]]
        then
            sed -i 's/[ \t]\+\(\r\?\)$/\1/' -- "$REPLY"
        else
            echo "Skipping $REPLY" >&2
        fi
    done 9< <(find . -type f -print0)
    

    Git repository-specific solution by Mat which uses the -I option of git grep to skip files which Git considers to be binary:

    git grep -I --name-only -z -e '' | xargs -0 sed -i 's/[ \t]\+\(\r\?\)$/\1/'
    
    0 讨论(0)
  • 2020-12-07 07:57

    This worked for me in OSX 10.5 Leopard, which does not use GNU sed or xargs.

    find dir -type f -print0 | xargs -0 sed -i.bak -E "s/[[:space:]]*$//"
    

    Just be careful with this if you have files that need to be excluded (I did)!

    You can use -prune to ignore certain directories or files. For Python files in a git repository, you could use something like:

    find dir -not -path '.git' -iname '*.py'
    
    0 讨论(0)
  • 2020-12-07 07:57

    Ack was made for this kind of task.

    It works just like grep, but knows not to descend into places like .svn, .git, .cvs, etc.

    ack --print0 -l '[ \t]+$' | xargs -0 -n1 perl -pi -e 's/[ \t]+$//'
    

    Much easier than jumping through hoops with find/grep.

    Ack is available via most package managers (as either ack or ack-grep).

    It's just a Perl program, so it's also available in a single-file version that you can just download and run. See: Ack Install

    0 讨论(0)
  • 2020-12-07 07:57

    Ruby:

    irb
    Dir['lib/**/*.rb'].each{|f| x = File.read(f); File.write(f, x.gsub(/[ \t]+$/,"")) }
    
    0 讨论(0)
  • 2020-12-07 07:59

    Use:

    find . -type f -print0 | xargs -0 perl -pi.bak -e 's/ +$//'
    

    if you don't want the ".bak" files generated:

    find . -type f -print0 | xargs -0 perl -pi -e 's/ +$//'
    

    as a zsh user, you can omit the call to find, and instead use:

    perl -pi -e 's/ +$//' **/*
    

    Note: To prevent destroying .git directory, try adding: -not -iwholename '*.git*'.

    0 讨论(0)
  • 2020-12-07 08:00

    Here is an OS X >= 10.6 Snow Leopard solution.

    It Ignores .git and .svn folders and their contents. Also it won't leave a backup file.

    export LC_CTYPE=C
    export LANG=C
    find . -not \( -name .svn -prune -o -name .git -prune \) -type f -print0 | xargs -0 sed -i '' -E "s/[[:space:]]*$//"
    
    0 讨论(0)
提交回复
热议问题