Find an identical file with a different name [duplicate]

Is it possible to find a without knowing its name?

I created a file with LaTex, then I copied it into another local directory and renamed the pdf. I don't know any more where the original file is located, but I have the renamed file on hand. I would like to make some modification to my latex file and recreate the pdf.

Since I know the original file is exactly the same as the renamed one except for the name, is there any way I can find my original file?


Solution 1:

When the only difference is the name booth files should have the same content and size.

1. About the content. We can compare two files by the command diff file-1 file-2. Also we can use this command for a test in this way:

diff -q file-1 file-2 > /dev/null && echo 'equal' || echo 'different'

2. About the size. We can find a file with certain size by the command (where 12672 is the file size in bytes):

find /path/to/search -type f -size 12672c -printf '%p\n'

Or we can use a range in this way (where 12600-12700 is the file size range in bytes):

find /path/to/search -type f -size -12700c -size +12600c -printf '%p\n'

Note that, by default the command find works recursively.

3. Combine the two methods (where file-1 is our pattern file):

find /path/to/search -type f -size -12700c -size +12600c -printf '%p\t' -exec sh -c 'diff -q file-1 "$1" > /dev/null && echo "equal" || echo "different"' sh {} \;

4. Example. Let's assume we have the following directory structure:

$ tree /tmp/test
/tmp/test
├── file-1   # this is the pattern file
├── file-2   # this is almost the same file but wit few additional characters
└── file-3   # this is exact copy of file-1

The result of the above command will be:

$ find /tmp/test -type f -size -12700c -size +12600c -printf '%p\t' -exec sh -c 'diff -q file-1 "$1" > /dev/null && echo "equal" || echo "different"' sh {} \; 
/tmp/test/file-2        different  # OK: here we have added few additional characters
/tmp/test/file-3        equal      # OK: this is exact copy of file-1
/tmp/test/file-1        equal      # OK: this is file-1 compared to its self

Or we can simplify the output by changing our command in this way:

$ find /tmp/test -type f -not -name "file-1" -size -12700c -size +12600c \
  -exec sh -c 'diff -q file-1 "$1" > /dev/null && printf "%s\tis equal\n" "$1"' sh {} \;
/tmp/test/file-3        is equal

Update from the comments. The following command finding for file with the same size as the file-1, and then the diff commas is involved with --brief and --report-identical-files options:

find /path -type f -not -name "file-1" -size $(stat -c%s file-1)c -exec diff -qs file-1 {} \;
Files file-1 and /tmp/test/file-3 are identical

We can compare md5sum of the files in this way:

  • Get the md5sum of the pattern file:

    $ md5sum file-1
    d18b61a77779d69e095be5942f6be7a7  file-1
    
  • Use it with our command:

    $ find /path -type f -not -name "file-1" -size $(stat -c%s file-1)c -exec sh -c 'echo "d18b61a77779d69e095be5942f6be7a7 $1" | md5sum -c -' sh {} \;
    /tmp/test/file-3: OK
    

Solution 2:

  • You can search for a particular string with grep -rl "string" (-r for recursive, finding the string in files, -l for showing the filename, not the string)