Search code examples
linuxbashcommand-linels

ls all files in directory plus all files in directories one level down


Ok I have the following situation.

Caps are directories, lowercase are files.

A/aa
B/bb
C/cc
D/dd
D/E/ddd
D/F/G/dddd
a
b
c
d

I want to do a ls that lists

a
b
c
d
A/aa
B/bb
C/cc
D/dd

but not either

D/E/ddd
D/F/G/dddd

Solution

  • Using find to find only files in the current directory or one directory down:

    $ find . -maxdepth 2  -type f
    

    Demo:

    # Show whole directory structure, digits are files, letters are folders. 
    $  find .
    .
    ./1
    ./2
    ./3
    ./4
    ./A
    ./A/11
    ./B
    ./B/22
    ./C
    ./C/33
    ./D
    ./D/44
    ./D/E
    ./D/F
    ./D/F/444
    ./D/F/G
    ./D/F/G/4444
    
    # Find only files at a maximum depth of 2
    $  find . -maxdepth 2  -type f
    ./1
    ./2
    ./3
    ./4
    ./A/11
    ./B/22
    ./C/33
    ./D/44