Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List files in order of last modification date

Tags:

git

My repo is overdue for a cleanup. I thought a good starting point would be to list all files in order of last time they were touched and start with the oldest. Is there a way to achieve this?

like image 857
pseudosudo Avatar asked Apr 28 '14 20:04

pseudosudo


People also ask

What command allows you to list files sorted by when they were last modified?

The 'ls' command lists all files and folders in a directory at the command line, but by default ls returns a list in alphabetical order. With a simple command flag, you can have ls sort by date instead, showing the most recently modified items at the top of the ls command results.

How do you arrange files by their modification date?

Right-click in an open area of the details pane and select Sort By from the pop-up menu. Select how you want to sort: Name, Date Modified, Type, or Size. Select whether you want the contents sorted in Ascending or Descending order.

How do I sort files by last modified?

Open File Explorer and navigate to the location where your files are stored. Sort files by Date Modified (Recent files first). Hold Shift key and click on the Name column. This will bring folders at the top with files sorted with Date Modified.

How do you display a list of all files sorted by time and date in Unix?

ls command ls – Listing contents of directory, this utility can list the files and directories and can even list all the status information about them including: date and time of modification or access, permissions, size, owner, group etc.


2 Answers

There is no way (of which I'm aware) to do this using the standard git commands. What you need is some sort of git blame for the whole repository, identifying the last commit in the history which changed each file. Of course, you could use the standard git blame, parse the porcelain output to identify the timestamp of the latest commit which added content to the file, and sort the files according to that timestamp:

#!/bin/bash

function last-modified()
{
    git blame -p "$1" | awk '
        BEGIN {
            print 0;
        }
        $1 == "author-time" {
            print $2;
        }' | sort -n | tail -n 1
}

function list-files()
{
    for file in $(git ls-files); do
        echo "$(last-modified $file) $file"
    done
}

list-files | sort -n

This approach is only able to register content being added to a file, though, not content being removed from the file. Also, it will break when there are lines in your repository starting with author-time.

like image 174
user3426575 Avatar answered Sep 20 '22 21:09

user3426575


Commit-level information should be enough to answer the OP question, no need for the sort of details that git-blame fetches.

This approach lists all files known to git with the date of the last commit that affects the file, sorted by that date:

while read FILE
do git log --pretty="%ad $FILE" --date=iso8601-strict -1 -- "$FILE"
done < <( git ls-files ) | sort

One might want to restrict to files currently checked out in their current directory:

while read FILE
do git log --pretty="%ad $FILE" --date=iso8601-strict -1 -- "$FILE"
done < <( find . -type f ) | sort

One might want to list only files, not showing dates:

while read FILE
do git log --pretty="%ad $FILE" --date=iso8601-strict -1 -- "$FILE"
done < <( git ls-files ) | sort | cut -f 2 -d " "

Other combinations are possible.

All those should work in case of files with spaces and other characters.

like image 38
Stéphane Gourichon Avatar answered Sep 24 '22 21:09

Stéphane Gourichon