List files in current directory with full path using Bash

空扰寡人 提交于 2019-12-11 19:48:59

问题


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


来源:https://stackoverflow.com/questions/22992790/list-files-in-current-directory-with-full-path-using-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!