Skip to content

Instantly share code, notes, and snippets.

@jose8a
Last active August 29, 2015 14:16
Show Gist options
  • Save jose8a/47b1714def186df7ce3a to your computer and use it in GitHub Desktop.
Save jose8a/47b1714def186df7ce3a to your computer and use it in GitHub Desktop.
Iterating/recursing over files in a directory. 'Find' is more powerful and flexible.
#!/bin/bash
# Method #1: Recursive 'find' w/it's powerful -OPTIONS filtering
# and then piping the results to a 'while' command for performing
# multiple file-by-file actions.
#
# Only explore this folder level
# Only return "dotfiles"
find . -maxdepth 1 -name ".*" | while read fname; do
regex="^\./(\.[a-zA-Z0-9_]+)"
if [[ $fname =~ $regex ]]; then
name="${BASH_REMATCH[1]}"
echo $fname
echo $name
echo ~/$name
echo $HOME/$name
fi
done
# Method #2: 'For-loop' over an 'ls' result
# This way to iterate is simpler, but doesn't quite work
# for most fine-grained filtering.
for fname in $( ls -a ); do
regex="^(\.[a-zA-Z0-9_]+)"
[[ $fname =~ $regex ]]
name="${BASH_REMATCH[1]}"
echo filename: $name
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment