ls command: how can I get a recursive full-path li

2019-01-09 20:35发布

How can I get ls to spit out a flat list of recursive one-per-line paths?

For example, I just want a flat listing of files with their full paths:

/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow
/home/dreftymac/stackoverflow/alpha.txt
/home/dreftymac/stackoverflow/bravo.txt
/home/dreftymac/stackoverflow/charlie.txt

ls -a1 almost does what I need, but I do not want path fragments, I want full paths.

23条回答
太酷不给撩
2楼-- · 2019-01-09 21:08

@ghostdog74: Little tweak with your solution.
Following code can be used to search file with its full absolute path.

sudo ls -R / | awk '<br/>
/:$/&&f{s=$0;f=0}<br/>
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}<br/>
NF&&f{ print s"/"$0 }' | grep [file_to_search]
查看更多
We Are One
3楼-- · 2019-01-09 21:11

A lot of answers I see. This is mine, and I think quite useful if you are working on Mac.

I'm sure you know there are some "bundle" files (.app, .rtfd, .workflow, and so on). And looking at Finder's window they seem single files. But they are not. And $ ls or $ find see them as directories... So, unless you need list their contents as well, this works for me:

find . -not -name ".*" -not -name "." | egrep -v "\.rtfd/|\.app/|\.lpdf/|\.workflow/"

Of course this is for the working dir, and you could add other bundles' extensions (but always with a / after them). Or any other extensions if not bundle's without the /.

Rather interesting the ".lpdf/" (multilingual pdf). It has normal ".pdf" extension (!!) or none in Finder. This way you get (or it just counts 1 file) for this pdf and not a bunch of stuff…

查看更多
Emotional °昔
4楼-- · 2019-01-09 21:12

Don't make it complicated. I just used this and got a beautiful output:

ls -lR /path/I/need
查看更多
孤傲高冷的网名
5楼-- · 2019-01-09 21:17

If you really want to use ls, then format its output using awk:

ls -R /path | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }'
查看更多
爷的心禁止访问
6楼-- · 2019-01-09 21:17
du -a

Handy for some limited appliance shells where find/locate aren't available.

查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-09 21:19

I don't know about the full path, but you can use -R for recursion. Alternatively, if you're not bent on ls, you can just do find *.

查看更多
登录 后发表回答