Forum › Forums › General › Tips and Tricks › Apt Grep Dpkg Some useful commands and scripts
- This topic has 3 replies, 3 voices, and was last updated Apr 12-10:29 am by ModdIt.
-
AuthorPosts
-
April 12, 2022 at 8:49 am #81115Member
ModdIt
After a mishap with packages or sources figuring out, how when and why can be very useful.
Knowing what packages are installed on a fresh setup and comparing with current state too.Please do add further tips and usage examples to the thread.
EDITED, erroniously prefixed some commands with sudo. Thanks PPC and Sybok for headsup..List of installed packages with details apt list --installed Show only package names apt list --installed | awk '{split($0, a, "/"); print a[1]}' apt list --manual-installed | sed 's/\// /' | awk '{print $1 "=" $3}' Apt show gives detailed info to a package apt show packagename Using dpkg to get a nicely formatted list in the terminal dpkg-query -l To get only package names dpkg-query -f '${binary:Package}\n' -W Use grep to search, in this case for packages installed from debian multi media org dmo dpkg-query -l | grep dmo To search for manualy installed packages. This will include upgrades but will include some non manualy installed packages. Depends. comm -23 \ <(apt-mark showmanual | sort -u) \ <(grep -oP '^(?:Package|Depends):\s+\K.*' /var/log/installer/status \ | grep -oP '[^\s,()]+?(?=(?:\s+\([^)]+\))?+(?:,|$))' \ | sort -u) For a very complete history of installated packages. Takes a long time to run, includes Date and Time of install. #!/bin/bash # List of all packages currently installed current=$(dpkg -l | awk '{print $2}' | sort | uniq) # List of all packages that were installed with the system pre=$(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort | uniq) # List of packages that don't depend on any other package manual=$(apt-mark showmanual | sort | uniq) # (Current - Pre) ∩ (Manual) packages=$(comm -12 <(comm -23 <(echo "$current") <(echo "$pre")) <(echo "$manual") ) for pack in $packages; do packname=$(echo $pack | cut -f 1 -d ":") desc=$(apt-cache search "^$packname$" | sed -E 's/.* - (.*)/\1/') date=$(date -r /var/lib/dpkg/info/$pack.list) echo "# $desc" echo "# $date" echo "sudo apt-get install $pack" echo -e "" done- This topic was modified 1 year ago by ModdIt.
- This topic was modified 1 year ago by ModdIt.
- This topic was modified 1 year ago by ModdIt.
April 12, 2022 at 9:06 am #81117MemberPPC
::Very nice list of commands, Moddit – one note, you probably don’t need to run any of the one line commands with “sudo”, since the commands do not perform changes in your system…
Also, connected with this subject is the command:
my_installed_debs
I talked about it recently, on a thread… The file the command generates can be used with a simple script to install all the packages existing on a old system to a new system ( ex: if you had to reinstall antiX or just want to “clone” an install to a new computer- just install the same antiX version, install the same apps, copy the home folder and you’re set)Edit: the thread I mentioned is: https://www.antixforum.com/forums/topic/restoring-a-backup-of-my-home-directory/#post-79945
…and the command to install the applications from the file generated using the command “my_installed_debs” (assuming the file is placed in your home folder) is
sudo apt install $(grep -vE "^\s*#" ~/my_installed_debs.txt | tr "\n" " ")P.
- This reply was modified 1 year ago by PPC.
- This reply was modified 1 year ago by PPC.
- This reply was modified 1 year ago by PPC.
April 12, 2022 at 10:10 am #81124Member
sybok
::Few comments on @ModdIt’s original post:
1) Unnecessary use of ‘sudo’ while invoking ‘apt list’ and ‘dpkg-query’. Both commands work without ‘sudo’ for me.
2) The for-loop in the shell script may not be required (if not interested in description and date), multiple packages can be passed to ‘apt install’ at once, e.g.
sudo apt install $(echo "${packages}" | tr '\n' ' ')
should work as well.
Downside: one cannot see which package pulls in additional requirements when compared to the for-loop.April 12, 2022 at 10:29 am #81127MemberModdIt
::Thanks PPC and sybok,
put on my learner hat again, removed the sudo prefixes to avoid any confusion.
The apt intall a list of packages is really useful at times, as is using a list to uninstall any
known unneeded packages.Users please be careful when removing a list of packages. You can quickly end up needing to reinstall.
How I know.. See the red ears ;-). -
AuthorPosts
- You must be logged in to reply to this topic.