From 3cd92cd9d4586ef58e9813da84fd41c7f3bf53f3 Mon Sep 17 00:00:00 2001 From: "Matthew O. Persico" Date: Tue, 2 Apr 2019 15:45:40 -0400 Subject: [PATCH] Added an example of moving from ls to find. --- SC2012.md | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/SC2012.md b/SC2012.md index d1b294c..97f0efc 100644 --- a/SC2012.md +++ b/SC2012.md @@ -1,4 +1,4 @@ -## Use find instead of ls to better handle non-alphanumeric filenames. +## Use `find` instead of `ls` to better handle non-alphanumeric filenames. ### Problematic code: @@ -28,7 +28,48 @@ total 0 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. +### Tips for replacing `ls` with `find`: + +#### Just the filenames, ma'am + +`ls` can usually be replaced by `find` if it's just the filenames you're after. Note that if you are using `ls` to get at the contents of a directory, a straight substitution of `find` may not yield the same results as `ls`. Here is an example: + +``` +$ ls -c1 .snapshot +rnapdev1-svm_4_05am_6every4hours.2019-04-01_1605 +rnapdev1-svm_4_05am_6every4hours.2019-04-01_2005 +rnapdev1-svm_4_05am_6every4hours.2019-04-02_0005 +rnapdev1-svm_4_05am_6every4hours.2019-04-02_0405 +rnapdev1-svm_4_05am_6every4hours.2019-04-02_0805 +rnapdev1-svm_4_05am_6every4hours.2019-04-02_1205 +snapmirror.1501b4aa-3f82-11e8-9c31-00a098cef13d_2147868328.2019-04-01_190000 +``` +versus +``` +$ find .snapshot -maxdepth 1 +.snapshot +.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-02_0005 +.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-02_0405 +.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-02_0805 +.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-01_1605 +.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-01_2005 +.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-02_1205 +.snapshot/snapmirror.1501b4aa-3f82-11e8-9c31-00a098cef13d_2147868328.2019-04-01_190000 +``` +You can see two differences here. The first is that the `find` output has the full paths to the found files, relative to the current working directory from which `find` was run whereas `ls` only has the filenames. You may have to adjust your code to not add the directory to the filenames as you process them when moving from `ls` to `find`. + +The second difference in the two outputs is that the `find` command includes the searched directory as an entry. This can be eliminated by always using a negative name option for the searched directory: +``` +$ find .snapshot -maxdepth 1 ! -name .snapshot +.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-02_0005 +.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-02_0405 +.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-02_0805 +.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-01_1605 +.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-01_2005 +.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-02_1205 +.snapshot/snapmirror.1501b4aa-3f82-11e8-9c31-00a098cef13d_2147868328.2019-04-01_190000 +``` +#### All the other info 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.