How to delete old versions of same packages from a Local Repository?
Solution 1
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.
First install
dpkg-dev
packagesudo apt-get install dpkg-dev
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
The task is now simple, modify the script to move all those files in another folder.
First replace the
Filename:
withmv
sed -i 's/Filename:/mv/' filenames
Now create a folder in the parent directory of deb folder. I named it newest (junk-free could be a good one ;P).
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 directoryNow guess what. Execute the file filenames
sh filenames
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.
Solution 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:
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)
Create a list of all packages in your pool:
find pool -type f | sort -u > files
Create a list of all packages in your mirrored distribution:
grep "^Filename:" `find dists/ -name Packages`|sed "s/^.*Filename: //"|sort -u > packages
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
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.
Solution 3
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
Related videos on Youtube
Admin
Updated on September 18, 2022Comments
-
Admin over 1 year
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.-
Anwar over 11 years+1 for the question. I also have a local repository and i delete my older versions with some tedious method (semi-automatic script)
-
Admin over 11 yearsThanks..Could you show me that script?! please
-
nanofarad over 11 years@OliveTwist I, too, would love to see the script.
-
Anwar over 11 years@ObsessiveSSOℲ I've given the answer. you can check.
-
-
NeilB over 11 years+1.nice script. leyt me check and i'll provide my results
-
Kolky about 8 yearsI 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