我有多个子文件夹,每个子文件夹里都有多个.txt文件。 我想组织一个包含每个文件的列表。我使用这个命令来获取绝对路径。
find $PWD -type f -name "*.txt"
但是如何将它们全部合并到一个文本文件中。
像这样
/home/2022/Dir/20220103/1.txt
/home/2022/Dir/20220103/2.txt
/home/2022/Dir/20220103/3.txt
/home/2022/Dir/20220103/4.txt
/home/2022/Dir/20220105/1.txt
/home/2022/Dir/20220105/2.txt
/home/2022/Dir/20220105/3.txt
/home/2022/Dir/20220107/11.txt
/home/2022/Dir1/20220211/12.txt
/home/2022/Dir1/20220211/13.txt
/home/2022/Dir1/20220217/1.txt
.
.
.
.
.
man find
给你答案:
摘录:
-print True; print the full file name on the standard output, followed by a newline. ...
因此(针对您的情况):
find $PWD -type f -name "*.txt" -print
为了在文件中获取任何 UNIX/Linux 命令的结果,您只需使用
>
字符重定向,如下所示:
find $PWD -type f -name "*.txt" -print >outputfile
像这样:
find "$PWD" -type f -name '*.txt' -exec realpath {} +