Results 1 to 4 of 4

Thread: Need recursive bash script tested...

  1. #1
    Senior Member
    Join Date
    Oct 2002
    Posts
    1,130

    Need recursive bash script tested...

    I have written a small script to calculate sha1 sums recursively through a directory tree, whoch works here on my system. However, my knowldege of awk scripting is extremely limited, so I'm not sure if it will work anywhere else. I would appreciate anyone testing it who has the ability and the inclination to do so.

    #!/bin/bash

    do_directory () {
    curdir=$1
    startdir=$2
    for file in *; do
    if [[ -f "$file" ]]; then
    if [[ -n $curdir ]]; then
    sha1sum "$file" | awk -F " " -v directory="$curdir" '{
    print $1 " " directory "/" $2
    }' >> "$startdir"/sha1sums.txt
    else
    sha1sum "$file" | awk -F " " -v directory="$curdir" '{
    print $1 " " directory $2
    }' >> "$startdir"/sha1sums.txt
    fi
    elif [[ -d "$file" ]]; then
    local previousdirectory="$curdir"
    if [[ -n $curdir ]]; then
    curdir="$curdir/$file"
    else
    curdir="$file"
    fi
    cd "$file"
    do_directory "$curdir" "$startdir"
    curdir="$previousdirectory"

    cd ..
    fi
    done
    }

    startdir=`pwd`
    do_directory "" "$startdir"
    The generated hash file should be compatible with the "sha1sum -c" command (at least that's the point). It cannot at this point handle filenames with double spaces in them. This script should be useful for quickly calculating these hashes on files found during forensic investigations, and the results can be piped through netcat.

    Any help in testing this would be appreciated.


    <edit>
    In the two awk statements, the field delimiter specified by -F " " should contain TWO spaces between the quotes, but the PHP engine on this site only displays one. This is necessary to prcess filenames with spaces.
    </edit>
    Government is like fire - a handy servant, but a dangerous master - George Washington
    Government is not reason, it is not eloquence - it is force. - George Washington.

    Join the UnError community!

  2. #2
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    Your script works here on cygwin.

    But I prefer letting find do the directory browsing though...
    Code:
    find . -type f -exec  sha1sum {} \;
    or
    Code:
    find . -type f | while read file ; do
       # do whatever what you wanna do here
       sha1sum $file
    done
    Peace always,
    &lt;jdenny&gt;
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  3. #3
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    Good idea, good job!
    Your script also runs like charm on debian sarge 2.4.26.


    To solve your space-problem, try the following approach.
    However, the compatibility to other systems should be tested
    (it should be POSIX compatible).

    Code:
    #!/bin/bash
    
    OLDIFS="$IFS"
    IFS='
    '
    
    
    do_directory () {
    curdir=$1
    startdir=$2
    for file in *; do
    if [[ -f "$file" ]]; then
    if [[ -n "$curdir" ]]; then
    echo "$startdir/$curdir/$file"
    sha1sum "$startdir/$curdir/$file"  &gt;&gt; "$startdir"/sha1sums.txt
    else
    echo "$startdir/$file"
    sha1sum "$startdir/$file"  &gt;&gt; "$startdir"/sha1sums.txt
    fi
    elif [[ -d "$file" ]]; then
    local previousdirectory="$curdir"
    if [[ -n "$curdir" ]]; then
    curdir="$curdir/$file"
    else
    curdir="$file"
    fi
    cd "$file"
    do_directory "$curdir" "$startdir"
    curdir="$previousdirectory"
    
    cd ..
    fi
    done
    }
    
    startdir=`pwd`
    do_directory "" "$startdir"
    
    IFS="$OLDIFS"


    Cheers
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  4. #4
    Senior Member
    Join Date
    Oct 2002
    Posts
    1,130
    Good morning (here anyway - 7:30)

    Thanks for your help guys, will try these later today while I wai for a configure/compile process to run.

    Also, if anyone known another way it could be useful, let me know.
    Government is like fire - a handy servant, but a dangerous master - George Washington
    Government is not reason, it is not eloquence - it is force. - George Washington.

    Join the UnError community!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •