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:23

Here is a partial answer that shows the directory names.

ls -mR * | sed -n 's/://p'

Explanation:

ls -mR * lists the full directory names ending in a ':', then lists the files in that directory separately

sed -n 's/://p' finds lines that end in a colon, strip off the colon and print the line

By iterating over the list of directories, we should be able to find the directories as well. Still workin on it. It is a challenge to get the wildcards through xargs.

查看更多
闹够了就滚
3楼-- · 2019-01-09 21:23

ls -lR is what you were looking for, or atleast I was. cheers

查看更多
老娘就宠你
4楼-- · 2019-01-09 21:26

I knew the file name but wanted the directory as well.

find $PWD | fgrep filename

worked perfectly in Mac OS 10.12.1

查看更多
太酷不给撩
5楼-- · 2019-01-09 21:27

find / will do the trick

查看更多
Lonely孤独者°
6楼-- · 2019-01-09 21:27
tar cf - $PWD|tar tvf -             

This is slow but works recursively and prints both directories and files. You can pipe it with awk/grep if you just want the file names without all the other info/directories:

tar cf - $PWD|tar tvf -|awk '{print $6}'|grep -v "/$"          
查看更多
该账号已被封号
7楼-- · 2019-01-09 21:28

Using no external commands other than ls:

ls -R1 /path | 
  while read l; do case $l in *:) d=${l%:};; "") d=;; *) echo "$d/$l";; esac; done

查看更多
登录 后发表回答