0

I have a repo with the following structure and I wouldlike to countnumber of subdirectories inside tier1, tier2 and tier3. I dont want to count subdirectories within the subdirectory. For examplee i have folders named a 1, 2, 3 inside tier1 and i wanted to see the count as 3. I dont want whats isnide those 1,2,3 folders. Repo Structure

Git clone actions should be avoided, as we do not need a local clone of the whole repo plus all history information. A simple fetch will be enough, is there are any leaner ways to retrieve the directory information ??

Am presently counting number of subdirectories by, entering each folder and the folloing command:

ls | wc -l
aynber
  • 22,380
  • 8
  • 50
  • 63
Manoj Kumar
  • 139
  • 7
  • Does this answer your question? [Recursively counting files in a Linux directory](https://stackoverflow.com/questions/9157138/recursively-counting-files-in-a-linux-directory) – nickl- Jun 23 '22 at 05:39
  • @nickl- i wanted to count only the sub-directories not files, in the given link it is counting number of files. – Manoj Kumar Jun 23 '22 at 06:01
  • `find . -type d` will list only directories, instead of `-type f` which does files, as per the other examples – nickl- Jun 23 '22 at 10:37

3 Answers3

2

Git clone actions should be avoided, as we do not need a local clone of the whole repo plus all history information. A simple fetch will be enough, is there are any leaner ways to retrieve the directory information ??

You can filter your clones to skip the actual content, just get the structure. For the linux repo this is a ~2.5M download, a ~99% savings:

git clone --bare --depth 1 --filter=blob:none u://r/l checkit
git -C checkit ls-tree --name-only -d @:

lists the toplevel directories, then it's just a formatting question,

for d in $(git -C checkit ls-tree --name-only -d @:)
do printf '%7d %s\n' $(git -C checkit ls-tree -d @:$d|wc -l) $d
done
jthill
  • 55,082
  • 5
  • 77
  • 137
0
git ls-tree -d HEAD:<dir>

git ls-tree -d HEAD:<dir> | wc -l

you can replace HEAD with any commit-ish reference :

git ls-tree -d origin/master:<dir>
git ls-tree -d v2.0.3:<dir>
git ls-tree -d <sha>:<dir>
...

If you want the list of all (recursive) subdirectories, add the -r option.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • edit: I initially thought you wanted the recursive list of subdirectories, my initial answer had the `-r` option on all `git ls-tree` commands – LeGEC Jun 23 '22 at 05:50
  • Thanky you for your answer. The commnd counting also the subdirectories inside the subdirectories. But, in my case i just wanted to count the subdirectories inside tier1. (for examplee i have folders named a 1, 2, 3 inside tier1 and i wanted to see the count as 3. I dont want whats isnide those 1,2,3 folders.) And for this command, the repo has to be cloned first, is there a way to count no.of directories without cloning a repo ? – Manoj Kumar Jun 23 '22 at 06:13
  • @ManojKumar : the easiest way is to clone first. Otherwise, you have to send a query to the central repository. This can be done either through an API call (if your central repo is hosted on github, azure, gitlab.com ...) or you can ssh to the central server and run git commands on the cenrtal repo directly (if you have a self hosted repository). – LeGEC Jun 23 '22 at 07:37
0

ls at best gives you the number of non-hidden entries directly inside the directory. If you have among them a plain file, or an entry containing spaces, or an entry where the name strats with a period, or a directory entry which is a directory, but has itself subdirectories, your count will be wrong.

I would instead do a

shopt -s nullglob
for topdir in tier[1-3]
do
  if [[ -d $topdir ]]
  then
    a=($(find "$topdir" -type d))  
    echo "Number of subdirectories below $topdir is ${#a[@]}"
  fi
done

The purpose of setting nullglob is only to avoid an error message if no tier directory exists.

UPDATE: If you are not interested in subdirectories further down, you could do instead of the find a

shopt -s dotglob
a=("$topdir"/*/)

The dotglob ensures that you are not missing hidden subdirectories, and the final slash in the glob-pattern ensures that the expansion happens only for entries which denote a directory.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • thank you for the solution. It is working, but not as expected. I just wanted to count no of directories isnide tier 1. But, it is counting the subdirectories, within the directories as well, can we avoid it ? – Manoj Kumar Jun 23 '22 at 06:10
  • Your question does not express this. Please always be precise when asking something. I will update my answer accordingly. – user1934428 Jun 23 '22 at 06:12