Ubuntu – How to delete old versions of same packages from a Local Repository

cleanuplocalrepository

In my local repository see below, I have many versions of same packages downloaded and stored in deb package directory. As Ubuntu always prefer to use the highest version, I don't need the earlier versions downloaded and used. (All are in a same folder).

How can I delete the earlier versions of packages from my local deb's folder ? It is not a feasible solution to delete them manually. (You can't examine the versions in thousands of files)

Any ideas?!!

Maybe a script that checks the version of each package keeping the newer version and deletind older ones can solve this issue.

I am seeking for such script or if some software is founded would be great

I have made my local repository following this how to tutorial.

*** A local repository is an offline storage of packages already downloaded, so that you don't need to download it again)

Note: I'm not referring to clearing the cache of downloaded packages, which can be done with apt-get clean command.

Best Answer

I've deleted the old versions of same packages following this. I used the inversion of the feature of dpkg-scanpackages's multiple version scanning feature.

  1. First install dpkg-dev package

    sudo apt-get install dpkg-dev
    
  2. Then generate a file with the name of packages (only newer will be listed) dpkg-scanpackages without the -m option. The default is without -m option.

    If you have .deb files in a folder named deb, run the below command from the parent of this folder

    dpkg-scanpackages deb /dev/null 2>/dev/null | grep Filename: > filenames
    

    This will create a file with name filenames which have all the .deb files' name listed in a format Filename: deb/packagename_version.deb.

    We now have all the names of files with newest versions in a file named filenames

  3. The task is now simple, modify the script to move all those files in another folder.

    1. First replace the Filename: with mv

      sed -i 's/Filename:/mv/' filenames
      
    2. Now create a folder in the parent directory of deb folder. I named it newest (junk-free could be a good one ;P).

    3. Again change the filenames file to move the .deb files in the newly created newest folder.

      sed -i 's/\.deb/.deb newest/' filenames
      

      This will make our filenames file a list of mv commands moving .deb files from deb directory to newest directory

    4. Now guess what. Execute the file filenames

      sh filenames
      
    5. The last step is delete the folder with older obsolete .deb files. Check the newest folder too as a pre-caution.

Update with one liner

After installing dpkg-dev packages, generate the move script with a single command, use this one by going to the parent of the .deb files' folder.

 dpkg-scanpackages deb /dev/null 2>/dev/null | grep Filename: | sed 's/Filename:/mv/;s/\.deb/.deb newest/' > filenames

Then create a folder named newest and execute the file filenames with `sh filenames* command.

Related Question