如何在 Linux 中生成带有绝对路径的文件列表?
我正在编写一个 shell 脚本,它将文件路径作为输入。
因此,我需要生成带有完整路径的递归文件列表。 例如,文件 bar
具有路径:
/home/ken/foo/bar
但是,据我所知, ls
和 find
都只给出相对路径列表:
./foo/bar (from the folder ken)
这似乎是一个明显的要求,但我在 find
或 ls
手册页中看不到任何内容。
如何在 shell 中生成文件列表(包括它们的绝对路径)?
I am writing a shell script that takes file paths as input.
For this reason, I need to generate recursive file listings with full paths. For example, the file bar
has the path:
/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.
How can I generate a list of files in the shell including their absolute paths?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(29)
如果你给
find
一个绝对路径作为开始,它会打印绝对路径。 例如,要查找当前目录中的所有 .htaccess 文件:或者如果您的 shell 将
$PWD
扩展到当前目录:find
只需在前面添加给定的路径即可从该路径到文件的相对路径。Greg Hewgill 还建议使用
pwd -P
如果您想解析当前中的符号链接目录。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:or if your shell expands
$PWD
to the current directory: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.给出完整的绝对路径。 但如果文件是符号链接,您将获得最终解析的名称。
gives the full absolute path. But if the file is a symlink, you'll get the final resolved name.
将此用于目录(bash 中需要
**
之后的/
将其限制为目录):这用于直接在当前目录下的文件和目录,其名称包含
.
:这适用于所有内容:
取自此处
http://www.zsh.org/mla/users/2002/msg00033.html
在 bash 中,如果启用
shopt -s globstar
,则**
是递归的。Use this for dirs (the
/
after**
is needed in bash to limit it to directories):this for files and directories directly under the current directory, whose names contain a
.
:this for everything:
Taken from here
http://www.zsh.org/mla/users/2002/msg00033.html
In bash,
**
is recursive if you enableshopt -s globstar
.您可以
在 bash 中使用
You can use
in bash
这只在当前目录中查找。 如果它包含空格,它会引用“$PWD”。
This looks only in the current directory. It quotes "$PWD" in case it contains spaces.
做
如果需要指定绝对路径或相对路径,也可以这样
You can do
If you need to specify an absolute path or relative path, you can do that as well
命令:
ls -1 -d "$PWD/"*
这将给出文件的绝对路径,如下所示。
Command:
ls -1 -d "$PWD/"*
This will give the absolute paths of the file like below.
试试这个:
您会在工作目录中获得绝对路径列表。
Try this:
You get list of absolute paths in working directory.
$PWD
是上面 Matthew 提出的一个不错的选择。 如果您只想查找打印文件,那么您还可以添加 -type f 选项以仅搜索普通文件。 其他选项是“d”仅表示目录等。所以在你的情况下它会是(如果我只想搜索带有 .c ext 的文件):或者如果你想要所有文件:
注意:你不能为上面的命令,因为当 bash 设置别名时,$PWD 会自动完成到您的主目录。
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):or if you want all files:
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.
如果你给 find 命令一个绝对路径,它会用绝对路径吐出结果。 因此,如果您要从 Ken 目录中键入:(
而不是相对路径
find . -name bar -print
),您应该得到:
因此,如果您想要
ls -l< /code> 并让它返回绝对路径,您可以告诉 find 命令对它找到的任何内容执行
ls -l
。注意:
{}
和;
之间有一个空格;您会得到类似这样的信息:
如果您不确定文件在哪里,您可以随时更改搜索地点。 只要搜索路径以“/”开头,你就会得到一个绝对路径作为返回。 如果您正在搜索一个位置(例如 /),您将在其中收到大量权限被拒绝错误,那么我建议重定向标准错误,以便您实际上可以看到查找结果:(
2>
是Borne 和 Bash shell 的语法,但不适用于 C shell 它也可能在其他 shell 中工作,但我只确定它在 Bourne 和 Bash 中工作。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:
(instead of the relative path
find . -name bar -print
)You should get:
Therefore, if you want an
ls -l
and have it return the absolute path, you can just tell the find command to execute anls -l
on whatever it finds.NOTE: There is a space between
{}
and;
You'll get something like this:
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:
(
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).另一种选择
只是确定
*
是 shell 扩展的,因此会做同样的事情(缺点是不能使用
-1
来用换行符分隔,而不是空格)。Just an alternative to
to pinpoint that
*
is shell expansion, sowould do the same (the drawback you cannot use
-1
to separate by new lines, not spaces).如果您需要当前目录和子目录中的所有文件的列表
如果您只需要当前目录中的所有文件的列表
If you need list of all files in current as well as sub-directories
If you need list of all files only in current directory
fd
使用
fd
(替代find
),使用以下语法:其中
.
是搜索模式,foo
是根目录。例如,要递归列出
etc
中的所有文件,请运行:fd 。 /etc-a
.fd
Using
fd
(alternative tofind
), use the following syntax:Where
.
is the search pattern andfoo
is the root directory.E.g. to list all files in
etc
recursively, run:fd . /etc -a
.递归查找jar文件并打印绝对路径
find jar file recursely and print absolute path
你可能想尝试一下这个。
您可以使用
for
循环和echo
简单地获取绝对路径,而无需find
。You might want to try this.
You can get abs path using
for
loop andecho
simply withoutfind
.如果您想要一个在函数中运行良好的动态解决方案,那么这种方法效果最好
This works best if you want a dynamic solution that works well in a function
下面的示例打印出没有额外句点的列表,并且还演示了如何搜索文件匹配项。 希望这可以帮助:
Here's an example that prints out a list without an extra period and that also demonstrates how to search for a file match. Hope this helps:
这对我有用。 但它没有按字母顺序列出。
此命令按字母顺序列出并列出隐藏文件。
This worked for me. But it didn't list in alphabetical order.
This command lists alphabetically as well as lists hidden files too.
stat
单个文件的绝对路径:
stat
Absolute path of a single file:
这将给出规范路径(将解析符号链接):
realpath FILENAME
如果您想要符号链接本身的规范路径,则:
realpath -s FILENAME
This will give the canonical path (will resolve symlinks):
realpath FILENAME
If you want canonical path to the symlink itself, then:
realpath -s FILENAME
如果路径包含空格,则大多数(如果不是全部)建议的方法都会导致无法在其他终端命令中直接使用路径。 理想情况下,结果前面会带有斜杠。
这对我在 macOS 上有效:
Most if not all of the suggested methods result in paths that cannot be used directly in some other terminal command if the path contains spaces. Ideally the results will have slashes prepended.
This works for me on macOS:
在 Linux 中可以通过多种方式列出递归文件。 在这里,我分享一个衬垫脚本,用于清除
/var/log/
目录中的所有文件日志(仅文件),并第二次检查最近哪个日志文件已添加条目。第一:
第二:
第三次使用:
注意:对于目录位置,您还可以传递
$PWD
而不是/var/log
。Recursive files can be listed by many ways in Linux. Here I am sharing one liner script to clear all logs of files(only files) from
/var/log/
directory and second check recently which logs file has made an entry.First:
Second:
Third use:
Note: for directory location you can also pass
$PWD
instead of/var/log
.如果您没有符号链接,您可以尝试
-i
在每行中生成树状打印文件名,而不显示树状结构。-f
使树打印每个文件的完整路径。-L 1
避免树递归。If you don't have symbolic links, you could try
-i
makes tree print filenames in each line, without the tree structure.-f
makes tree print the full path of each file.-L 1
avoids tree from recursion.编写一个小函数
然后您可以使用它,就像
它给出完整路径一样
Write one small function
Then you can use like
it gives full path like
我使用以下命令列出 txt 文件中目录中文件的绝对路径:
I used the following to list absolute path of files in a directory in a txt file:
这是一个更短、更方便的解决方案:
~+
表示"$PWD"
,但它是自动引用的,并且需要较少的接触键盘的符号- name .htaccess
或-maxdepth 1
如有必要,与另一个答案所说的不同,您可以为此命令创建一个别名:
~/.bashrc
中,然后您只需输入 2 个字母-name
并像fa .htaccess
一样使用Here is a shorter, more convenient solution:
~+
means"$PWD"
, except it's quoted automatically, and requires less reaching around the keyboard for symbols-name .htaccess
or-maxdepth 1
if necessaryUnlike what another answer says, you CAN make an alias for this command:
~/.bashrc
and you only have to type as little as 2 letters-name
at the end of the alias and use it likefa .htaccess