Where's the Fun in That?

Any solution starts with a problem. In my case, that problem is how do I find duplicate files in a directory on a Linux server? “Easy”, you may say, “Just write a python script”, or “There’s an app for that”. Fair enough, but where’s the fun in that? Why not challenge yourself? Let’s do it in a single bash line!! Here’s the beautiful, and ugly mess I came up with:

find /root/ -type f -exec md5sum '{}' \; > md5.txt && cat md5.txt | sort -u -k1 | cut -d' ' -f1| uniq -c | awk 'int($1)>1' | sort -rn | awk '{print $2}' > 2uniq.txt && md5s=$(cat 2uniq.txt) && for md5 in $md5s; do grep $md5 md5.txt; done >> matches.txt

Let’s break the command, and make since of it, but before that a few bash basics. A pipe | takes the results from a previous command and runs it against another command. The && allow for a new series of bash commands to run on the same line.

The highlighted section performs a find command against the root directory looking for -type of file (f). Then executing (-exec) md5sum on the files discovered taking the results creating a list of MD5 hashes, and file path for every file found.  

find /root/ -type f -exec md5sum '{}' \; > md5.txt && cat md5.txt | sort -u -k1 | cut -d' ' -f1| uniq -c | awk 'int($1)>1' | sort -rn | awk '{print $2}' > 2uniq.txt && md5s=$(cat 2uniq.txt) && for md5 in $md5s; do grep $md5 md5.txt; done >> matches.txt

The next series command take the md5.txt file, sort the unique (-u) results by first key (-k) value of 1, then cut those results by a space delimiter (-d’ ‘) returning the first field (-f1), then I count (-c) the unique (-uniq) results, then return only the results with a count greater than 1, sorting the results again in reverse order by number(-rn), then printing to screen the 2nd value of that results (awk '{print $2}'), and putting those unique hashes with count greater than 2 to a text (2uniq.txt).

find /root/ -type f -exec md5sum '{}' \; > md5.txt && cat md5.txt | sort -u -k1 | cut -d' ' -f1| uniq -c | awk 'int($1)>1' | sort -rn | awk '{print $2}' > 2uniq.txt && md5s=$(cat 2uniq.txt) && for md5 in $md5s; do grep $md5 md5.txt; done >> matches.txt

The next command (highlighted) takes the unique hashes with count greater than 2 from the text file 2uniq.txt, and catenate (cat), fancy word for prints, the results to a list (md5s).

find /root/ -type f -exec md5sum '{}' \; > md5.txt && cat md5.txt | sort -u -k1 | cut -d' ' -f1| uniq -c | awk 'int($1)>1' | sort -rn | awk '{print $2}' > 2uniq.txt && md5s=$(cat 2uniq.txt) && for md5 in $md5s; do grep $md5 md5.txt; done >> matches.txt

The next series of commands (highlighted) take the list of unique hashes with count greater than 2 list, and runs the hashes through a for loop, and globally search for a regular expression (grep) for the hash in the text file (md5.txt) that was created in the first series of commands. This creates a list of files that have matching file hashes?

find /root/ -type f -exec md5sum '{}' \; > md5.txt && cat md5.txt | sort -u -k1 | cut -d' ' -f1| uniq -c | awk 'int($1)>1' | sort -rn | awk '{print $2}' > 2uniq.txt && md5s=$(cat 2uniq.txt) && for md5 in $md5s; do grep $md5 md5.txt; done >> matches.txt

Like I said there’s easier ways to find out if you have duplicate files, but where’s the fun in that.

 

Comments

Popular posts from this blog

Mapping CIS Critical Controls to Gartner Markets

Malware Can Hide But It Must Run

PCAP Doesn't Lie Part 2