How can I list files with their absolute path in l

2019-01-07 01:20发布

I want to generate recursive file listings with full paths

/home/ken/foo/bar

but as far as I can see both ls and find only give relative path listings

./foo/bar   (from the folder ken)

It seems like an obvious requirement but I can't see anything in the find or ls man pages.

10条回答
时光不老,我们不散
2楼-- · 2019-01-07 01:38

You can use

find $PWD 

in bash

查看更多
该账号已被封号
3楼-- · 2019-01-07 01:39
ls -1 | awk  -vpath=$PWD/ '{print path$1}'
查看更多
来,给爷笑一个
4楼-- · 2019-01-07 01:46

Use this for dirs:

ls -d -1 $PWD/**

this for files:

ls -d -1 $PWD/*.*

this for everything:

ls -d -1 $PWD/**/*

Taken from here http://www.zsh.org/mla/users/2002/msg00033.html

查看更多
地球回转人心会变
5楼-- · 2019-01-07 01:46
ls -d $PWD/*
查看更多
做自己的国王
6楼-- · 2019-01-07 01:48

If you give find an absolute path to start with, it will print absolute paths. For instance, to find all .htaccess files in the current directory:

find `pwd` -name .htaccess

find simply prepends the path it was given to a relative path to the file from that path.

Greg Hewgill also suggested using pwd -P if you want to resolve symlinks in your current directory.

查看更多
淡お忘
7楼-- · 2019-01-07 01:49

The $PWD is a good option by Matthew above. If you want find to only print files then you can also add the -type f option to search only normal files. Other options are "d" for directories only etc. So in your case it would be (if i want to search only for files with .c ext):

find $PWD -type f -name "*.c" 

or if you want all files:

find $PWD -type f

Note: You can't make an alias for the above command, because $PWD gets auto-completed to your home directory when the alias is being set by bash.

查看更多
登录 后发表回答