Bash 如何在find命令的o/p中的目录名末尾附加“/”

Bash 如何在find命令的o/p中的目录名末尾附加“/”,bash,Bash,我正在使用find/tmp/tartest/-maxdepth 1-mindepth 1-not-type l命令来获取不包括符号链接的所有文件和目录的列表。这给了我以下o/p: /tmp/tartest/hi.txt /tmp/tartest/hello1.txt /tmp/tartest/testdir /tmp/tartest/hello.txt 注意/tmp/tartest/testdir是否可以用/结束此操作/tmp/tartest/testdir/ 我正在shell脚本中使用此o/

我正在使用
find/tmp/tartest/-maxdepth 1-mindepth 1-not-type l
命令来获取不包括符号链接的所有文件和目录的列表。这给了我以下o/p:

/tmp/tartest/hi.txt
/tmp/tartest/hello1.txt
/tmp/tartest/testdir
/tmp/tartest/hello.txt
注意
/tmp/tartest/testdir
是否可以用
/
结束此操作<找到的所有目录的代码>/tmp/tartest/testdir/


我正在shell脚本中使用此o/p。

find
表达式可以具有分支逻辑。附加两个后续操作的默认方式是使用
-a
(“and”),但也可以手动使用
-o
(“or”)

因此,使用GNU
find
(此处使用,因为它支持
-printf
,允许您指定任意格式字符串):

注:

  • -maxdepth 1
    是全局的,因此它本质上适用于所有分支
  • -type d-printf“%p/\n”
    打印目录名(其中
    -type d
    为true的内容),后面加一个
    /
  • -o-printf“%p\n”
    以or条件附加到上面,使得不带
    /
    printf
    仅在与
    -type d
    不匹配时运行

find
表达式可以具有分支逻辑。附加两个后续操作的默认方式是使用
-a
(“and”),但也可以手动使用
-o
(“or”)

因此,使用GNU
find
(此处使用,因为它支持
-printf
,允许您指定任意格式字符串):

注:

  • -maxdepth 1
    是全局的,因此它本质上适用于所有分支
  • -type d-printf“%p/\n”
    打印目录名(其中
    -type d
    为true的内容),后面加一个
    /
  • -o-printf“%p\n”
    以or条件附加到上面,使得不带
    /
    printf
    仅在与
    -type d
    不匹配时运行

您想这样做,而不仅仅是使用
test-d
(这是一个shell内置项,因此不会招致fork/exec惩罚)来检查类型,有什么原因吗?…如果您确定这是您想做的,那么查找
的哪个版本?您是否100%确定只需要支持带有GNU
find
(与BSD版本相反)的系统?是否有理由这样做,而不仅仅是使用
test-d
(这是一个shell内置项,因此不会招致fork/exec惩罚)来检查类型?…如果您确定这是您想要做的,查找
的哪个版本?您是否100%确定只需要支持带有GNU
find
(与BSD版本相反)的系统?这打印了另外两个内容:
/tmp/tartest/
(父目录),并且还列出了symlinksok
find/tmp/tartest-maxdepth 1'('-type d-printf'%p/\n'))'-o'('-not-type l-printf'%p\n'))“
这将删除符号链接。现在只剩下父目录(/tmp/tartest/)。为什么会被打印出来。我想
-o-print
应该是suffice@aaj,您还可以运行
find/tmp/tartest-类型l'('-type d-printf'%p/\n')'-o-print')
,因此当
-type l
为真时不会进行打印。这打印了另外两个内容:
/tmp/tartest/
(父目录),还列出了symlinksok
find/tartest-maxdepth 1'('-type d-printf'%p/\n')-o'('-not-type l-printf'%p\n')“
这将删除符号链接。现在只剩下父目录(/tmp/tartest/)。为什么会被打印出来。我想
-o-print
应该是suffice@aaj,您还可以运行
find/tmp/tartest-类型l'('''-type d-printf'%p/\n')'-o-print')
,因此当
-type l
为真时,无法进行打印。
find /tmp/tartest -maxdepth 1 \
  -type d -printf '%p/\n' \
  -o -printf '%p\n'