I'm looking for a bash command to find files with trailing spaces at the end of each line. I'm not interested in removing the spaces, but just in finding the files.
-
4Note: While the wording of the question (as of this writing) unambiguously calls for finding files with trailing spaces on _each and every line_, the unusualness of this requirement has resulted in some answers - notably the one with the most up-votes - finding files with _one or more_ lines with trailing spaces instead. – mklement0 Mar 27 '15 at 20:39
-
sed -n '/ \+$/p' filename – Luv33preet Mar 11 '17 at 11:27
8 Answers
Find files that has trailing spaces.
find . -type f -exec egrep -l " +$" {} \;

- 7,296
- 1
- 25
- 22
-
While you can make do with just `grep`, as other answers have demonstrated, using `find` may still be a good idea, if you want more control over what files to examine., You can make the `find` command much more efficient by using `+` instead of `\;` to terminate the `-exec` primary - this will invoke `egrep` (typically) only _once_, with _all_ filenames. – mklement0 Mar 27 '15 at 20:32
-
1Note that this answer finds files with _at least 1_ line with trailing spaces. This can definitely be useful and is probably the more common use case, but is not what the OP asked for (at least as implied by their wording, "at the end of _each_ line"). – mklement0 Mar 27 '15 at 20:41
If the goal is to list files which have trailing whitespaces in one or more lines:
grep -r '[[:blank:]]$' .
To not print the lines, and print just the file names only, also specify the -l
option. That's l
as in the word list
, not the number 1
.

- 57,944
- 17
- 167
- 143
-
1
-
1@Sorin I'm gonna guess that wasn't quite the intention of the OP, but will admit that the question does actually say that, taken literally... – twalberg Mar 27 '15 at 19:33
-
@twalberg, it's what makes it a not trivial question, that would be worth the third nearly identical answer – Sorin Mar 27 '15 at 19:42
-
@Sorin In that case, perhaps `grep -rL '[^[:blank]]$'` would work... Searches for files that have a line where the character immediately preceding the newline is not a blank, and report all files that *don't* match that pattern... – twalberg Mar 27 '15 at 19:49
-
@twalberg, of course that would work, but that's not the point of this discussion ;) – Sorin Mar 27 '15 at 19:52
-
2The OP's question, if taken literally, has absolutely no real world applicability that I am aware of. IMHO it should be edited to reflect the common use case and also the most upvoted answer. The question is after all also the top google result for `grep trailing whitespace`. Are there any objections to editing the question as such? – Asclepius Mar 27 '15 at 21:20
-
Find files with one or more trailing space characters:
find . -name "*" | xargs egrep ".* +$"

- 4,887
- 9
- 42
- 73
-
I believe the original question was to find files which have trailing spaces on ALL lines. Could be just bad wording originally, though. – Etheryte Jun 26 '12 at 15:07
-
@Nit it does say clearly in the body, must have missed that, but the question header can be ambiguous. – matcheek Jun 26 '12 at 15:10
-
Expanding on your solution, though, `egrep -m1 -v -l ".* +$"` should find all files that **don't** have trailing spaces on every line, if I'm not mistaken? – Etheryte Jun 26 '12 at 15:18
-
If the former always holds true, then `LIST=`find * | xargs egrep -m1 -v -l ".* +$" | xargs -I {} echo {}`;echo -e "$LIST""\n""`find *`"` should enlist all files which don't meet the criteria twice, and the files we want to list only once. However, it appears I've broken a pipe somewhere so I can't use `uniq` on the result. Any ideas? – Etheryte Jun 26 '12 at 15:42
-
6simpler regex: `' $'` or to include both tabs and spaces: `'[[:blank:]]$'` – Dennis Williamson Jun 26 '12 at 16:28
-
2Causes various warnings `egrep: (path): Is a directory`. Be sure to specificy `-type f` to only pass file names to grep. – Timo Tijhof Jun 18 '13 at 06:55
There is an option to list the files which do not contain a match anywhere in them; use that and a regex for a character other than a space just before end of line.
grep -L '[^ ]$' *
To recurse directories, add -r
. To search for other whitespace characters as well, use a character class $'[^ \t]$'
or the POSIX '[^[:blank:]]$'
for the regex.

- 175,061
- 34
- 275
- 318
-
Nicely done. This truly only returns files ALL of whose lines have trailing spaces - with one exception: _empty_ (zero-byte) files are matched, too. Also, using `*` (without `-r`) will result in warnings for subdirectories, if any. `-L` is not POSIX-compliant, but widely supported, given that both GNU grep and BSD sed implement it. – mklement0 Mar 27 '15 at 21:02
-
1@mklement0 Nice catch about the zero-sized files. You can use `-s` to suppress warnings about directories, or use `-r .` if you want to traverse an entire directory tree (the OP was unspecific about the task). – tripleee Mar 27 '15 at 21:08
If the goal is to list files with trailing whitespace in the current path:
grep -rli '[[:blank:]]$' .

- 8,621
- 29
- 30
ack -l ' \n'
Note: Like some of the other answers, this will list files that contain one or more lines with trailing spaces.

- 33,549
- 14
- 108
- 109
If the question is literally to find files that have a blank at the end of every single line, then this should work:
grep -rL '[^[:blank:]]$' .
The -L
tells grep
to report every file that does not match the pattern, and the pattern is looking for lines that do not have a blank immediately preceding the newline.

- 59,951
- 11
- 89
- 84