Can I use ls -R to show up to 2 folder depth?

21,253

Solution 1

If you want to display only a list of filenames and directory names, tree command is very convenient.

This is not installed by default, you have to install this:

sudo apt-get install tree

Then, you can see the tree structure by using the below command:

tree -L 2 main/

option -L : will set the directory depth number.

Example screenshot:

enter image description here

Solution 2

Use wildcards for your search like

ls /main/*/*/

That will list with a search depth of 2. User more wildcards for deeper search.

Solution 3

You cannot specify recursion depth in ls, but instead you can use the following:

find -maxdepth 2 -type d -ls

Courtesy: https://stackoverflow.com/questions/4509624/limit-depth-for-recursive-file-list-in-linux

Share:
21,253

Related videos on Youtube

Aggelos Kolaitis
Author by

Aggelos Kolaitis

Updated on September 18, 2022

Comments

  • Aggelos Kolaitis
    Aggelos Kolaitis over 1 year

    I have a folder tree that looks like this

    main/                             
    main/34532-23423632-2354/what-i-want/sth/other/blah-blah            
    main/54634-56345634-3422/what-i-want/sth/
    ....
    main/54356-34225675-2345/what-i-want/
    

    I want it to show the tree up to the folder what I want. Because the folders sth, other and so on contain many other things that are useless.

    I just want to see what's inside each folder named xxxx-xxxxxxxx-xxxxx.

    Is there any way?

  • Thagomizer
    Thagomizer over 2 years
    This will list files, which is often not desirable. Preceding "-ls" with "-type d" will improve this answer.