LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Show folder and only one subfolder using ls in command line (http://www.linuxquestions.org/questions/linux-newbie-8/show-folder-and-only-one-subfolder-using-ls-in-command-line-891569/)

Drigo 07-13-2011 04:26 PM

Show folder and only one subfolder using ls in command line
 
So I have many folders with many subfolders. All I want it to get the folder name and the first subfolder. I tried using ls -R but this give me more than I want it.

Let say I have:

dir1/subdir1/sub_1_1/file.a
dir1/subdir1/sub_1_1/file.a2
dir1/subdir1/sub_1_1/file.a3
dir1/subdir1/sub_1_1/file.a4
dir1/subdir1/sub_1_2/file.b
dir1/subdir2/sub_2_1/file.c
dir1/subdir3/sub_3_1/file.d
dir1/subdir4/sub_4_1/file.e
dir2/subdir1/sub_1_1/file.a
dir2/subdir1/sub_1_2/file.b
dir2/subdir2/sub_2_1/file.c
dir2/subdir3/sub_3_1/file.d
dir2/subdir4/sub_4_1/file.e
dir3/subdir1/sub_1_1/file.a
dir3/subdir1/sub_1_2/file.b
dir3/subdir2/sub_2_1/file.c
dir3/subdir3/sub_3_1/file.d
dir3/subdir4/sub_4_1/file.e
.
.
.

All I want when I execute the command is to show me:

dir1/subdir1
dir1/subdir1
dir1/subdir2
dir1/subdir3
dir1/subdir4
dir2/subdir1
dir2/subdir1
dir2/subdir2
dir2/subdir3
dir2/subdir4
dir3/subdir1
dir3/subdir1
dir3/subdir2
dir3/subdir3
dir3/subdir4
.
.
.

theNbomr 07-13-2011 05:47 PM

I don't think you can do that with ls, but you can with find:
Code:

find . -type d -maxdepth 2
--- rod.

Drigo 07-13-2011 11:19 PM

That was my first guess but maxdepth or the depth option dont work .....give me an argument error

David the H. 07-14-2011 12:03 AM

How about showing us the exact command you used? It sounds like you have a syntax error somewhere.

This should work in simple cases, but will throw an error if there are no subdirectories to match.
Code:

ls -d ./*/*/
Note though that it's not advisable to parse ls for a script.

Which leads me to the big question...what do you need this for? If you tell us what your true goal is, it may be that there's a better solution.

Drigo 07-14-2011 12:56 PM

Thanks that's what I wanted


All times are GMT -5. The time now is 09:31 AM.