0

I want to compare the list of packages in a computer with an authorized list that I have. How would I do that? Would the following work?

cd /home
touch authorizedlist.txt
(list contents) >> authorizedllist.txt
touch currentlist.txt
dpkg -l | cut -d ' ' -f3 | less >> currentlist.txt
touch difflist.txt
diff authorizedlist.txt currentlist.txt >> difflist.txt

Is there a way to both create a new text file and add contents to it in one command instead of doing touch then putting the text in? Also, is there a way to only print what is unneeded from the currentlist and not what is missing from it?

2

Yes.

  • touch sets the modification timestamp of a given file to "now". If the given file doesn't yet exist, it is created. This is a common way to create empty files.

  • The >> redirection appends output to a given file. Similar to touch it creates the file if it doesn't yet exist. But keep in mind, it appends data to an existing file.

  • The > redirection overwrites a given file with the output of the preceeding command. If the file doesn't yet exist, it gets created. If it already exists, it gets truncated (that is: cleared or emptied) and then the output is written to it.

  • less is a so called pager. That is: it is meant for interactive usage so you can browse through larger files and use space and b (beyond others) to go forwards and backwards in a file or command output. If you redirect the output of a command to a file anyway, you can (and should) omit the less call.

Put together:

cd /home
(list contents) > authorizedlist.txt
dpkg -l | cut -d ' ' -f3 > currentlist.txt
diff authorizedlist.txt currentlist.txt > difflist.txt
  • Thanks for the explanation; it was clear and very helpful! – suomynonA Jan 21 '18 at 17:46
  • What about for the last question I asked in my post? – suomynonA Jan 21 '18 at 17:53
  • @suomynonA (Actually that is a separate question that should be posted as such.) I don't know what you mean exactly. All lines in currentlist that are not in authorizedlist or just the other way round or something else? – PerlDuck Jan 21 '18 at 18:01
  • yes, all lines in currentlist not in authorizedlist – suomynonA Jan 21 '18 at 18:10
  • 1
    I think I understand it. Unneeded = lines that are in current-list and not in authorized-list. – Terrance Jan 21 '18 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.