Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 512489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:19:30+00:00 2026-05-13T07:19:30+00:00

Problem Specification: Given a directory, I want to iterate through the directory and its

  • 0

Problem Specification:

Given a directory, I want to iterate through the directory and its non-hidden sub-directories,
 and add a whirlpool hash into the non-hidden
file’s names.
If the script is re-run it would would replace an old hash with a new one.

<filename>.<extension>   ==>  <filename>.<a-whirlpool-hash>.<extension>

<filename>.<old-hash>.<extension>   ==>  <filename>.<new-hash>.<extension>

Question:

a) How would you do this?

b) Out of the all methods available to you, what makes your method most suitable?

Verdict:

Thanks all, I have chosen SeigeX’s answer for it’s speed and portability.
It is emprically quicker than the other bash variants,
 and it worked without alteration on my Mac OS X machine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:19:31+00:00Added an answer on May 13, 2026 at 7:19 am

    Updated to fix:

    1. File names with ‘[‘ or ‘]’ in their name (really, any character now. See comment)
    2. Handling of md5sum when hashing a file with a backslash or newline in its name
    3. Functionized hash-checking algo for modularity
    4. Refactored hash-checking logic to remove double-negatives

    #!/bin/bash
    if (($# != 1)) || ! [[ -d "$1" ]]; then
        echo "Usage: $0 /path/to/directory"
        exit 1
    fi
    
    is_hash() {
     md5=${1##*.} # strip prefix
     [[ "$md5" == *[^[:xdigit:]]* || ${#md5} -lt 32 ]] && echo "$1" || echo "${1%.*}"
    }
    
    while IFS= read -r -d $'\0' file; do
        read hash junk < <(md5sum "$file")
        basename="${file##*/}"
        dirname="${file%/*}"
        pre_ext="${basename%.*}"
        ext="${basename:${#pre_ext}}"
    
        # File already hashed?
        pre_ext=$(is_hash "$pre_ext")
        ext=$(is_hash "$ext")
    
        mv "$file" "${dirname}/${pre_ext}.${hash}${ext}" 2> /dev/null
    
    done < <(find "$1" -path "*/.*" -prune -o \( -type f -print0 \))
    

    This code has the following benefits over other entries thus far

    • It is fully compliant with Bash versions 2.0.2 and beyond
    • No superfluous calls to other binaries like sed or grep; uses builtin parameter expansion instead
    • Uses process substitution for ‘find’ instead of a pipe, no sub-shell is made this way
    • Takes the directory to work on as an argument and does a sanity check on it
    • Uses $() rather than “ notation for command substitution, the latter is deprecated
    • Works with files with spaces
    • Works with files with newlines
    • Works with files with multiple extensions
    • Works with files with no extension
    • Does not traverse hidden directories
    • Does NOT skip pre-hashed files, it will recalculate the hash as per the spec

    Test Tree

    $ tree -a a
    a
    |-- .hidden_dir
    |   `-- foo
    |-- b
    |   `-- c.d
    |       |-- f
    |       |-- g.5236b1ab46088005ed3554940390c8a7.ext
    |       |-- h.d41d8cd98f00b204e9800998ecf8427e
    |       |-- i.ext1.5236b1ab46088005ed3554940390c8a7.ext2
    |       `-- j.ext1.ext2
    |-- c.ext^Mnewline
    |   |-- f
    |   `-- g.with[or].ext
    `-- f^Jnewline.ext
    
    4 directories, 9 files 
    

    Result

    $ tree -a a
    a
    |-- .hidden_dir
    |   `-- foo
    |-- b
    |   `-- c.d
    |       |-- f.d41d8cd98f00b204e9800998ecf8427e
    |       |-- g.d41d8cd98f00b204e9800998ecf8427e.ext
    |       |-- h.d41d8cd98f00b204e9800998ecf8427e
    |       |-- i.ext1.d41d8cd98f00b204e9800998ecf8427e.ext2
    |       `-- j.ext1.d41d8cd98f00b204e9800998ecf8427e.ext2
    |-- c.ext^Mnewline
    |   |-- f.d41d8cd98f00b204e9800998ecf8427e
    |   `-- g.with[or].d41d8cd98f00b204e9800998ecf8427e.ext
    `-- f^Jnewline.d3b07384d113edec49eaa6238ad5ff00.ext
    
    4 directories, 9 files
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.