如何在 Linux 中生成带有绝对路径的文件列表?

发布于 2024-07-08 05:46:47 字数 381 浏览 12 评论 0原文

我正在编写一个 shell 脚本,它将文件路径作为输入。

因此,我需要生成带有完整路径的递归文件列表。 例如,文件 bar 具有路径:

/home/ken/foo/bar

但是,据我所知, lsfind 都只给出相对路径列表:

./foo/bar   (from the folder ken)

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

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(29

深海里的那抹蓝 2024-07-15 05:46:47

如果你给find一个绝对路径作为开始,它会打印绝对路径。 例如,要查找当前目录中的所有 .htaccess 文件:

find "$(pwd)" -name .htaccess

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

find "$PWD" -name .htaccess

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:

find "$(pwd)" -name .htaccess

or if your shell expands $PWD to 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.

泪之魂 2024-07-15 05:46:47
readlink -f filename 

给出完整的绝对路径。 但如果文件是符号链接,您将获得最终解析的名称。

readlink -f filename 

gives the full absolute path. But if the file is a symlink, you'll get the final resolved name.

岛歌少女 2024-07-15 05:46:47

将此用于目录(bash 中需要 ** 之后的 / 将其限制为目录):

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

这用于直接在当前目录下的文件和目录,其名称包含.

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

这适用于所有内容:

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

取自此处
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):

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

In bash, ** is recursive if you enable shopt -s globstar.

2024-07-15 05:46:47

您可以

find $PWD 

在 bash 中使用

You can use

find $PWD 

in bash

拥醉 2024-07-15 05:46:47
ls -d "$PWD/"*

这只在当前目录中查找。 如果它包含空格,它会引用“$PWD”。

ls -d "$PWD/"*

This looks only in the current directory. It quotes "$PWD" in case it contains spaces.

美胚控场 2024-07-15 05:46:47

ls -1 | xargs realpath

如果需要指定绝对路径或相对路径,也可以这样

ls -1 $FILEPATH | xargs realpath

You can do

ls -1 | xargs realpath

If you need to specify an absolute path or relative path, you can do that as well

ls -1 $FILEPATH | xargs realpath
你不是我要的菜∠ 2024-07-15 05:46:47

命令: ls -1 -d "$PWD/"*

这将给出文件的绝对路径,如下所示。

[root@kubenode1 ssl]# ls -1 -d "$PWD/"*
/etc/kubernetes/folder/file-test-config.txt
/etc/kubernetes/folder/file-test.txt
/etc/kubernetes/folder/file-client.txt

Command: ls -1 -d "$PWD/"*

This will give the absolute paths of the file like below.

[root@kubenode1 ssl]# ls -1 -d "$PWD/"*
/etc/kubernetes/folder/file-test-config.txt
/etc/kubernetes/folder/file-test.txt
/etc/kubernetes/folder/file-client.txt
白况 2024-07-15 05:46:47

试试这个:

find "$PWD"/

您会在工作目录中获得绝对路径列表

Try this:

find "$PWD"/

You get list of absolute paths in working directory.

(り薆情海 2024-07-15 05:46:47

$PWD 是上面 Matthew 提出的一个不错的选择。 如果您只想查找打印文件,那么您还可以添加 -type f 选项以仅搜索普通文件。 其他选项是“d”仅表示目录等。所以在你的情况下它会是(如果我只想搜索带有 .c ext 的文件):

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

或者如果你想要所有文件:

find $PWD -type f

注意:你不能为上面的命令,因为当 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):

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.

白云不回头 2024-07-15 05:46:47

如果你给 find 命令一个绝对路径,它会用绝对路径吐出结果。 因此,如果您要从 Ken 目录中键入:(

find /home/ken/foo/ -name bar -print    

而不是相对路径 find . -name bar -print),

您应该得到:

/home/ken/foo/bar

因此,如果您想要 ls -l< /code> 并让它返回绝对路径,您可以告诉 find 命令对它找到的任何内容执行 ls -l

find /home/ken/foo -name bar -exec ls -l {} ;\ 

注意:{}; 之间有一个空格

;您会得到类似这样的信息:

-rw-r--r--   1 ken admin       181 Jan 27 15:49 /home/ken/foo/bar

如果您不确定文件在哪里,您可以随时更改搜索地点。 只要搜索路径以“/”开头,你就会得到一个绝对路径作为返回。 如果您正在搜索一个位置(例如 /),您将在其中收到大量权限被拒绝错误,那么我建议重定向标准错误,以便您实际上可以看到查找结果:(

find / -name bar -exec ls -l {} ;\ 2> /dev/null

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:

find /home/ken/foo/ -name bar -print    

(instead of the relative path 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.

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).

日久见人心 2024-07-15 05:46:47

另一种选择

ls -d "$PWD/"* 

只是确定 * 是 shell 扩展的

echo "$PWD/"*

,因此会做同样的事情(缺点是不能使用 -1 来用换行符分隔,而不是空格)。

Just an alternative to

ls -d "$PWD/"* 

to pinpoint that * is shell expansion, so

echo "$PWD/"*

would do the same (the drawback you cannot use -1 to separate by new lines, not spaces).

安人多梦 2024-07-15 05:46:47

如果您需要当前目录和子目录中的所有文件的列表

find $PWD -type f

如果您只需要当前目录中的所有文件的列表

find $PWD -maxdepth 1 -type f

If you need list of all files in current as well as sub-directories

find $PWD -type f

If you need list of all files only in current directory

find $PWD -maxdepth 1 -type f
Saygoodbye 2024-07-15 05:46:47

fd

使用 fd (替代 find),使用以下语法:

fd . foo -a

其中 . 是搜索模式,foo 是根目录。

例如,要递归列出 etc 中的所有文件,请运行: fd 。 /etc-a.

-a, --absolute-path 显示绝对路径而不是相对路径

fd

Using fd (alternative to find), use the following syntax:

fd . foo -a

Where . is the search pattern and foo is the root directory.

E.g. to list all files in etc recursively, run: fd . /etc -a.

-a, --absolute-path Show absolute instead of relative paths

看轻我的陪伴 2024-07-15 05:46:47

递归查找jar文件并打印绝对路径

ls -R |grep "\.jar$" | xargs readlink -f                                                                                                                                                                                                                                                               
/opt/tool/dev/maven_repo/com/oracle/ojdbc/ojdbc8-19.3.0.0.jar
/opt/tool/dev/maven_repo/com/oracle/ojdbc/ons-19.3.0.0.jar
/opt/tool/dev/maven_repo/com/oracle/ojdbc/oraclepki-19.3.0.0.jar
/opt/tool/dev/maven_repo/com/oracle/ojdbc/osdt_cert-19.3.0.0.jar
/opt/tool/dev/maven_repo/com/oracle/ojdbc/osdt_core-19.3.0.0.jar
/opt/tool/dev/maven_repo/com/oracle/ojdbc/simplefan-19.3.0.0.jar
/opt/tool/dev/maven_repo/com/oracle/ojdbc/ucp-19.3.0.0.jar

find jar file recursely and print absolute path

ls -R |grep "\.jar
quot; | xargs readlink -f                                                                                                                                                                                                                                                               
/opt/tool/dev/maven_repo/com/oracle/ojdbc/ojdbc8-19.3.0.0.jar
/opt/tool/dev/maven_repo/com/oracle/ojdbc/ons-19.3.0.0.jar
/opt/tool/dev/maven_repo/com/oracle/ojdbc/oraclepki-19.3.0.0.jar
/opt/tool/dev/maven_repo/com/oracle/ojdbc/osdt_cert-19.3.0.0.jar
/opt/tool/dev/maven_repo/com/oracle/ojdbc/osdt_core-19.3.0.0.jar
/opt/tool/dev/maven_repo/com/oracle/ojdbc/simplefan-19.3.0.0.jar
/opt/tool/dev/maven_repo/com/oracle/ojdbc/ucp-19.3.0.0.jar

油饼 2024-07-15 05:46:47

你可能想尝试一下这个。

for name in /home/ken/foo/bar/*
do
    echo $name
done

您可以使用 for 循环和 echo 简单地获取绝对路径,而无需 find

You might want to try this.

for name in /home/ken/foo/bar/*
do
    echo $name
done

You can get abs path using for loop and echo simply without find.

腹黑女流氓 2024-07-15 05:46:47

如果您想要一个在函数中运行良好的动态解决方案,那么这种方法效果最好

lfp ()
{
  ls -1 $1 | xargs -I{} echo $(realpath $1)/{}
}

This works best if you want a dynamic solution that works well in a function

lfp ()
{
  ls -1 $1 | xargs -I{} echo $(realpath $1)/{}
}

绮筵 2024-07-15 05:46:47
lspwd() { for i in $@; do ls -d -1 $PWD/$i; done }
lspwd() { for i in $@; do ls -d -1 $PWD/$i; done }
夏尔 2024-07-15 05:46:47

下面的示例打印出没有额外句点的列表,并且还演示了如何搜索文件匹配项。 希望这可以帮助:

find . -type f -name "extr*" -exec echo `pwd`/{} \; | sed "s|\./||"

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:

find . -type f -name "extr*" -exec echo `pwd`/{} \; | sed "s|\./||"
海未深 2024-07-15 05:46:47

这对我有用。 但它没有按字母顺序列出。

find "$(pwd)" -maxdepth 1

此命令按字母顺序列出并列出隐藏文件。

ls -d -1 "$PWD/".*; ls -d -1 "$PWD/"*;

This worked for me. But it didn't list in alphabetical order.

find "$(pwd)" -maxdepth 1

This command lists alphabetically as well as lists hidden files too.

ls -d -1 "$PWD/".*; ls -d -1 "$PWD/"*;
宣告ˉ结束 2024-07-15 05:46:47

stat

单个文件的绝对路径:

stat -c %n "$PWD"/foo/bar

stat

Absolute path of a single file:

stat -c %n "$PWD"/foo/bar
本宫微胖 2024-07-15 05:46:47

这将给出规范路径(将解析符号链接):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

顾北清歌寒 2024-07-15 05:46:47

如果路径包含空格,则大多数(如果不是全部)建议的方法都会导致无法在其他终端命令中直接使用路径。 理想情况下,结果前面会带有斜杠。
这对我在 macOS 上有效:

find / -iname "*SEARCH TERM spaces are okay*" -print 2>&1  | grep -v denied |grep -v permitted |sed -E 's/\ /\\ /g'

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:

find / -iname "*SEARCH TERM spaces are okay*" -print 2>&1  | grep -v denied |grep -v permitted |sed -E 's/\ /\\ /g'
别想她 2024-07-15 05:46:47
for p in <either relative of absolute path of the directory>/*; do
    echo $(realpath -s $p)
done
for p in <either relative of absolute path of the directory>/*; do
    echo $(realpath -s $p)
done
忱杏 2024-07-15 05:46:47

在 Linux 中可以通过多种方式列出递归文件。 在这里,我分享一个衬垫脚本,用于清除 /var/log/ 目录中的所有文件日志(仅文件),并第二次检查最近哪个日志文件已添加条目。

第一:

find /var/log/ -type f  #listing file recursively 

第二:

for i in $(find $PWD -type f) ; do cat /dev/null > "$i" ; done #empty files recursively 

第三次使用:

ls -ltr $(find /var/log/ -type f ) # listing file used in recent

注意:对于目录位置,您还可以传递 $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:

find /var/log/ -type f  #listing file recursively 

Second:

for i in $(find $PWD -type f) ; do cat /dev/null > "$i" ; done #empty files recursively 

Third use:

ls -ltr $(find /var/log/ -type f ) # listing file used in recent

Note: for directory location you can also pass $PWD instead of /var/log.

人生百味 2024-07-15 05:46:47

如果您没有符号链接,您可以尝试

tree -iFL 1 [DIR]

-i 在每行中生成树状打印文件名,而不显示树状结构。

-f 使树打印每个文件的完整路径。

-L 1 避免树递归。

If you don't have symbolic links, you could try

tree -iFL 1 [DIR]

-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.

请你别敷衍 2024-07-15 05:46:47

编写一个小函数

lsf() {
ls `pwd`/$1
}

然后您可以使用它,就像

lsf test.sh 

它给出完整路径一样

/home/testuser/Downloads/test.sh

Write one small function

lsf() {
ls `pwd`/$1
}

Then you can use like

lsf test.sh 

it gives full path like

/home/testuser/Downloads/test.sh
以酷 2024-07-15 05:46:47

我使用以下命令列出 txt 文件中目录中文件的绝对路径:

find "$PWD" -wholename '*.JPG' >test.txt

I used the following to list absolute path of files in a directory in a txt file:

find "$PWD" -wholename '*.JPG' >test.txt
苯莒 2024-07-15 05:46:47

这是一个更短、更方便的解决方案:

find ~+
  • ~+ 表示 "$PWD",但它是自动引用的,并且需要较少的接触键盘的符号
  • 添加 - name .htaccess-maxdepth 1 如有必要,

与另一个答案所说的不同,您可以为此命令创建一个别名:

alias fa='find ~+'
  • 将其放入 ~/.bashrc 中,然后您只需输入 2 个字母
  • 或者您可以在别名末尾添加 -name 并像 fa .htaccess 一样使用
  • 单引号,以防止过早扩展
  • fa表示查找绝对路径

Here is a shorter, more convenient solution:

find ~+
  • ~+ means "$PWD", except it's quoted automatically, and requires less reaching around the keyboard for symbols
  • Add -name .htaccess or -maxdepth 1 if necessary

Unlike what another answer says, you CAN make an alias for this command:

alias fa='find ~+'
  • Put this in ~/.bashrc and you only have to type as little as 2 letters
  • Alternatively you can add -name at the end of the alias and use it like fa .htaccess
  • The single quotes prevent premature expansion
  • fa means Find Absolute path
漫漫岁月 2024-07-15 05:46:47
ls -1 | awk  -vpath=$PWD/ '{print path$1}'
ls -1 | awk  -vpath=$PWD/ '{print path$1}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文