简体   繁体   中英

List files in current directory with full path using Bash

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?

代替解析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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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