如何在 Linux 中生成带有绝对路径的文件列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/246215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I generate a list of files with their absolute path in Linux?
提问by Ken
I am writing a shell script that takes file paths as input.
我正在编写一个将文件路径作为输入的 shell 脚本。
For this reason, I need to generate recursive file listings with full paths. For example, the file bar
has the path:
出于这个原因,我需要生成具有完整路径的递归文件列表。例如,该文件bar
具有以下路径:
/home/ken/foo/bar
but, as far as I can see, both ls
and find
only give relative path listings:
但是,据我所看到的,ls
并且find
只给出相对路径列表:
./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.
这似乎是一个明显的要求,但我在手册页find
或ls
手册页中看不到任何内容。
How can I generate a list of files in the shell including their absolute paths?
如何在 shell 中生成文件列表,包括它们的绝对路径?
采纳答案by Matthew Scharley
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
一个绝对路径开始,它会打印绝对路径。例如,要查找当前目录中的所有 .htaccess 文件:
find "$(pwd)" -name .htaccess
or if your shell expands $PWD
to the current directory:
或者如果您的 shell 扩展$PWD
到当前目录:
find "$PWD" -name .htaccess
find
simply prepends the path it was given to a relative path to the file from that path.
find
简单地将它提供的路径添加到从该路径到文件的相对路径。
Greg Hewgillalso suggested using pwd -P
if you want to resolve symlinks in your current directory.
pwd -P
如果您想解析当前目录中的符号链接,Greg Hewgill还建议使用。
回答by David Arno
find / -print
will do this
find / -print
会这样做
回答by Vinko Vrsalovic
You can use
您可以使用
find $PWD
in bash
在 bash
回答by didi
ls -d "$PWD/"*
This looks only in the currentdirectory. It quotes "$PWD" in case it contains spaces.
这仅在当前目录中查找。如果它包含空格,它会引用“$PWD”。
回答by Trudius
If you give the find command an absolute path, it will spit the results out with an absolute path. So, from the Ken directory if you were to type:
如果你给 find 命令一个绝对路径,它会用绝对路径输出结果。因此,如果您要从 Ken 目录中键入:
find /home/ken/foo/ -name bar -print
(instead of the relative path find . -name bar -print
)
(而不是相对路径find . -name bar -print
)
You should get:
你应该得到:
/home/ken/foo/bar
Therefore, if you want an ls -l
and have it return the absolute path, you can just tell the find command to execute an ls -l
on whatever it finds.
因此,如果你想要 anls -l
并让它返回绝对路径,你可以告诉 find 命令ls -l
在它找到的任何东西上执行 an 。
find /home/ken/foo -name bar -exec ls -l {} ;\
NOTE: There is a space between {}
and ;
注意:{}
和之间有一个空格;
You'll get something like this:
你会得到这样的东西:
-rw-r--r-- 1 ken admin 181 Jan 27 15:49 /home/ken/foo/bar
If you aren't sure where the file is, you can always change the search location. As long as the search path starts with "/", you will get an absolute path in return. If you are searching a location (like /) where you are going to get a lot of permission denied errors, then I would recommend redirecting standard error so you can actually see the find results:
如果您不确定文件在哪里,您可以随时更改搜索位置。只要搜索路径以“/”开头,就会得到绝对路径作为回报。如果您正在搜索一个位置(如 /),您将在其中获得大量权限被拒绝的错误,那么我建议您重定向标准错误,以便您可以实际看到查找结果:
find / -name bar -exec ls -l {} ;\ 2> /dev/null
(2>
is the syntax for the Borne and Bash shells, but will not work with the C shell. It may work in other shells too, but I only know for sure that it works in Bourne and Bash).
(2>
是用于 Borne 和 Bash shell 的语法,但不适用于 C shell。它也可以在其他 shell 中工作,但我只确定它在 Bourne 和 Bash 中工作)。
回答by Albert
ls -1 | awk -vpath=$PWD/ '{print path}'
回答by user431529
Use this for dirs (the /
after **
is needed in bash to limit it to directories):
将此用于目录(bash 中需要/
after**
将其限制为目录):
ls -d -1 "$PWD/"**/
this for files and directories directly under the current directory, whose names contain a .
:
这适用于直接位于当前目录下的文件和目录,其名称包含.
:
ls -d -1 "$PWD/"*.*
this for everything:
这一切:
ls -d -1 "$PWD/"**/*
Taken from here http://www.zsh.org/mla/users/2002/msg00033.html
取自这里 http://www.zsh.org/mla/users/2002/msg00033.html
In bash, **
is recursive if you enable shopt -s globstar
.
在 bash 中,**
如果启用shopt -s globstar
.
回答by balki
readlink -f filename
gives the full absolute path. but if the file is a symlink, u'll get the final resolved name.
给出完整的绝对路径。但如果文件是一个符号链接,你会得到最终解析的名称。
回答by rxw
lspwd() { for i in $@; do ls -d -1 $PWD/$i; done }
回答by Gurpreet
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):
这$PWD
是上面 Matthew 的一个不错的选择。如果您只想查找打印文件,那么您还可以添加 -type f 选项以仅搜索普通文件。其他选项是仅用于目录等的“d”。所以在你的情况下它会是(如果我只想搜索带有 .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.
注意:您不能为上述命令创建别名,因为当 bash 设置别名时,$PWD 会自动完成到您的主目录。