Star Hype News.

Premium celebrity moments with standout appeal.

updates

Compare two files and print results to new text file

By James Williams

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?

0

1 Answer

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
8

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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