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?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
sasan
  • 67
  • 1
  • 6
  • What use are you later making of these entries? Iterating over them in a shell script? Or just printing to stdout? – Charles Duffy Sep 30 '20 at 20:32
  • What you want to achieve? Just list the directories in the current directory? – kofemann Sep 30 '20 at 20:33
  • @CharlesDuffy printing to stdout – sasan Sep 30 '20 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 Sep 30 '20 at 20:37
  • You mean print them _on the same line_? – Charles Duffy Sep 30 '20 at 20:53
  • note that that's an innately unsafe thing to do: directory names can have spaces. How can you tell the difference between two different directory names printed on the same line, and one name with a space? (The same argument is why line-separating filenames is a bad idea as well, since newlines are _also_ legal inside names; Doing It Right calls for using the NUL character, which is the _only_ character that can't exist in a name, as terminator). – Charles Duffy Sep 30 '20 at 20:54

2 Answers2

1
for dir in */; do
  echo "${dir%/}"
done
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
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.

flaxel
  • 4,173
  • 4
  • 17
  • 30