如何获得递归完整路径列表,每个文件一行?

发布于 2024-08-12 06:12:29 字数 348 浏览 5 评论 0原文

我怎样才能输出一个递归单行路径的平面列表?

例如,我只想要一个带有完整路径的文件的平面列表:

/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow
/home/dreftymac/stackoverflow/alpha.txt
/home/dreftymac/stackoverflow/bravo.txt
/home/dreftymac/stackoverflow/charlie.txt

ls -a1 几乎可以满足我的需要,但我不需要路径片段,我想要完整路径。

How can I spit out a flat list of recursive one-per-line paths?

For example, I just want a flat listing of files with their full paths:

/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow
/home/dreftymac/stackoverflow/alpha.txt
/home/dreftymac/stackoverflow/bravo.txt
/home/dreftymac/stackoverflow/charlie.txt

ls -a1 almost does what I need, but I do not want path fragments, I want full paths.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(26

许你一世情深 2024-08-19 06:12:30

使用 find:

find .
find /home/dreftymac

如果您只需要文件(省略目录、设备等):

find . -type f
find /home/dreftymac -type f

Use find:

find .
find /home/dreftymac

If you want files only (omit directories, devices, etc):

find . -type f
find /home/dreftymac -type f
天煞孤星 2024-08-19 06:12:30

如果您确实想使用 ls,请使用 awk 格式化其输出:

ls -R /path | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }'

If you really want to use ls, then format its output using awk:

ls -R /path | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }'
停滞 2024-08-19 06:12:30

ls -ld $(find .)

如果您想按修改时间对输出进行排序:

ls -ltd $(find .)

ls -ld $(find .)

if you want to sort your output by modification time:

ls -ltd $(find .)

自控 2024-08-19 06:12:30

最好的命令是:tree -fi

-f 打印每个文件的完整路径前缀
-i 不打印缩进

,例如

$ tree -fi
.
./README.md
./node_modules
./package.json
./src
./src/datasources
./src/datasources/bookmarks.js
./src/example.json
./src/index.js
./src/resolvers.js
./src/schema.js

为了使用文件而不是链接,您必须从输出中删除 >

tree -fi |grep -v \>

如果您想了解每个文件的性质(例如仅读取 ASCII 文件),请尝试while 循环:

tree -fi |
grep -v \> |
while read -r first ; do 
    file "${first}"
done |
grep ASCII

Best command is: tree -fi

-f print the full path prefix for each file
-i don't print indentations

e.g.

$ tree -fi
.
./README.md
./node_modules
./package.json
./src
./src/datasources
./src/datasources/bookmarks.js
./src/example.json
./src/index.js
./src/resolvers.js
./src/schema.js

In order to use the files but not the links, you have to remove > from your output:

tree -fi |grep -v \>

If you want to know the nature of each file, (to read only ASCII files for example) try a while loop:

tree -fi |
grep -v \> |
while read -r first ; do 
    file "${first}"
done |
grep ASCII
心碎的声音 2024-08-19 06:12:30

哦,答案真是一长串。
它很有帮助,最后,我创建了自己想要的:

列出目录及其子目录中的所有文件:

find "$PWD" -type f

列出目录及其子目录中的所有目录:

find "$PWD" -type d

列出所有目录目录及其子目录中的文件:

find "$PWD"

Oh, really a long list of answers.
It helped a lot and finally, I created my own which I was looking for :

To List All the Files in a directory and its sub-directories:

find "$PWD" -type f

To List All the Directories in a directory and its sub-directories:

find "$PWD" -type d

To List All the Directories and Files in a directory and its sub-directories:

find "$PWD"
奶气 2024-08-19 06:12:30

尝试以下更简单的方法:

find "$PWD"

Try the following simpler way:

find "$PWD"
清风挽心 2024-08-19 06:12:30
du -a

对于某些无法查找/定位的有限设备外壳非常方便。

du -a

Handy for some limited appliance shells where find/locate aren't available.

孤寂小茶 2024-08-19 06:12:30

我不知道完整路径,但您可以使用 -R 进行递归。或者,如果您不喜欢ls,则可以直接执行find *

I don't know about the full path, but you can use -R for recursion. Alternatively, if you're not bent on ls, you can just do find *.

月下客 2024-08-19 06:12:30

除 ls 之外不使用任何外部命令:

ls -R1 /path | 
  while read l; do case $l in *:) d=${l%:};; "") d=;; *) echo "$d/$l";; esac; done

Using no external commands other than ls:

ls -R1 /path | 
  while read l; do case $l in *:) d=${l%:};; "") d=;; *) echo "$d/$l";; esac; done

蒲公英的约定 2024-08-19 06:12:30

找到/就可以了

find / will do the trick

蓝眼泪 2024-08-19 06:12:30

使用以下格式运行 bash 命令:

find /path -type f -exec ls -l \{\} \;

同样,要删除 -l 详细信息并仅返回绝对路径:

find /path -type f -exec ls \{\} \;

Run a bash command with the following format:

find /path -type f -exec ls -l \{\} \;

Likewise, to trim away -l details and return only the absolute paths:

find /path -type f -exec ls \{\} \;
故事↓在人 2024-08-19 06:12:30

对于所有未来的人来说,最简单的方法很简单:

du

但是,这也显示了每个文件夹中包含的内容的大小
您可以使用 awk 仅输出文件夹名称:

du | awk '{print $2}'

编辑-抱歉抱歉,我的错。我以为只需要文件夹。
我会把它留在这里,以防将来有人需要它......

The easiest way for all you future people is simply:

du

This however, also shows the size of whats contained in each folder
You can use awk to output only the folder name:

du | awk '{print $2}'

Edit- Sorry sorry, my bad. I thought it was only folders that were needed.
Ill leave this here in case anyone in the future needs it anyways...

抱着落日 2024-08-19 06:12:30

可以自由地使用所有可能的 ls 选项:

find -type f | xargs ls -1

With having the freedom of using all possible ls options:

find -type f | xargs ls -1

行雁书 2024-08-19 06:12:30

我认为对于平面列表,最好的方法是:(

find -D tree /fullpath/to-dir/ 

或者为了将其保存在 txt 文件中)

find -D tree /fullpath/to-dir/ > file.txt

I think for a flat list the best way is:

find -D tree /fullpath/to-dir/ 

(or in order to save it in a txt file)

find -D tree /fullpath/to-dir/ > file.txt
眼眸 2024-08-19 06:12:30

在 ls 目录末尾添加通配符会强制使用完整路径。现在你有这个:

$ ls /home/dreftymac/
foo.txt
bar.txt
stackoverflow
stackoverflow/alpha.txt
stackoverflow/bravo.txt
stackoverflow/charlie.txt

你可以这样做:

$ ls /home/dreftymac/*
/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow:
alpha.txt
bravo.txt
charlie.txt

不幸的是,这不会打印递归目录的完整路径,因此它可能不是您正在寻找的完整解决方案。

Adding a wildcard to the end of an ls directory forces full paths. Right now you have this:

$ ls /home/dreftymac/
foo.txt
bar.txt
stackoverflow
stackoverflow/alpha.txt
stackoverflow/bravo.txt
stackoverflow/charlie.txt

You could do this instead:

$ ls /home/dreftymac/*
/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow:
alpha.txt
bravo.txt
charlie.txt

Unfortunately this does not print the full path for directories recursed into, so it may not be the full solution you're looking for.

反差帅 2024-08-19 06:12:30

别把事情搞复杂了。我刚刚使用了这个并得到了一个漂亮的输出:

ls -lR /path/I/need

Don't make it complicated. I just used this and got a beautiful output:

ls -lR /path/I/need
望她远 2024-08-19 06:12:30

这是显示目录名称的部分答案。

ls -mR * | sed -n 's/://p'

说明:

ls -mR *列出以“:”结尾的完整目录名称,然后单独列出该目录中的文件

sed -n 's/://p' 查找以冒号结尾的行,去掉冒号并打印该行

通过迭代目录列表,我们也应该能够找到目录。仍在努力。通过 xargs 获取通配符是一个挑战。

Here is a partial answer that shows the directory names.

ls -mR * | sed -n 's/://p'

Explanation:

ls -mR * lists the full directory names ending in a ':', then lists the files in that directory separately

sed -n 's/://p' finds lines that end in a colon, strip off the colon and print the line

By iterating over the list of directories, we should be able to find the directories as well. Still workin on it. It is a challenge to get the wildcards through xargs.

诠释孤独 2024-08-19 06:12:30

realpath 命令打印解析的路径:

realpath *

要包含点文件,请将 ls -a 的输出通过管道传输到 realpath:

ls -a | xargs realpath

递归列出子目录:

ls -aR | xargs realpath

如果文件名中有空格, man xargs 建议使用 -o 选项来防止文件名被错误处理,这与 find -print0 的输出配合使用效果最佳它开始看起来比其他答案复杂得多:

find -print0 |xargs -0 realpath

另请参阅 Unix 和 Linux stackexchange 关于如何列出具有绝对路径的目录中的所有文件的问题

The realpath command prints the resolved path:

realpath *

To include dot files, pipe the output of ls -a to realpath:

ls -a | xargs realpath

To list subdirectories recursively:

ls -aR | xargs realpath

In case you have spaces in file names, man xargs recommends using the -o option to prevent file names from being processed incorrectly, this works best with the output of find -print0 and it starts to look a lot more complex than other answers:

find -print0 |xargs -0 realpath

See also Unix and Linux stackexchange question on how to list all files in a directory with absolute path.

只为一人 2024-08-19 06:12:30
tar cf - $PWD|tar tvf -             

这很慢,但是递归地工作并打印目录和文件。如果您只想要文件名而不需要所有其他信息/目录,您可以使用 awk/grep 对其进行管道传输:

tar cf - $PWD|tar tvf -|awk '{print $6}'|grep -v "/$"          
tar cf - $PWD|tar tvf -             

This is slow but works recursively and prints both directories and files. You can pipe it with awk/grep if you just want the file names without all the other info/directories:

tar cf - $PWD|tar tvf -|awk '{print $6}'|grep -v "/$"          
陪你到最终 2024-08-19 06:12:30

我看到很多答案。这是我的,我认为如果您在 Mac 上工作,这非常有用。

我确信您知道有一些“捆绑”文件(.app.rtfd.workflow 等)。从 Finder 窗口来看,它们似乎是单个文件。但他们不是。并且 $ ls$ find 将它们视为目录...所以,除非您也需要列出它们的内容,否则这对我有用:

find . -not -name ".*" -not -name "." | egrep -v "\.rtfd/|\.app/|\.lpdf/|\.workflow/"

当然这是为了工作dir,并且您可以添加其他捆绑包的扩展(但始终在它们后面带有 / )。或者任何其他扩展(如果不是没有 / 的捆绑包)。

相当有趣的“.lpdf/”(多语言pdf)。它具有正常的“.pdf”扩展名 (!!),或者在 Finder 中没有扩展名。这样你就可以得到这个pdf(或者只算1个文件),而不是一堆东西......

A lot of answers I see. This is mine, and I think quite useful if you are working on Mac.

I'm sure you know there are some "bundle" files (.app, .rtfd, .workflow, and so on). And looking at Finder's window they seem single files. But they are not. And $ ls or $ find see them as directories... So, unless you need list their contents as well, this works for me:

find . -not -name ".*" -not -name "." | egrep -v "\.rtfd/|\.app/|\.lpdf/|\.workflow/"

Of course this is for the working dir, and you could add other bundles' extensions (but always with a / after them). Or any other extensions if not bundle's without the /.

Rather interesting the ".lpdf/" (multilingual pdf). It has normal ".pdf" extension (!!) or none in Finder. This way you get (or it just counts 1 file) for this pdf and not a bunch of stuff…

岁月静好 2024-08-19 06:12:30

ls -lR 是您正在寻找的,或者至少是我正在寻找的。干杯

ls -lR is what you were looking for, or atleast I was. cheers

作妖 2024-08-19 06:12:30

如果目录作为相对路径传递,则需要在调用 find 之前将其转换为绝对路径。在以下示例中,目录作为第一个参数传递给脚本:

#!/bin/bash

# get absolute path
directory=`cd $1; pwd`
# print out list of files and directories
find "$directory"

If the directory is passed as a relative path and you will need to convert it to an absolute path before calling find. In the following example, the directory is passed as the first parameter to the script:

#!/bin/bash

# get absolute path
directory=`cd $1; pwd`
# print out list of files and directories
find "$directory"
东京女 2024-08-19 06:12:30

如果您必须在 100 GB 或更大的大磁盘上进行搜索,
我建议执行 @kerkael 发布 的命令 tree 而不是 find< /code> 或 ls

我建议当您执行命令 tree 时,将输出写入文件中,这与 @kerkael 的帖子不同

示例:

tree -fi > result.txt

然后,使用 grep -i "*.docx" > 等模式在文件中执行 grep 命令 result.txt 这样您就不会浪费时间。这种方式对于在大磁盘上搜索文件来说速度更快。

我在 270GB 磁盘上执行了这些命令,得到了一个 100MB 的文件 txt

命令tree 花费的时间是 14 分钟。
输入图片此处描述

If you have to search on a large disk like 100 Gb or more,
I suggest to do the command tree that @kerkael posted and not the find or ls.

I suggest that when you do the command tree you write the output in the file, different from the @kerkael's post.

Example:

tree -fi > result.txt

Afterwards, do a grep command in file using a pattern like grep -i "*.docx" > result.txt so you do not lose time. This way is faster for searching file(s) on big disks.

I did these commands on a 270GB disk that I get a file txt taken 100MB.

The time that taken for command tree was 14 minutes.
enter image description here

姐不稀罕 2024-08-19 06:12:30

当前位置的所有文件的递归列表:

ls -l $(find . -type f)

Recursive list of all files from current location:

ls -l $(find . -type f)

翻身的咸鱼 2024-08-19 06:12:30

我知道文件名,但也想要目录。

find $PWD | fgrep filename

在 Mac OS 10.12.1 中完美运行

I knew the file name but wanted the directory as well.

find $PWD | fgrep filename

worked perfectly in Mac OS 10.12.1

喵星人汪星人 2024-08-19 06:12:30

@鬼狗74:
对您的解决方案进行一些调整。
以下代码可用于搜索文件及其完整绝对路径。

sudo ls -R / | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }' | grep [file_to_search]

@ghostdog74:
Little tweak with your solution.
Following code can be used to search file with its full absolute path.

sudo ls -R / | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }' | grep [file_to_search]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文