2

Is there a way to identify packages, perhaps by analysing files in a package and when they were last accessed, to identify when a package was last used?

Are there perhaps existing solutions for this, maybe even part of apt already that would show me this?

It would be helpful to identify packages and applications that are rarely, or no longer, used and which could potentially be deleted from the system to free up space. (At least that's what I am trying to accomplish).

4
  • I doubt there is such a way. There may be binaries rarely or never used, but which are essential to the system (to be used in very exceptional, but very important cases).
    – FedKad
    May 29, 2019 at 8:00
  • @FedonKadifeli Thanks, I was just looking at the atime of all the files in /usr/bin and seeing what packages they belong to, to try and find the last time an executable from that package was used. I hope that might give me an indication of which packages with executables I could eliminate. May 29, 2019 at 8:03
  • I am really interested in this question, since I am not a very experienced user and/or administrator of Ubuntu. I will highly appreciate if an experienced user answers this question.
    – FedKad
    May 29, 2019 at 8:48
  • @FedonKadifeli I have provided an answer with a sample solution as a Ruby script which basically does what I described in my previous comment. May 29, 2019 at 11:02

1 Answer 1

1

Though I am sure there is a way to do this with just Bash scripting, I wrote a little script (which calls shell commands anyway to execute dpkg) in Ruby.

What it does is:

  1. Calls dpkg -S /usr/bin/* to get a list of all the packages that each of the files in /usr/bin belong to.
  2. Build an array of each package with the latest date of a contained file from the same directory, by comparing the last accessed file (atime) of each file in the package and sorts the list by the time
  3. Output the results to a tab delimited file

A better approach would probably be to go through each package, list all the files in the package, and find the last access time for each file, and so find the maximum access time for each package. (Which sounds like it should be doable with some single line Bash script).

Perhaps someone could be so kind as to provide a Bash-only version that does the same thing?

# Obtain a list of files in /usr/bin with the corresponding package they belong to:
packfiles = (`dpkg -S /usr/bin/* 2>/dev/null`).split("\n") # errors written to /dev/null

# For each executable in a package, get the last accessed time and save the latest time per package
packagetimes = packfiles.reduce({}) {|packages, pf|
  package, file = pf.split(' ') # Split line into package name and file path
  package.chomp!(':') # Remove last colon after package name
  packages[package]
  lat = open(file).atime # Get the last accessed time of the file
  #Take greater of package last change time or time for file for package:
  packages[package] = (packages[package] && packages[package] > lat ? packages[package] : lat) 
  packages #for next reduce iteration
}.
# Produce a sorted list of packages and times
reduce([]) {|arr, entry| arr << [entry[0],entry[1]] }. # Create array with package, time
sort_by {|line| line[1]} # Sort array by time

# Write out the list of packages and times
open('./pusage.txt', 'w') {|out|
  packagetimes.each {|pt|
    out << "#{pt[0]}\t#{pt[1]}\n"
  }
}

You can test it, as I did, by running some binary from some package and seeing that, when you run the script again, the timestamp next to the package changes and moves it to the bottom of the list.

This kind of works for me, though I would still be a bit hesitant to delete some of the packages, even though their usage seems like it might date back to when I installed the system on my machine.

1
  • If you are asking for a Bash solution, you should mention that in the question, especially by the tag.
    – Melebius
    May 29, 2019 at 12:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .