如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:40:41  来源:igfitidea点击:

How can I generate a list of files with their absolute path in Linux?

linuxcommand-lineabsolute-pathls

提问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 barhas the path:

出于这个原因,我需要生成具有完整路径的递归文件列表。例如,该文件bar具有以下路径:

/home/ken/foo/bar

but, as far as I can see, both lsand findonly 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 findor lsman pages.

这似乎是一个明显的要求,但我在手册页findls手册页中看不到任何内容。

How can I generate a list of files in the shell including their absolute paths?

如何在 shell 中生成文件列表,包括它们的绝对路径?

采纳答案by Matthew Scharley

If you give findan 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 $PWDto the current directory:

或者如果您的 shell 扩展$PWD到当前目录:

find "$PWD" -name .htaccess

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

find简单地将它提供的路径添加到从该路径到文件的相对路径。

Greg Hewgillalso suggested using pwd -Pif you want to resolve symlinks in your current directory.

pwd -P如果您想解析当前目录中的符号链接,Greg Hewgill还建议使用。

回答by David Arno

find / -printwill 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 -land have it return the absolute path, you can just tell the find command to execute an ls -lon 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 $PWDis 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 会自动完成到您的主目录。