使用 find 时不要将父目录列为子目录的一部分

404*_*und 3 find

假设我有这样的情况:

$ find ./src -name '*.txt'
./src/file1.txt
./src/subdir1/file2.txt
./src/subdir1/subsubdir1/file3.txt
./src/subdir2/file4.txt
Run Code Online (Sandbox Code Playgroud)

我想排除正在搜索的目录,所以类似:

./file1.txt代替./src/file1.txt
./subdir1/file2.txt代替./src/subdir1/file2.txt等等。

添加-mindepth 1没有任何作用。

是否可以纯粹做我想做的事find

roa*_*ima 9

对于没有find支持的版本的情况-printf '%P',您可以使用此替代结构来避免包含搜索路径。

问题的出发点:

find ./src -name '*.txt'
Run Code Online (Sandbox Code Playgroud)

相同,但结果中没有搜索路径

( cd ./src && find . -name '*.txt' )
Run Code Online (Sandbox Code Playgroud)

假设您的问题中显示的文件结构,结果是

./file1.txt
./subdir1/file2.txt
./subdir1/subsubdir1/file3.txt
./subdir2/file4.txt
Run Code Online (Sandbox Code Playgroud)


l0b*_*0b0 5

扩展@steeldriver所说的内容:

$ cd "$(mktemp --directory)" # create temporary directory
direnv: unloading
$ mkdir foo bar
$ touch foo/1 bar/2
$ find foo bar -type f -name '*' -printf '%P\n'
1
2
Run Code Online (Sandbox Code Playgroud)

GNU 手册中对格式化字符串%P的记录如下find

%P
文件名以及发现该文件的起点名称已被删除。

这里,“文件名”指的是找到的文件的路径名,而不仅仅是文件名。