简体   繁体   English

如何按路径对linux find命令进行排序

[英]how to sort the linux find command by the path

I am writing a small shell script. 我正在写一个小的shell脚本。 there i want to get all the paths of files recursively. 在那里,我想递归获取文件的所有路径。 for that i am using bellow code. 为此,我正在使用波纹管代码。

for entry in `find . -type f`; do
       echo $entry 
done

getting the files recursively is okay. 递归获取文件是可以的。 but it does not sort by path. 但它不会按路径排序。

ex : the folder has 1,2,3,rename.sh (The shell script) files and a folder called test. ex:该文件夹包含1,2,3,rename.sh(shell脚本)文件和一个名为test的文件夹。 inside that test folder again there are 1,2, 3 files. 再次在该测试文件夹中有1,2,3个文件。 when i execute this code the out put is like this 当我执行此代码时,输​​出是这样的

./rename.sh
./1.png
./test/1.png
./test/2.jpg
./test/3.jpg
./2.jpg
./3.jpg

why is it not sorted by path. 为什么不按路径排序。 How do i sort it by the path so the out put wold be 我如何按路径对它进行排序,以便输出

./rename.sh
./1.png
./2.jpg
./3.jpg
./test/1.png
./test/2.jpg
./test/3.jpg

If you want content sorted, do it yourself . 如果您要对内容进行排序, 请自己进行 For a list of files, doing that safely means NUL-delimiting the names (so a file with a newline in its name doesn't get read as the names of two separate files and thus split apart during the sort process), and using sort -z (a GNU extension). 对于文件列表,安全地进行操作意味着用NUL分隔名称(因此,名称中带有换行符的文件不会被读取为两个单独文件的名称,因此在排序过程中会分开),并使用sort -z (GNU扩展)。

while IFS= read -r -d '' entry; do
  printf 'Processing: %s\n' "$entry"
done < <(find . -type f -print0 | sort -z)

See Using Find for guidance on using find reliably, and BashFAQ #1 for a discussion of the while read construct used here. 请参阅使用查找使用的指导find可靠和BashFAQ#1对的讨论while read构建物用在这里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM