I need to generate a list of all the images and css files with the modified dates, from a commit, for example.

git ls-tree -r --name-only HEAD | while read filename; do
  echo "$(git log -1 --format="%ad" -- $filename) $filename"

How would I modify this to just return jpg, gif, png, and css files?

  • 1
    In your place I'd man grep, seeking for regexes and multiple patterns. – Marco Jul 11 at 17:11
  • I'm trying to build a list of files that I need to purge cache on. Ideally I'd want to get a list of all files that were modified in a build, but the git ls-tree returns all files in the build. Since the list from git shows all files, not just those modified, I could settle for limiting to types and then make assumptions based on modified date to current date. – Kari Cowan Jul 11 at 19:58
  • If you consider my answer relevant to your question you can flag it as accepted. TY – Marco yesterday

Oh I see @Marco - thanks. It didn't occur to me I could toss a grep into the mix, for example this slight modification worked well.

--name-only HEAD | grep ".css" | while...

Since you need a list of all images and css files, ideally, you should pipe your output to something like:

grep '.gif$\|.jpg$\|.png$\|.css$'

Of course you could add more relevant extensions your project might contain, such as .bmp, for instance.

If I try ls |grep -i '.gif$\|.jpg$\|.png$\|.css$' I get a list of all files in current folder with given extensions.

Let's take a closer look at this command:

$ ensures you won't match any undesired file, such as .css.bak. $ matches end of line.

\| allows you to specify multiple patterns, so you don't need to invoke multiple git ls-tree to get the whole list.

Should your repo contain any files with uppercase extensions you might want to add -i in order to run grep case insensitively.

Patterns between single ' ensures dots are interpreted as dots, and not as "any character" as meant in regexes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.