How do I list (ls) the content of a folder/directory recursively but to a depth of only one folder/directory?

charlesdarwin

How do I list (ls) the content of a folder/directory recursively but to a depth of only one folder/directory?

I have the following folder structure:

folderA:
  folderB1:
    folderC1:
      fileD1:
      fileD2:
    fileC2
    fileC3
  folderB2:
    folderC4:
      fileD3:
      fileD4:
    fileC5
    fileC6

I am in the parent folder of folderA and would like to list everything in it and its subfolders but not subsubfolders. So I would like to see:

folderB1/folderC1
folderB1/fileC2
folderB1/fileC3
folderB2/folderC2
folderB2/fileC4
folderB2/fileC5

Is that possible? At the moment, I use ls -R folderA which takes me down a rabbit hole of hundreds of subsub..subfolders I am not interested in. I would like to stop at a certain depth. Ideally, there would be an option like depth 1 to list the content of folderA and its subfolders and stop.

I am working on macOS X High Sierra.

Jeff Schaller

You don't want a recursive listing, then, because as you've seen, recursive means "to the end", not "to some arbitrary stopping point".

To list two levels beneath folderA while in folderA's parent,

(cd folderA && ls -d -- */*)

The crux of it is the */* wildcard/glob; that asks the shell to generate all of the immediate subdirectories (with */) and then all of its entries (the final *).

Importantly, we have to tell ls to not expand any of those final entries if they happen to be directories; we do this with the -d flag.

The last piece, to get the output format you're looking for, I solved by starting a subshell where we cd into folderA in order to do the listing. Once ls exits, the subshell exits, and we return to our current prompt & directory (above folderA).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I recursively duplicate a List?

From Dev

How do I get the level of depth of a list?

From Dev

How do I remove only one instance from a list in Python?

From Dev

How do I recursively list dependencies of a package that need to be installed?

From Dev

ls: how do I list directories sorted by timestamps of the files it contains

From Dev

How do I take a list and print all of the content in a random order, printing each exactly one time in python?

From Java

How do I grep recursively?

From Dev

How can I print DOM Content recursively?

From Dev

Collections.sort() affects all ArrayLists. How do I sort only one list and not the others?

From Dev

How do i add Points coordinates to a List of PointF of only one side of the rectangle?

From Dev

How can I search a directory recursively for any if statements with only one statement in them?

From Dev

How do I recursively increment the first value in a list and append to a nested list in python?

From Dev

How do I recursively compress only certain file extensions with 7zip command line?

From Dev

How do I restrict style to affect content only in modal?

From Dev

How do I replace bytes in multiple files in one directory recursively with Python 3.5.1?

From Dev

How can I list subdirectories recursively for HDFS?

From Dev

How can I list subdirectories recursively?

From Dev

How can I list files by type with ls?

From Dev

How do I add all elements from a list in F# recursively?

From Dev

How do I compare two folders recursively and generate a list of files and folders that are different?

From Dev

(Prolog) How do I rearrange elements in the list recursively to give multiple resulting states for an answer?

From Dev

How do I add all elements from a list in F# recursively?

From Dev

XSLT: how do I parse a document to retain only interesting nodes, along with all parents, at any depth?

From Dev

XSLT: how do I parse a document to retain only interesting nodes, along with all parents, at any depth?

From Dev

How do I create ActionBars recursively at runtime?

From Java

How do I copy directories recursively with gulp?

From Dev

how do I create a ctags file recursively?

From Dev

How do I JS-Beautify recursively?

From Dev

How do I recursively pass a mutable reference?

Related Related

  1. 1

    How do I recursively duplicate a List?

  2. 2

    How do I get the level of depth of a list?

  3. 3

    How do I remove only one instance from a list in Python?

  4. 4

    How do I recursively list dependencies of a package that need to be installed?

  5. 5

    ls: how do I list directories sorted by timestamps of the files it contains

  6. 6

    How do I take a list and print all of the content in a random order, printing each exactly one time in python?

  7. 7

    How do I grep recursively?

  8. 8

    How can I print DOM Content recursively?

  9. 9

    Collections.sort() affects all ArrayLists. How do I sort only one list and not the others?

  10. 10

    How do i add Points coordinates to a List of PointF of only one side of the rectangle?

  11. 11

    How can I search a directory recursively for any if statements with only one statement in them?

  12. 12

    How do I recursively increment the first value in a list and append to a nested list in python?

  13. 13

    How do I recursively compress only certain file extensions with 7zip command line?

  14. 14

    How do I restrict style to affect content only in modal?

  15. 15

    How do I replace bytes in multiple files in one directory recursively with Python 3.5.1?

  16. 16

    How can I list subdirectories recursively for HDFS?

  17. 17

    How can I list subdirectories recursively?

  18. 18

    How can I list files by type with ls?

  19. 19

    How do I add all elements from a list in F# recursively?

  20. 20

    How do I compare two folders recursively and generate a list of files and folders that are different?

  21. 21

    (Prolog) How do I rearrange elements in the list recursively to give multiple resulting states for an answer?

  22. 22

    How do I add all elements from a list in F# recursively?

  23. 23

    XSLT: how do I parse a document to retain only interesting nodes, along with all parents, at any depth?

  24. 24

    XSLT: how do I parse a document to retain only interesting nodes, along with all parents, at any depth?

  25. 25

    How do I create ActionBars recursively at runtime?

  26. 26

    How do I copy directories recursively with gulp?

  27. 27

    how do I create a ctags file recursively?

  28. 28

    How do I JS-Beautify recursively?

  29. 29

    How do I recursively pass a mutable reference?

HotTag

Archive