List files in current directory with full path usi

2019-09-01 10:41发布

Situation

I have the following Structure:

+ 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

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

So for every directory I find, I list the files, inverse-sort them by name and cut after line 5.

Here's the result:

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?

1条回答
成全新的幸福
2楼-- · 2019-09-01 10:58

Instead of parsing ls, use find again:

for i in $(find /mydirectory/ -type d); do cd $i && find $PWD -type f -name "*.*" | sort -nr | head -5 >> /mydirectory/myfile.txt; done
查看更多
登录 后发表回答