Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create list of all files in every subdirectories in bash

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

like image 407
Maj0r Avatar asked May 03 '13 19:05

Maj0r


People also ask

How do I list files in all subdirectories?

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.

How do I get a list of files in a directory and subdirectories?

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.

How do I get a list of files in a directory in bash?

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.


Video Answer


2 Answers

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 {} \;).

like image 191
jbr Avatar answered Oct 19 '22 17:10

jbr


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

like image 1
technosaurus Avatar answered Oct 19 '22 16:10

technosaurus