简体   繁体   中英

Number of files and directories in linux directory, only in level 2

The instruction

$ ls | wc -l

gives us the number of directories and files that are in a certain directory without counting those that exist within the subdirectories of this first level, that is, it gives us the number in a non-recursive way.

How could you know the number of files and directories that are only in the second level of that same directory? That is, the number of subdirectories and files in the subdirectories of the main directory, also in a non-recursive way, only at level 2.

The instruction:

$ shuf -ezn 7 directory/*/*/* | xargs -0 -n1 echo

gives us 7 files or subdirectories random chosen from the second level of the main one. It works perfectly, but I am unable to reason a similar instruction for what I want to achieve

I hope I have explained myself. Thank you

There are probably other better options, but I think I've found it:

$ find directory/  -mindepth 2 -maxdepth 2 | wc -l

gives me the expected result.

Hope it helps someone

This command should work:

$ ls * | wc -l

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