#!/bin/bash # A script which substitutes recursively the content of the files of a folder # with their own sha1sum # http://perassi.org/2008/01/03/hashem-all/ # 2008-01-03 # It was written by Carlo Perassi as a modified version # of Kaz Kylheku script # From: kaz@ashi.footprints.net (Kaz Kylheku) # Newsgroups: comp.os.linux.misc # Subject: Re: bash question: subdirectories # Message-ID: # Date: Tue, 08 Feb 2000 16:24:35 GMT # Actually it can be made to. That is to say, it is possible to code a recursive # descender function in the bash language. Here is an example. # # What is nice about this is that you can embed the function into your shell # script. The function changes the current working directory as it descends. # So it can handle arbitrarily deep paths. Whereas paths generated by the # find command can cause a problem when they get too long; the kernel has a # hard limit on the length of the string passed to the open() and other # system calls. # There are races; what if the directory tree is blown away during the traversal? # The function won't be able to crawl back up using the .. link and will just # bail. # Recursive Directory Traverser # Author: Kaz Kylheku # Date: Feb 27, 1999 # Copyright 1999 # Function parameter usage: # $1 directory to process function recurse { local file local path path="${1%/}/" if cd "$1" ; then for file in $(ls -la); do if [ -f "$file" ]; then sha1sum $file | sponge $file fi done for file in .* * ; do if [ "$file" = "." ] || [ "$file" = ".." ] ; then continue fi if [ -d "$file" ] && [ ! -L "$file" ]; then recurse $file "$(ls -la)" fi done cd .. fi } if [ $# -eq 0 ] then echo "Invoke with a folder name as first argument" exit fi recurse $1