List files in current directory with full path usi

2019-09-01 11:02发布

问题:

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:

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