简体   繁体   中英

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?

There's nothing wrong with a simple

find ~/test -mindepth 1

Similarly, this will have the same effect:

find ~/test/*

as it matches everything contained within ~/test/ but not ~/test itself.

As an aside, you'll almost certainly find that find will complain about the -mindepth n option being after any other switches, as ordering is normally important but the -(min|max)depth n switches affect overall behaviour.

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.

Then you need -type f instead of -type d .

Or, if you want to display list of folders, excluding the parent -mindepth 1 ( find ~/test -type d -mindepth 1 ).

And now that you edited it, I think what you want may be

find ~/test -type d -mindepth 1 |cut -d/ -f3-

But I think you need to be more specific ;-)

I just fixed it with sed

find $BASE -type d \( ! -iname "." \)|sed s/$BASE//g

Where $BASE is initial foldername.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM