PDA

View Full Version : Package Management from the Command Line


Gen2ly
July 26th, 2009, 03:02 AM
I like to work from the command line. I found that once I got to know it, it was quicker to do the tasks that I needed to do. Here is a quick front-end to common package management tasks that I do in Ubuntu (installing/removing packages, package information...). It's well commented and includes help by just typing the command. I call it apt and the syntax is simple:

apt <option> <*option2>

'option2' is often a package name but sometimes isn't needed. For example:

apt i gimp

will install Gimp, and:

apt y

will sync your computer with the package repository database. Here's the script:

#!/bin/bash
# Frontend for common Debian/Ubuntu package management tasks
# http://www.cyberciti.biz/ref/apt-dpkg-ref.html

if [[ -z $@ ]]; then
echo "apt <option> <*package> - debian management tasks
c | clean - delete downloaded packages that are extinct (to make space)
f | fix - fix a package system that's broken (possibly, emergencies only)
h | hold - hold/freeze a package (prevent it from being updated)
i | install - install package (and dependencies)
I | local - install a local package (.deb) (and dependencies)
l | list - list files installed by package
n | info - package information
o | owns - package ownership of a file
q | query - lookup installed package
Q | Query - lookup installed package and version
r | remove - remove package (and unneeded dependencies)
R | purge - remove package and it's configurations
s | search - search for package and package description
u | update - update system
y | sync - sync repository database
ppaadd - add ppa repository
pparem - remove ppa repository
Distup - distribution upgrade
taskinst - add a package group
tasklist - list package groups
taskpkg - lisk packages in a group
taskrem - remove package group"
exit
fi

case $1 in
c | clean ) sudo apt-get autoclean # rm extinct d/l pkgs
# sudo apt-get clean # rm all d/l pkgs (clean all?)
sudo apt-get autoremove
;;
f | fix ) sudo mv /var/lib/dpkg/available{,.bck}
sudo touch /var/lib/dpkg/available
sudo rm -vf /var/lib/apt/lists/* # rm repo list
# https://wiki.ubuntu.com/ReducingDiskFootprint#Disable_apt_caches
sudo mv pkgcache.bin{,.bck}
sudo mv srcpkgcache.bin{,.bck}
sudo apt-get update # new repo list, rebuild cache
# sudo apt-get clean
# sudo mv /var/cache/apt /var/cache/apt-01
;;
h | hold ) shift
echo "$@" hold | sudo dpkg --set-selections
;;
i | install ) shift
#sudo apt-get update # Sync - bbs says here, 24 hrs?
# sudo apt-get upgrade # Update software
sudo apt-get install "$@" # Install software, here thght org
;;
I | local ) shift
# sudo dpkg -i "$@"
sudo gdebi "$@"
;;
l | list ) shift
dpkg -L "$@"
;;
n | info ) shift
apt-cache show "$@"
;;
o | owns ) shift
dpkg -S "$@"
;;
q | query ) shift
dpkg -l | grep ^[h,i]i | awk '{print $2}' | grep "$@"
;;
Q | Query ) shift
dpkg -l | grep ^[h,i]i | awk '{print $2"_"$3}' | grep "$@"
;;
r | remove ) shift
sudo apt-get remove "$@" && sudo apt-get autoremove
;;
R | purge ) shift
sudo apt-get --purge remove "$@" && sudo apt-get autoremove
;;
s | search ) shift
apt-cache search "$@"
;;
S | source ) shift
apt-get source "$@"
;;
u | update ) sudo apt-get update
sudo apt-get upgrade
;;
y | sync ) sudo apt-get update
;;
ppaadd ) shift
sudo add-apt-repository "$@"
sudo apt-get update
;;
pparem ) shift
sudo ppa-purge "$@"
#sudo apt-get update
;;
Distup ) sudo apt-get update
sudo apt-get dist-upgrade
;;
taskinst ) shift
sudo tasksel install "$@"
;;
tasklist ) sudo tasksel --list-tasks
;;
taskpkg ) shift
sudo tasksel --task-packages "$@"
;;
taskrem ) shift
sudo tasksel remove "$@"
;;
* ) shift
sudo apt-get "$@"
;;
esac

To have the script behave as a normal program (i.e. by typing it from the command line, you'll have to create a scripting environment. You can read more on how to do that here:

Setting up a Scripting Environment (http://linuxtidbits.wordpress.com/2009/12/03/setting-up-a-scripting-environment/)

Any comments and tips appreciated.

Gen2ly
July 27th, 2009, 01:31 AM
Oy, fixed a typo in the list line.

Elep.Repu
July 29th, 2009, 05:48 PM
Thanks for the tip. I really couldn't find a good one, but now I have.
Good day.

Gen2ly
August 1st, 2009, 05:41 PM
Glad you could put it to use Elep.Repu. Danke.

Gen2ly
August 5th, 2009, 05:09 AM
Oop, fixed some typos.

Gen2ly
August 17th, 2009, 11:35 PM
Removed tasksel option (didn't use anyway).

Gen2ly
November 18th, 2009, 06:15 PM
Cleaned up script a little bit more, added daemon option.

Gen2ly
May 1st, 2010, 10:48 PM
Cleaned up script. Nobody uses my script :(... :)... :(

petteriIII
May 2nd, 2010, 12:28 AM
But ofcourse uses, but many of us don't give feedback even if it is important. Me, for one - I read these posts reqularly to get new ideas and your post gave also some. Thanks.

Gen2ly
May 2nd, 2010, 09:51 PM
Thanks for saying such petteriIII, you made my day. :)

dusdus
May 3rd, 2010, 04:59 AM
First of all, thanks for trying and making life on the command line more convenient for all of us :)

I won't be using your script though, as I already use these options by adding aliases in my .bashrc
To be honest, I prefer it this way, as I keep a central point where I can change the terminal behaviour (I also like fancy colors in my terminal :)

Anyway, keep the good work going!

Gen2ly
September 22nd, 2011, 02:32 PM
Updated script. Was using on a Debian system earlier and am now using Ubuntu. Change log:

- 'sudo' prepended for the the commands I thing that need it (pretty sure I got all of them)
- Added removing repository information and downloading new repository information to the clean command as this can sometimes become corrupted.
- Changed to gdebi to install local packages (.debs already downloaded) as it is supposedly able to check for dependencies (untested).

Gen2ly
October 26th, 2011, 10:52 PM
Another script update. Change log:

Re-ordered to better group similar tasks
Rename ppa adding and subtracting options
Fixed ppa add and subtract not always getting recognized.

Gen2ly
January 5th, 2012, 10:19 PM
Script updated. Change log:

Query option now only returns if package is installed; added additional 'Q | Query' option to get version information along with package name.
Reordered help description