I trying to write script in bash which will loop over all subdirectories, starring from given path, and will create list with md5 sums of all files in current directory.
I need something like ls -R
, but I'm not sure how to start
By default, ls lists just one directory. If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively.
The dir command displays a list of files and subdirectories in a directory. With the /S option, it recurses subdirectories and lists their contents as well.
Use the ls Command to List Directories in Bash. We use the ls command to list items in the current directory in Bash. However, we can use */ to print directories only since all directories finish in a / with the -d option to assure that only the directories' names are displayed rather than their contents.
There is a very easy way of doing this with find:
find . -type f -exec md5 {} \;
The command finds all files (-type f
), and executes the command md5 on each file (-exec md5 {} \;
).
There is also a program called tree, but you can simulate it with only shell builtins:
#!/bin/sh
DIR=${1:-`pwd`}
SPACING=${2:-|}
cd $DIR
for x in * ; do
[ -d "$DIR/$x" ] && echo "$SPACING\`-{$x" && $0 "$DIR/$x" "$SPACING " || \
echo "$SPACING $x : MD5=" && md5sum "$DIR/$x"
done
Note it requires a full path argument (or none for current directory)
Its not as fast as find (though there are plenty of ways to speed it up that make the code more complicated to follow), but gives a graphical representation of the tree structure.
You can modify it to not follow symlinks by adding- && [ ! -L "$DIR/$x" ]
or to only list directories: remove the || echo $SPACING $x
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With