Bash Alias Not Found Error

aliasbash

I have the following command that I am trying to put into a bash alias. The command by itself works fine, but when I try to alias it, I am getting the following errors:

The Command

find . -maxdepth 1 -mindepth 1 -type d -exec sh -c 'echo "$(find "{}" -type f | wc -l)" {}' \; | sort -nr

The Alias

alias csfiles='find . -maxdepth 1 -mindepth 1 -type d -exec sh -c 'echo "$(find "{}" -type f | wc -l)" {}' \; | sort -nr'

The Error:

-sh: alias 0: not found
-sh: alias {} \; | sort nr: not found

I think this means I am not using quotes right but I am having trouble determining the correct combo. Help?

Best Answer

Instead of an alias use a function. Also,I wouldn't use find's placeholder- {} as a parameter in the inline script.

csfiles () {
find . -maxdepth 1 -mindepth 1 -type d -exec sh -c 'for dir
do
        echo "$(find "$dir" -type f | wc -l)" "$dir"
done' sh {} + | sort -nr
}