0

I have a line that will generate the md5sum into a file from files included:

find / -type f \( -name "*.pl" -o -name "*.py" \) | md5sum *.pl *.py >> sum.txt

The sum.txt will output:

d41d8cd98f00b204e9800998ecf8427e  file.pl
60b725f10c9c85c70d97880dfe8191b3  file.py

I would need to include the server name after the file name, preferably reading $HOSTNAME as the script will run on different servers, as:

d41d8cd98f00b204e9800998ecf8427e  file.pl  host.one.com
60b725f10c9c85c70d97880dfe8191b3  file.py  host.one.com
5
  • 2
    That command isn't doing what you expect. Or more specifically that find portion is useless. You are throwing away its output and md5sum is just running on the *.pl and *.py files in the current directory. If you want to operate on the output of find you need to use xargs or -exec or similar actually run md5sum on the output of the find command. Commented Aug 18, 2015 at 13:25
  • As to the actual question use sed or awk or similar to post-process the output file. Commented Aug 18, 2015 at 13:26
  • It is actually doing what I need it to do. I have experimented with awk, but don't seem to find a solution. Commented Aug 18, 2015 at 13:41
  • You might be getting the output you want but the find bit of the command isn't helping you at all. Try without it and you'll see. Also md5sum *.pl *.py | awk '{print $0, ENVIRON["HOSTNAME"]}' >> sum.txt. Commented Aug 18, 2015 at 14:09
  • 1
    Or even just md5sum *.pl *.py | awk -v ORS=" $HOSTNAME\n" 1. Commented Aug 18, 2015 at 14:12

2 Answers 2

1

The command will search through all *.pland *.py files. find will exec with md5sum command and a hostname will be added to each line. Both commands will generate same output:

find / -type f \( -name "*.pl" -o -name "*.py" \) -exec md5sum {} + | awk -v ORS="  $HOSTNAME\n" 1. >> sum.txt

find / -type f -name "*.p[ly]" -exec md5sum {} + |awk -v "h=$HOSTNAME" '{print $0,h}' >> sum.txt
Sign up to request clarification or add additional context in comments.

2 Comments

Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem.
The second solution - "*.p[ly]" is nice in this case where you are searching for pl and py files. Plus one for that! For a more generic solution you might use -regex as I've shown.
1

An alternative would be to use find's -regex and to use sed for appending the hostname:

find / -type f -regex '.*\.\(py\|pl\)' -exec md5sum {} + | sed 's/$/ '"$HOSTNAME'/'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.