How to check the duplicancy of file using shell script?

Hi all,
i am getting the file through sftp from a location in the name of 20141201_file.txt (iam getting the file on daily basis with date appended at the beginning)

so before going to my actual process ,i want to check whether today files and yesterday file are same or not(which i used to get through sftp)....how can i achieve this check through script....?

thanks in advance
hemanth saikumar

Use "date" command to derive today's & yesterday's date and then check the file size (or) checksum to make sure they are different.

The same ways as if you were in command line, using unix file comparison utilities...

If those files are in a dedicated directory, I mean if there are no files other than the files you want to compare, try this

ls -t | head -n2 | paste - - | xargs diff -q

If there is no output, the files are the same.

If there are other files in the same directory, you would need to change ls to something like ls -t 20*_file.txt or the like.

In a script, the above approach could be something like

if ! ls -t | head -n2 | paste - - | xargs diff -q >/dev/null
then
    echo "Files differ"
else
    echo "Files are the same"
fi
2 Likes