Results 1 to 10 of 15

Thread: Package Management from the Command Line

Threaded View

  1. #1
    Join Date
    Nov 2006
    Location
    There and back again
    Beans
    1,097

    Package Management from the Command Line

    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:

    Code:
    apt <option> <*option2>
    'option2' is often a package name but sometimes isn't needed. For example:

    Code:
    apt i gimp
    will install Gimp, and:

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

    Code:
    #!/bin/bash
    # Frontend for common Debian/Ubuntu package management tasks
    # http://www.cyberciti.biz/ref/apt-dpkg-ref.html
    
    # Display usage if no parameters given
    if [[ -z $@ ]]; then
      echo "apt <option> <*package> - debian management tasks
      c | clean   - delete downloaded packages that are extinct (to make space)
      h | hold    - hold/freeze a package (prevent it from being updated)
      i | install - install package                (and dependencies)
      I | reinst  - reinstall package
      l | list    - list files installed by package
      L | local   - install a local package (.deb) (and dependencies)
      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
      S | source  - download package source code and pkg creation information
      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
      ---
      fixpkgman   - fix a package system that's broken (possibly, emergencies only)
      forceremove - remove package forcibly (ignore parents)
      pin         - prevent package from being installed (a.k.a. locking)
      unpin       - remove pinning (locking) of package
      rmlock      - remove package manager lock"
      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
                    ;;
      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 | reinst  ) shift
                    sudo apt-get install --reinstall "$@"
                    ;;
      l | list )    shift
                    dpkg -L "$@"
                    ;;
      L | local )   shift
                    # sudo dpkg -i "$@"
                    for p in "$@"; do sudo gdebi "$p"; done
                    ;;
      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 "$@"
                    ;;
      # ---
      fixpkgman )   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
                    ;;
      forceremove ) shift
                    sudo dpkg --force-all --remove "$@"
                    ;;
      pin )         shift
                    for p in "$@"; do
                    inst_pkg=$(apt-cache search $p | awk '{print $1}' | grep -x $p)
                      if [ -z $inst_pkg ]; then
                        echo " Package \"$inst_pkg\" appears not to exist"
                        exit
                      else
    echo "
    Package: "$p"
    Pin: version  0.0
    Pin-Priority: -1" | sudo tee -a /etc/apt/preferences
                      fi
                    done
                    ;;
      unpin  )      shift
                    sudo sed -i "/^.*"$@"/,/-1/d" \
                    /etc/apt/preferences
                    ;;
      rmlock )      [ -f /var/lib/dpkg/lock ] && echo " Lock found, removing..."
                    # Taking down owning app is taking down system
                    #sudo fuser -cuk /var/lib/dpkg/lock; \
                    #sudo rm -f /var/lib/dpkg/lock
                    sudo rm -f /var/lib/dpkg/lock
                    ;;
      * )           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

    Any comments and tips appreciated.
    Last edited by Gen2ly; May 19th, 2012 at 07:14 AM. Reason: Updated script.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •