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?
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:2. About the size. We can find a file with certain size by the command (where 12672 is the file size in bytes):
Or we can use a range in this way (where 12600-12700 is the file size range in bytes):
Note that, by default the command
find
works recursively.3. Combine the two methods (where
file-1
is our pattern file):4. Example. Let's assume we have the following directory structure:
The result of the above command will be:
Or we can simplify the output by changing our command in this way:
Update from the comments. The following command finding for file with the same size as the
file-1
, and then thediff
commas is involved with--brief
and--report-identical-files
options:We can compare md5sum of the files in this way:
Get the md5sum of the pattern file:
Use it with our command:
grep -rl "string"
(-r for recursive, finding the string in files, -l for showing the filename, not the string)This might take a while, but it should be effective and reliable. It assumes you are using Bash. Replace
file
with the name of your renamed file:shopt -s globstar
turns on recursive globbing with**
. You can turn it off withshopt -u globstar
, but it is off by default and will be off when you open a new shell.for i in **
loop over all files below this one. Run the command from the highest level directory that might contain the file or the directory with the file or the directory... (apply recursion to this sentence!)[ -f "$i" ] &&
if the file is a regular file that exists then...cmp --silent file "$i" &&
if there is no difference betweenfile
and the file being examined (ie ifcmp
exits successfully), then...echo $i
print the relative path of the file (this also prints the path offile
itself, but I didn't see much benefit in fixing that).Thanks to this answer on Stack Overflow for the
cmp
method of comparing files.If you're looking for (or okay with) a GUI application, you may try the "FSlint Janitor" application. You can install it by running
How to use the application:
Once installed, follow the steps below.
Grep can find it quickly
When used properly, the
grep
command can find the duplicate quickly. You must be careful not to search the whole file system or it will take days to complete. I recently documented this here: `grep`ing all files for a string takes a long timeFor optimum speed use:
If your file might be on a Windows directory remove the
mnt
directory.If you know the file is within the
/home
directory someplace you can shorten the command: