使用不带 ./ 的查找结果

Mar*_*son 11 find

我正在尝试使用 find 创建一堆符号链接,但在每个文件名之前使用 {} 包含 ./ 的结果。我怎样才能避免这种情况?

find . -type l -name '*.h' -exec ln -s /sourcedir/{} /destinationdir/{} \;
Run Code Online (Sandbox Code Playgroud)

Ant*_*hon 16

您只需更改命令中的一个字符:

find * -type l -name '*.h' -exec ln -s /sourcedir/{} /destinationdir/{} \;
#    ^
Run Code Online (Sandbox Code Playgroud)


Sté*_*las 7

使用标准语法,例如:

S=/sourcedir D=/destdir find . -type l -name '*.h' -exec sh -c '
  for i do
    ln -s -- "$S${i#.}" "$D/$i"
  done' sh {} +
Run Code Online (Sandbox Code Playgroud)

如果你想使用 GNUisms,你可以这样做:

find . -type l -name '*.h' -printf '/sourcedir/%P\0/destdir/%P\0' |
  xargs -r0n2 ln -s
Run Code Online (Sandbox Code Playgroud)

或者如果/sourcedir 当前目录:

find "$PWD" -type l -name '*.h' -printf '%p\0/destdir/%P\0' |
  xargs -r0n2 ln -s
Run Code Online (Sandbox Code Playgroud)


bon*_*ing 0

find将打印相对于您作为参数提供的路径的名称。在本例中,路径为.,因此所有名称都以 开头./。要获取绝对路径,您需要提供绝对路径作为输入:

find "$PWD" -type l -name '*.h'
Run Code Online (Sandbox Code Playgroud)

该命令使用$PWD环境变量,其中包含当前工作目录的绝对路径,因此它应该保留原始命令的含义。

  • 然后,他需要将“$PWD”从“ln”命令的目标中删除。 (3认同)