1

以下是文件夹结构 - home/ABCD/test1/example1/sample1/textfile.txt

如果我执行find命令

find /home/ABCD/ -type f -print

我得到以下输出

/home/ABCD/test1/example1/sample1/textfile.txt

注意:我正在从 ABCD 文件夹中执行 find 命令,在结果中我想排除 /home/ABCD/ 文件夹我只想要/test1/example1/sample1/testfile.txt作为结果

我怎样才能做到这一点?

4

4 回答 4

1

由于您正在执行 find from ,因此请执行/home/ABCD/以下操作:

find * -type f -print

或者,如果您正在查找文件,test1请执行以下操作:

find test1 -type f -print

您还-maxdepth N可以限制 find 中的递归

如果您只想查找名为textfile.txtdo的文件

find test1 -type f -name 'textfile.txt' -print 

如果要打印前导斜杠,请执行

find . -type f -printf '/%p\n'

欲了解更多信息,请查看这里

注意:如果变量中有上述字符串,您可以像这样修剪它:

string="/home/ABCD/test1/example1/sample1/textfile.txt"
echo "${string#/home/ABCD}"

这里还有一些字符串操作的例子

于 2013-02-27T06:33:07.380 回答
1

只需.用作起始目录

find . -type f -print

./test1/example1/sample1/textfile.txt

如果你真的想要一个前导斜杠,请使用-printf输出

find . -type f -printf '/%P\n'
于 2013-02-27T07:26:00.107 回答
0

您可以使用该mindepth参数开始查看当前目录的下一级

find /home/ABCD/ -mindepth 1 -type f -print
于 2013-02-27T06:29:22.880 回答
0

这应该将您当前的工作目录名称替换为 .

find . -type f | perl -pne "s#$PWD#.#"

所以你会得到如下结果:./test1/example1/sample1/textfile.txt

如果您不想要前面的 ./,请改用此命令:

find . -type f | perl -pne "s#$PWD/##"
于 2013-02-27T07:28:22.700 回答