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?
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.
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.
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.
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With