1

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?

2

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
}
| improve this answer | |
1

This command should work:

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

basically for the echo command you need to change from ' to `

| improve this answer | |
  • On first glance that doesn’t appear to be equivalent, won't that fork and return the result in place rather than being an argument to the command? (Indent with 4 spaces for code formatting, otherwise the command will be parsed for formatting characters, see the preview when composing your post.) – grg Nov 20 '18 at 18:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .