user8314628
user8314628

Reputation: 2042

How to get the directories' names only by using the ls command?

xxxx:~/209_repo> ls -d */
drwx------ 4 xxxx student 4.0K Jan 16 12:44 a1/
drwx------ 2 xxxx student 4.0K Jan 11 14:06 t01/
drwx------ 2 xxxx student 4.0K Jan 17 06:50 t02/

This one works properly. But when I go to the subdirectory of 209_repo, it comes up with a path rather than a directory name.

xxxx:~/209_repo/t02> ls -d /student/xxxx/209_repo/*/
drwx------ 4 xxxx student 4.0K Jan 16 12:44 /student/xxxx/209_repo/a1/
drwx------ 2 xxxx student 4.0K Jan 11 14:06 /student/xxxx/209_repo/t01/
drwx------ 2 xxxx student 4.0K Jan 17 06:50 /student/xxxx/209_repo/t02/

Is there any way that I can get a1,t01,t01 only so that I don't have to extract them later on?

Upvotes: 0

Views: 3255

Answers (3)

Honza P.
Honza P.

Reputation: 1242

Or standard command to find the subtree of directories is also find . -type d and you can tune it and add some filtering.

Upvotes: 3

asimovwasright
asimovwasright

Reputation: 838

Similar to @user7369280, but without subshell

cd /student/xxxx/209_repo/ && ls -d */ && cd -

EDIT

To mitigate failing ls command, and return to wd:

cd /student/xxxx/209_repo/ && ls -d */ && cd - || cd -

Upvotes: 0

Ralf
Ralf

Reputation: 1813

I'm not aware of a ls option to do this.

You could try

(cd /student/xxxx/209_repo && ls -d */)

This starts a new subshell, due to (...) and in this subshell changes to the directory .../209_repo and then executes the ls.

After the command you are still in the directory you were before, as the change dir was only executed in the subshell.

Upvotes: 2

Related Questions