【问题标题】:How can I list only directory names, with no trailing "/"?我怎样才能只列出目录名称,没有尾随“/”?
【发布时间】:2020-09-30 20:30:22
【问题描述】:

通过在文件夹中执行以下命令

ls -d */ | cut -f1 -d'/'

我收到如下条目:

env1
env2
env3
env4

如何使用cat/grepyq/jq 或任何其他替代命令代替上述命令?

【问题讨论】:

  • 您以后对这些条目有何用处?在 shell 脚本中迭代它们?还是只是打印到标准输出?
  • 你想达到什么目的?只列出当前目录下的目录?
  • @CharlesDuffy 打印到标准输出
  • @kofemann 我在 helm 文件中使用这个命令作为我的 groovy 代码中的脚本来打印输出中的环境列表,使用这个命令它按顺序打印一个 env 列表,但我需要并行打印它们
  • 你的意思是把它们打印在在同一行

标签: grep cat ls


【解决方案1】:
for dir in */; do
  echo "${dir%/}"
done

【讨论】:

  • 能否请您也简单解释一下您的回答?
【解决方案2】:

有多种选择。您可以使用带有选项的tree 命令:

# d: list only directories
# i: no print of indention line
# L: max display depth of the directory tree
tree -di -L 1 "$(pwd)"

或者也可以使用grep命令获取目录和命令awk

# F: input field separator
# $9: print the ninth column of the output
ls -l | grep "^d" | awk -F" " '{print $9}' 

或者您可以使用sed 命令删除斜线:​​

# structure: s|regexp|replacement|flags
# g: apply the replacement to all matches to the regexp, not just the first
ls -d */ | sed 's|[/]||g'

我在post 中找到了这个解决方案。

【讨论】:

    猜你喜欢
    • 2019-01-12
    • 1970-01-01
    • 2021-10-10
    • 2019-03-18
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多