Last active
August 29, 2015 14:16
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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