# How to compare a file on a host with a local file

**URL:** <https://forum.ansible.com/t/how-to-compare-a-file-on-a-host-with-a-local-file/28081>\
**Category:** Ansible Project\
**Created:** [July 18, 2018, 2:43pm UTC](https://forum.ansible.com/t/how-to-compare-a-file-on-a-host-with-a-local-file/28081 "2018-07-18T14:43:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Gedin](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@Sebastian\_Gedin](https://forum.ansible.com/u/Sebastian_Gedin)\
**Post date:** [July 18, 2018, 2:43pm UTC](https://forum.ansible.com/t/how-to-compare-a-file-on-a-host-with-a-local-file/28081/1 "2018-07-18T14:43:08Z")

</div>

Hello,

Is there any nice way of comparing a file on a host with a local file in Ansible? I don’t just want to know if they are different but also how they differ. Something like the output of the Linux diff command. The only way I can think of is first copying the file from the host to local and then using Linux diff through the command module, but I would prefer to do it without having to create a local copy of the file. Any ideas?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 18, 2018, 2:47pm UTC](https://forum.ansible.com/t/how-to-compare-a-file-on-a-host-with-a-local-file/28081/2 "2018-07-18T14:47:28Z")

</div>

use a copy action, with check and diff enabled:

- copy: src=/path/to/local dest=/path/to/remote  
&nbsp;&nbsp;check\_mode: yes  
&nbsp;&nbsp;diff: yes

---

<div class="post-metadata">

**Author:** ![Sebastian\_Gedin](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@Sebastian\_Gedin](https://forum.ansible.com/u/Sebastian_Gedin)\
**Post date:** [July 19, 2018, 7:17am UTC](https://forum.ansible.com/t/how-to-compare-a-file-on-a-host-with-a-local-file/28081/3 "2018-07-19T07:17:48Z")

</div>

Thanks!

Any way of making this work for directories? So that differences in each file in the directory and subdirectories are displayed. And to display if one file only exists in one directory or the other?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 19, 2018, 7:44am UTC](https://forum.ansible.com/t/how-to-compare-a-file-on-a-host-with-a-local-file/28081/4 "2018-07-19T07:44:19Z")

</div>

You might be better of using diff or some other diff tool.

&nbsp;&nbsp;&nbsp;diff -r dir1/ dir2/
