Personal_Website/content/snippets/seconds-since-last-pacman-sync.md
2022-06-14 19:20:02 +02:00

53 lines
1.7 KiB
Markdown

---
title: "Seconds since last pacman sync"
date: 2022-06-11T08:45:13Z
tags: ["archlinux", "linux", "pacman"]
draft: false
---
## Result
We can find out how many seconds ago the last package was installed or updated using pacman by using the following command:
```sh {data-copy-button=true}
echo $(expr $(date +%s) - $(grep -rn -A 1 %INSTALLDATE% /var/lib/pacman/local/*/desc | grep -oP '\d{10,}$' | sort | tail -1))
```
---
## Explanation
Let's dive into the weeds to see how this is built up.
Pacman stores its local cache on your system at `/var/lib/pacman/local/`. This folder contains a couple files for every package.
Try reading the files for the kernel for example. We are only interested in the installation date, so let's focus on that field.
```sh
$ grep -A 1 '%INSTALLDATE%' /var/lib/pacman/local/linux-5.17.5.arch1-1/desc
%INSTALLDATE%
1651693442
```
By running this on all files in the pacman cache directory and then filtering for numbers, you get to see the entire list of dates for every package.
```sh
$ grep -A 1 '%INSTALLDATE%' /var/lib/pacman/local/*/desc | grep -oP '\d{10,}$' | head -5
1642498423
1644184812
1640442731
1640443485
1640443485
```
Now we just need to sort the entries and pick the highest value. This is the timestamp of our last package installation.
```sh
$ grep -rn -A 1 %INSTALLDATE% /var/lib/pacman/local/*/desc | grep -oP '\d{10,}$' | sort | tail -1
1654790640
```
Now we just have to subtract this timestamp from the current date to get the amount of seconds since the last package installation.
```sh {data-copy-button=true}
echo $(expr $(date +%s) - $(grep -rn -A 1 %INSTALLDATE% /var/lib/pacman/local/*/desc | grep -oP '\d{10,}$' | sort | tail -1))
```