[英]How to sort output of the find command in a script
I have the following simple script to find all directories (at a depth of 2) that were added in the last N days... 我有以下简单的脚本来查找最近N天添加的所有目录(深度为2)...
#!/bin/bash
DAYS_PRIOR=180
DIR='/mydir'
FILES=`find $DIR -mindepth 2 -maxdepth 2 -type d -mtime -$DAYS_PRIOR -printf '%f\\\n'`
echo
echo "Files added in the last $DAYS_PRIOR days:"
echo
echo -e $FILES
echo
To get it to add newlines I had to double-escape the printf and use echo -e
. 为了使其添加换行符,我不得不两次转义printf并使用
echo -e
。 That seems odd to me but it was the only way I could get it to print one directory per line on the output. 这对我来说似乎很奇怪,但这是我唯一可以在输出的每行打印一个目录的方法。
Everything works up to this point and I get a list of directories as expected. 至此一切正常,我得到了预期的目录列表。 Now I want to sort the list alphabetically.
现在,我想按字母顺序对列表进行排序。 I tried changing the printf in the find command to...
我尝试将find命令中的printf更改为...
FILES=`find <xxx> -printf '%f\\\n' | sort`
however this doesn't sort the directory names. 但是,这不会对目录名称进行排序。 Based on other posts I tried the following..
根据其他帖子,我尝试了以下操作。
FILES=`find <xxx> -printf %f\\\n | sort -t '\0' | awk -F '\0' '{print $0; print "\\\n"}'`
This is very close but leaves an extra space at the start of each line and seems horribly awkward. 这非常接近,但是在每行的开头都留出了额外的空间,并且看上去很尴尬。
Is there a simple method to add a sort to the original find
command? 是否有一种简单的方法可以将排序添加到原始的
find
命令?
First: double-quote your variable references! 第一:双引号引用您的变量! When you use
echo -e $FILES
, the variable FILE
's value gets split into "words" based on whitespace (spaces, tabs, and newlines), and then echo
sticks those words back together with spaces between them. 当您使用
echo -e $FILES
,变量FILE
的值将基于空格(空格,制表符和换行符)分成“单词”,然后echo
这些单词及其之间的空格粘在一起。 This has the effect of converting newlines into spaces. 这具有将换行符转换为空格的作用。 In order to wind up with newlines at the end, you're having to use
\\n
instead of a true newline, and use echo -e
to convert it. 为了最后使用换行符,您必须使用
\\n
而不是真正的换行符,并使用echo -e
进行转换。 Just use real newlines, and put double-quotes around the variable reference to avoid all this mess: 只需使用实际的换行符,然后在变量引用周围加上双引号即可避免所有此类混乱:
FILES=$(find "$DIR" -mindepth 2 -maxdepth 2 -type d -mtime "-$DAYS_PRIOR" -printf '%f\n')
# ...
echo "$FILES"
Note that I put double-quotes around all variable references, since this is almost always a good idea. 请注意,我在所有变量引用周围都加上了双引号,因为这几乎总是一个好主意。 I also used
$( )
instead of backticks -- it's easier to read, and avoids some parsing oddities that backticks have. 我还使用
$( )
代替了反引号-它更易于阅读,并且避免了反引号具有的某些解析奇数。
Anyway, with this format you're using proper newlines throughout, so piping through sort
should work as expected. 无论如何,使用这种格式,您在整个过程中都使用适当的换行符,因此通过
sort
管道应该可以按预期工作。
BTW, I'd also recommend switching from uppercase variable names to lower- or mixed-case names, since there are a bunch of all-caps names that have special meanings, and if you accidentally use one of them bad things can happen. 顺便说一句,我还建议从大写变量名切换为小写或大小写混合名,因为有一堆具有特殊含义的全大写名称,如果不小心使用其中之一,可能会发生不好的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.