Compare command output with file

[problem]

You want to run a command, save the output to a file – then compare the output at a later date with the file.

For example – you want to update your crontab: save a backup crontab -l > cronfile.$(date +%j), make your updates, then compare it with the backup file.

[/problem]

[solution]

Use diff with the hypen to represent read from stdin.

This saves keeping multiple copies.

[/solution]

[example]


crontab -l > cronfile.$(date +%j)
crontab -e # make your diffs
crontab -l | diff - cronfile.$(date +%j)

Also you can use cmp, which just reports the line number, etc


crontab -l > cronfile.$(date +%j)
crontab -e # make your diffs
crontab -l | cmp - cronfile.$(date +%j)

[/example]

[reference]

[tags]crontab, diff, cmp, comparing command output with file, UNIX Coding School[/tags]

[/reference]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

Your email address will not be published. Required fields are marked *