7

I'd like to know all distinct extensions of files tracked by git in a given repo, in order to create appropriate .gitattributes file.

Example output expected:

bat
gitignore
gradle
html
jar
java
js
json
md
png
properties
py
svg
webp
xml
yml

What command can I use for that?

2 Answers 2

10
git ls-tree -r HEAD --name-only | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u 

When you declare it as an alias, you have to escape $1:

alias gitFileExtensions="git ls-tree -r HEAD --name-only | perl -ne 'print \$1 if m/\.([^.\/]+)$/' | sort -u"

This is better than naive find, because:

  • it excludes untracked (gitignored) files
  • it excludes .git directory which contains usually hundreds/thousands of files and hence slows down the search

(inspired by How can I find all of the distinct file extensions in a folder hierarchy?)

Sign up to request clarification or add additional context in comments.

4 Comments

Any reason you used double quotes around the perl part? Also maybe it'd be worth using a hash rather than piping to sort - you could add && !$a{$1}++ (with single quotes around the whole command) to only print the first occurrence of each result.
If I use single quotes, it prints SCALAR(0xa031e3c)SCALAR(0xa031e3c).... I have to remove the escape before $1 for it to work back. But then when I declare an alias, I have to add the escape back. Updated.
Yeah, $ needs escaping from the shell inside double quotes. Better to just use a function in my opinion.
ad !$a{$1}++ I tested on a huge git repo (Chrome's blink) and the speed difference is negligible in practice (0.8s vs 1.0s on my machine). I think I'll leave sort for readability :)
1

If you have access to PowerShell, here is a nice one-liner which also gives you the count of how many files exist of each type:

$ext = @{}; git ls-tree -r HEAD --name-only | Get-Item | %{ $ext[$_.Extension]++ }; $ext

1 Comment

This just gives me an error. Get-Ittem : Illegal characters in path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.