Compare two files and print results to new text file
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.txtIs 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?
01 Answer
Yes.
touchsets 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 totouchit 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.lessis 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 thelesscall.
Put together:
cd /home
(list contents) > authorizedlist.txt
dpkg -l | cut -d ' ' -f3 > currentlist.txt
diff authorizedlist.txt currentlist.txt > difflist.txt 8