使用Bash列出具有完整路径的当前目录中的文件 [英] List files in current directory with full path using Bash
本文介绍了使用Bash列出具有完整路径的当前目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
情况
我具有以下结构:
+ Folder 1
--20140410.txt
--20140409.txt
--20140408.txt
--20140407.txt
--20140406.txt
--20140405.txt
--20140404.txt
+ Folder 2
--20140410.txt
--20140409.txt
--20140408.txt
--20140407.txt
--20140406.txt
--20140405.txt
--20140404.txt
我需要最新"(取决于文件名)该目录中的5个文件,包括Path. 目前,我正在使用以下代码来做到这一点:
I need the "newest" (depending on the file name) 5 Files from that directory, including Path. Currently i'm using the following code to do so:
for i in `find /mydirectory/ -type d` ; do cd $i && ls *.* | sort -n -r | head -5 >> /mydirectory/myfile.txt ; done
因此,对于我找到的每个目录,我都会列出文件,然后按名称对它们进行反排序,并在第5行后剪切.
So for every directory I find, I list the files, inverse-sort them by name and cut after line 5.
结果如下:
20140410.txt
20140409.txt
20140408.txt
20140407.txt
20140406.txt
20140410.txt
20140409.txt
20140408.txt
20140407.txt
20140406.txt
如何在文件名之前"写入当前工作目录?
How do i write the current working directory "before" the file name?
推荐答案
代替解析ls ,使用再次find
:
for i in $(find /mydirectory/ -type d); do cd $i && find $PWD -type f -name "*.*" | sort -nr | head -5 >> /mydirectory/myfile.txt; done
这篇关于使用Bash列出具有完整路径的当前目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文