Generating MD5's of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generating MD5's of files
# 1  
Old 10-22-2010
[SOLVED] Generating MD5's of files

On my website I host a lot of files, and when people view the site, currently each time the page loads, I have PHP generating the md5 sums for the files right then and there. It was fine when my site was small, but now that's obviously very inefficient. Now I'd like to start generating MD5 sums when a user uploads a file, and storing it in a text file along with other information to be read from there instead.

I know how to incorporate this into my PHP script, however I still have the problem of the hundreds of files that have already been uploaded, that won't have MD5 information stored in their .desc file.

Here's my site's structure:

files/HTC Eris/Rom maker/rom.zip
files/HTC Eris/Rom maker/rom.zip.desc
files/HTC Eris/Rom maker/rom.zip.dlcnt
files/HTC Incredible/Rom maker2/rom2.zip
files/HTC Incredible/Rom maker2/rom2.zip.desc
files/HTC Incredible/Rom maker2/rom2.zip.dlcnt

...and so forth.

The .desc file already stores certain information such as username, IP, and a file description. I want to append the md5 information to the end of the .desc file. The .dlcnt file is irrelevant, it stores amount of times downloaded.

So basically I'm trying to write a bash script to do this. Trouble is... I know squat about bash scripting! Smilie

All I've been able to come up with so far is this:

Code:
#!/bin/bash

find -name *.zip >> files.lst
find -name *.apk >> files.lst
find -name *.exe >> files.lst
find -name *.png >> files.lst
find -name *.tar.gz >> files.lst
find -name *.img >> files.lst
find -name *.xml >> files.lst

for i in $( cat files.lst );
do
		md5sum $i >> $i.desc;
done

I'm sure this is a really horribly inefficient way to do this, but it's all I know.

First problem is that a lot of the directories and files have spaces in them. That screws up the script right there. I don't know how to change the names to have a \ in them.

Second problem is I'm not exactly sure how to output the md5sum to the .desc file. Obviously what I have written up there doesn't work, it's not proper use of the variable... but I'm not sure what to do?

Can anyone give me a hand? Smilie

Last edited by GrdLock; 10-23-2010 at 12:28 AM.. Reason: Solved
# 2  
Old 10-22-2010
find and xargs are good friends; one finds files and prints them, the other converts lines into command arguments. They're such good friends they even have a mutually-agreed-on format for communicating names into xargs without messing up on spaces: nulls. -print0 will tell find to print using nulls as seperators, and --null will tell xargs to read them likewise.

You can specify more than one extension with find's -or option, as well.

And, wherever you have 'for LINE in `cat file` you can do 'while read LINE ; do something ; done < file', much more efficient. It can also read from a pipe.

So you can cram this all into one efficient statement:
Code:
find /path  '(' -name '*.type' -or -name '*.type2' -or -name '*.type3' ')' -print0 |
        xargs --null md5sum |
        while read CHECKSUM FILENAME
        do
                echo "${CHECKSUM} ${FILENAME}" >> "${FILENAME}.desc"
        done

find seems to need brackets around the things in -or to use it both, otherwise it just ends up looking for the last one.

Last edited by Corona688; 10-23-2010 at 12:44 AM.. Reason: needed more brackets
# 3  
Old 10-23-2010
EDIT: Ok, I see you modified your original post, let me give that a shot.

Thanks for the reply.

Ok, I did:

Code:
find Files/ -name '*.zip' -or '*.apk' -or '*.exe' -print0 |
        xargs --null md5sum

Which returns:

Code:
grdlock@mainbox:~/Temp$ ./md5script2
find: paths must precede expression: *.apk
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
d41d8cd98f00b204e9800998ecf8427e  -

The syntax of the find command isn't working. So just for testing purposes I took out the -or switches and tried it just for .zip files, and it worked, it outputted the md5's of the .zip files, however, I'm not sure how I would write those md5's to the .desc files using your example, instead of just outputting them?



---------- Post updated at 10:03 PM ---------- Previous update was at 10:02 PM ----------




Ok, wow, that's working. Smilie

The only problem is for some reason the -or switch isn't working. It still says "find: paths must precede expression: *.apk" for anything with the -or option.

That's really no big deal though, you're been more than helpful already! It won't take too much extra time to make a script for each file extension, or just run it multiple times or something.

Either way I really appreciate the help. Also, thanks for the explanation of how everything works as well!
# 4  
Old 10-23-2010
I made a mistake. -or -name '*.ext', not just -or '*.ext' I should have copy-pasted it from my terminal instead of rewriting it...

Edited my original post to fix the issue.
# 5  
Old 10-23-2010
Figured it out... it was:

find . -name '*.zip' -or -name '*.apk'

Anyways, I really appreciate your help with this.



---------- Post updated at 10:49 PM ---------- Previous update was at 10:25 PM ----------




Oh, just thought I'd include one more thing incase anyone ever reads this thread...

I had to also add this to the find statement:

find files/ -name '*.zip' -print0 -or -name '*.apk' -print0 -or -name '*.exe' -print0

I had to stick the print0 after each filename, otherwise it was only returning the last file type in the output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split Command Generating Incomplete Output Files

Hello All, May i please know how do i ensure my split command would NOT generate incomplete output files like below, the last lines in each file is missing some columns or last line is complete. split -b 50GB File File_ File_aa |551|70210203|xxxxxxx|12/22/2010 20:44:58|11/01/2010... (1 Reply)
Discussion started by: Ariean
1 Replies

2. Shell Programming and Scripting

Comparing two files and generating the report

Hi All, What am trying to do is generate the report by compating two files. File A ----------- 111 22222 3333 222 55555 7777 File B ----------- 11A 22222 3333 333 55555 7778 Now the report should be as follows Added: 333 55555 7778 Removed: (6 Replies)
Discussion started by: Prashantckc
6 Replies

3. Shell Programming and Scripting

Generating graphs for many number of files

Hi, I have a series of data files for which I wish to plot using "splot". Say, the files names are like: 950_data,951_data,952_data,......1000_data. For one file , say to plot 950_data, i write following lines into a single file and load it in the gnuplot as : gnuplot> load 'plot' ... (6 Replies)
Discussion started by: begin_shell
6 Replies

4. Shell Programming and Scripting

Create md5 sums and archive the resulting md5 files

Hello everyone, I am looking to basically creating md5sum files for all iso files in a directory and archive the resulting md5 files into a single archive in that very same directory. I worked out a clumsy solution such as: #find files for which md5sum are to be created and store the... (1 Reply)
Discussion started by: SurfTranquille
1 Replies

5. Shell Programming and Scripting

Problem with script generating files in directory recursively

I have a script which generates recursively some files in folders for a given root folder. I have checks for permissions and it works for all folders except one(i have 777 permission on it). When i try calling the script in problematic folder(problematic folder being root folder), script works as... (2 Replies)
Discussion started by: bb2
2 Replies

6. Shell Programming and Scripting

Need help with generating m3u files in numbered directories

Hello: First, I have no idea what to search for on this task, so I'll just spell it out. I have the Bible in Audio format and each book is in a directory of it's own. 01 to 66 accordingly. I need a script to walk through each of them and ls *.mp3 > directory|book.m3u without the preceding... (2 Replies)
Discussion started by: Habitual
2 Replies

7. Shell Programming and Scripting

Generating an xml having information related to files in the directory

Hi all, Have to generate an xml having information related to files in the directory Suppose i have file file1.xml (datafile) file2.xml (datafile) file3.xml (metafile) Now i need to generate an xml in the format >> <?xml version="1.0" encoding="UTF-8"?> <AuditFile Version="2.0">... (8 Replies)
Discussion started by: abhinav192
8 Replies

8. Shell Programming and Scripting

Generating formatted reports from log files

Given that I have a log file of the format: DATE ID LOG_LEVEL | EVENT 2009-07-23T14:05:11Z T-4030097550 D | MessX 2009-07-23T14:10:44Z T-4030097550 D | MessY 2009-07-23T14:34:08Z T-7298651656 D | MessX 2009-07-23T14:41:00Z T-7298651656 D | MessY 2009-07-23T15:05:10Z T-4030097550 D | MessZ... (5 Replies)
Discussion started by: daccad
5 Replies

9. Shell Programming and Scripting

Generating files.

I/P file name:- 20092008.txt Check number of entries in i/p file by following command ChkEnt -infl 20092008.txt -opfl 20092008_test.txt >count.txt Dear Friends, Please help me in automating following thing. If output generated (count.txt) is having value more than 1000 i.e.... (8 Replies)
Discussion started by: anushree.a
8 Replies

10. Shell Programming and Scripting

Generating files of specific size

I've been working on getting a script to take size, dir name and file name variables from an input file and creating the same dir structure along with the file of specific size. An example of the input file: size/dirname/filename 2100/JAN_06/12345ABC.TCC 2354/FEB_06/24564XYZ.NOS... (2 Replies)
Discussion started by: nxd25
2 Replies
Login or Register to Ask a Question