bashfindbasename

How to understand how this find with while loop works in Bash


How can I understand this syntax a bit better?

find ~/Documents/project/updates -type d -name "Branch*[0-9]" -maxdepth 1 -mtime -2 -print |\
while read path; do
    dir_name=$(basename $path)
    function_call $dir_name
done

Solution

    1. Generate list of directories with find

      • ~/Documents/project/updates look for directories under this path
      • -type d look only for "directories" (and not files, inodes, or other file types)
      • -name "Branch*[0-9]" look only for directories whose names match this wildcard
      • maxdepth 1 don't look any lower than one level deep
      • -mtime -2 modification time -2 days
    2. Now that we have a list, for each item in that list,

      • dir_name=$(basename $path) Set "dir_name" to the basename of the directory
      • function_call $dir_name Call "function_call" with "$dir_name"

    STRONG SUGGESTION:

    Temporarily add set -xv to the top of your shell script and observe the results :)