diff --git a/SC2012.md b/SC2012.md new file mode 100644 index 0000000..211166f --- /dev/null +++ b/SC2012.md @@ -0,0 +1,31 @@ +## Use find instead of ls to better handle non-alphanumeric filenames. + +### Problematic code: + + ls -l | grep " $USER " | grep '\.txt$' + +### Correct code: + + find . -maxdepth 1 -name '*.txt' -user "$USER" + +### Rationale: + +`ls` is only intended for human consumption: it has a loose, non-standard format and may "clean up" filenames to make output easier to read. + +Here's an example: + + $ ls -l + total 0 + -rw-r----- 1 me me 0 Feb 5 20:11 foo?bar + -rw-r----- 1 me me 0 Feb 5 2011 foo?bar + -rw-r----- 1 me me 0 Feb 5 20:11 foo?bar + +It shows three seemingly identical filenames, and did you spot the time format change? How it formats and what it redacts can differ between locale settings, `ls` version, and whether output is a tty. + +`ls` can usually be substituted for `find` if it's the filenames you're after. + +If trying to parse out any other fields, first see whether `stat` (GNU, OS X, FreeBSD) or `find -printf` (GNU) can give you the data you want directly. + +### Exceptions: + +If the information is intended for the user and not for processing (`ls -l ~/dir | nl; echo "Ok to delete these files?"`) you can ignore this message (add a [[directive]]). \ No newline at end of file