5

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.

4
  • +1 for the question. I also have a local repository and i delete my older versions with some tedious method (semi-automatic script)
    – Anwar
    Oct 10, 2012 at 9:39
  • Thanks..Could you show me that script?! please
    – user61928
    Oct 10, 2012 at 10:11
  • 2
    @OliveTwist I, too, would love to see the script.
    – nanofarad
    Oct 10, 2012 at 12:42
  • @ObsessiveSSOℲ I've given the answer. you can check.
    – Anwar
    Oct 11, 2012 at 5:23

5 Answers 5

5

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.

2
  • +1.nice script. leyt me check and i'll provide my results
    – user61928
    Oct 11, 2012 at 5:33
  • Based on your solution, I created this one-liner that directly deletes the old versions dpkg-scanpackages $repo 2>&1 >/dev/null | grep 'ignored data from' | sed 's/.*ignored data from \(.*\)!/\1/' | xargs rm -f. You can use it in a script defining the directory to be cleaned as $repo. Or you just replace $repo with the directory to be scanned on the command line. Mar 1 at 18:35
2

I had the same problem: I created a local repository using apt-mirror and this tool downloads new packages but leaves all the old packages in place. I did the following to cleanup my local mirror:

  1. Go to the location of your top of your mirror (the directory where the dists and pool directory are located, in my setup /var/spool/apt-mirror/mirror/ftp.nl.debian.org/debian)

  2. Create a list of all packages in your pool:

    find pool -type f | sort -u > files
    
  3. Create a list of all packages in your mirrored distribution:

    grep "^Filename:" `find dists/ -name Packages`|sed "s/^.*Filename: //"|sort -u > packages
    
  4. Now create a diff between the two files and convert it into a cleanup script:

    diff files packages | sed "/^[1-9]/d;s/^< /rm /" > cleanup.sh
    
  5. Execute the generated script

Make sure you have write-permissions on the repository (or execute using sudo). Instead of removing you can also move the files to a temporary location.

The idea behind this is that you can remove the files that are not mentioned in one of the Packages files, as they are replaced with newer versions or are deprecated. If files are not mentioned in the Packages files they cannot be found by clients connecting to your repository.

1
  • I updated your last statement to also remove --- and > lines found in the diff; diff files packages | sed "/^[1-9]/d;/^---/d;/^>/d;s/^< /rm /" > cleanup.sh
    – Kolky
    Apr 1, 2016 at 9:54
1

Update with one liner To Delete (not to move)

this one liner will delete only old versions of same packages

run it as root

sudo dpkg-scanpackages /var/cache/apt/archives 2>&1 >/dev/null | grep -Po '((\/.*?deb)(?=.*?repeat;))|used that.*?\K(\/.*deb)' | xargs rm

"/var/cache/apt/archives" replace with your .deb directory

it works very good in Ubuntu 12.10

0

Nice workaround from "Anwar" (in the first answer)! Only this line I would change:

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

because if you have a Package called: packagename.debian_123.deb your line will only replace the first ".deb" in this line and that would look like this in the end:

packagename.deb newest_123.deb

Take this line instead, and only the last .deb in every line will be replaced:

sed -i 's/\(.*\).deb/\1.deb newest/' filenames
0

In Thunar:

  1. Open /var/cache/apt/archives
  2. Select files based on access time:
    • click View >> View as Detailed List
    • click View >> Configure Columns >> enable Date Accessed
    • click on the Date Accessed table header, to sort the files
    • ctrl-click to select multiple files, shift-click to select a range of files
  3. Delete the files, either via right-click >> Delete (if Thunar was opened with sufficient privileges) or:
    • Right click >> choose Rename
    • choose Insert/Overwrite, set a custom prefix
    • open a terminal, sudo rm /var/cache/apt/archives/customprefix*.deb

You must log in to answer this question.