cat
is a program that simply takes its input stream, or a file, and prints it to standard output. You cannot cat
a directory, that doesn't make sense. If you just want to see the names of all first level subdirectories of a given directory, you can use echo
which simply prints what you give it:
$ for dir in foo/*/; do echo "$dir"; done
foo/dir1/
foo/dir2/
foo/dir3/
foo/dir4/
You don't even need a loop:
$ echo foo/*/
foo/dir1/ foo/dir2/ foo/dir3/ foo/dir4
To get only the directory names, without the path:
$ for dir in foo/*/; do basename "$dir"; done
dir1
dir2
dir3
dir4
Alternatively, you can cd
to the path:
$ cd foo
$ echo */
dir1/ dir2/ dir3/ dir4/
Or cd
in a subshell so you stay where you were originally when the command finishes:
$ ( cd foo && echo */ )
dir1/ dir2/ dir3/ dir4/
Or, to get them on separate lines and also to ensure that this will work even on weird directory names (e.g. a name that contains a newline):
$ ( cd foo && printf -- '%s\n' */ )
dir1/
dir2/
dir3/
dir4/
Finally, if your directory names can contain newlines or other strangeness, use:
cat
is not meant for folders but text files... – schrodingerscatcuriosity Oct 17 '20 at 01:48