如何在查找命令的o/p的目录名末尾添加`/`

aaj*_*aaj 0 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
Run Code Online (Sandbox Code Playgroud)

注意/tmp/tartest/testdir是否可以用/?结束?/tmp/tartest/testdir/找到所有目录?

我在shell脚本中使用这个o/p.

Cha*_*ffy 5

find表达式可以有分支逻辑.附加两个后续操作的默认方式是-a("和"),但您也可以-o手动使用("或").

因此,使用GNU find(这里使用因为它支持-printf,让你指定任意格式字符串):

find /tmp/tartest -maxdepth 1 \
  -type d -printf '%p/\n' \
  -o -printf '%p\n'
Run Code Online (Sandbox Code Playgroud)

注意:

  • -maxdepth 1 是全球性的,因此它适用于所有分支机构.
  • -type d -printf '%p/\n'打印目录名称(内容-type d为true),/后面跟着它们.
  • -o -printf '%p\n'重视上述用条件,使得printf/只是运行时,事情就不会匹配-type d.