Search code examples
linuxbashfind

find folders in a directory, without listing the parent directory


Having trouble listing the contents of a folder I'm not in, while excluding the actual folder name itself.

ex:

root@vps [~]# find ~/test -type d
/root/test
/root/test/test1

However I want it to only display /test1, as the example.

Thoughts?


Solution

  • You can do that with -exec and basename:

    find ~/test -type d -exec basename {} \;
    

    Explanation:

    • The find ~/test -type d part finds all directories recursively under ~/test, as you already know.
    • The -exec basename {} \; part runs the basename command on {}, which is where all the results from the last step are substituted into.