0

by doing the following command in the folder

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

I get entries like:

env1
env2
env3
env4

how I can use cat/grep or yq/jq or any other alternative command(s) instead of the above command?

6
  • What use are you later making of these entries? Iterating over them in a shell script? Or just printing to stdout? Commented Sep 30, 2020 at 20:32
  • What you want to achieve? Just list the directories in the current directory?
    – kofemann
    Commented Sep 30, 2020 at 20:33
  • @CharlesDuffy printing to stdout
    – sasan
    Commented Sep 30, 2020 at 20:35
  • @kofemann I'm using this command as a script in my groovy code using in helm file to print the list of environments in the output, using this command it print a list of env sequentially, but I need to print them in parallel
    – sasan
    Commented Sep 30, 2020 at 20:37
  • You mean print them on the same line? Commented Sep 30, 2020 at 20:53

2 Answers 2

1
for dir in */; do
  echo "${dir%/}"
done
1
  • can I ask you also briefly explain about your answer?
    – sasan
    Commented Sep 30, 2020 at 20:38
0

There are several options. You can use the tree command with the options:

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

Or you can also use the grep command to get the directories and the command awk:

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

Or you can use the sed command to remove the slash:

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

I found this solutions in this post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.