I have a bash script to add some files of a project to git and then synchronize that branch, as the amount of files has increased i have noticed the script has become much slower, so i want to figure out if I’m doing it the correct way.
This is the section of the script where the files are added:
echo "Adding files..."
find . -name '*.js' -exec git add {} \;
find . -name '*.html' -exec git add {} \;
find . -name '*.css' -exec git add {} \;
find . -name '*.py' -exec git add {} \;
find . -name '*.txt' -exec git add {} \;
find . -name '*.jpg' -exec git add {} \;
find . -name '*.sh' -exec git add {} \;
echo "Commit"
git commit -m "'$1'"
I’m not sure if making one single call to find would be faster than having all these separated commands, but I did it this way so it was simpler to remove some types of file or add new ones.
I would really appreciate any suggestion to make this more efficient, using the commands in a different way or using different commands is a completely acceptable answer.
This means you only scan the directory structure once, which is the primary way of speeding up ‘multiple
finds’; you replace ‘multiple’ with ‘one’. The+is a POSIX 2008 addition tofindbut makes it act more likexargsall on its own. If it isn’t available to you, consider using-printandxargs(or, if you’re likely to have blanks in names and you have GNUfindandxargs, then-print0andxargs -0, but if you have them, you (probably — but see comment) have the+notation too).