Skip to main content
added 108 characters in body
Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179

In addition to Chris's good advice to use a function, Don't parse ls Use stat to get statistics about files:

lsfc() {
    ls -lq -- "$@"
    printf 'File count: %d\n' "$(stat -c '%F' "$@" | grep -cF 'regular file')"
}

GNU stat understands the -c option. Check your stat man page.

... Although, depending on your positional parameters, ls and stat may interpret "$@" differently.

In addition to Chris's good advice to use a function, Don't parse ls Use stat to get statistics about files:

lsfc() {
    ls -lq -- "$@"
    printf 'File count: %d\n' "$(stat -c '%F' "$@" | grep -cF 'regular file')"
}

GNU stat understands the -c option. Check your stat man page.

In addition to Chris's good advice to use a function, Don't parse ls Use stat to get statistics about files:

lsfc() {
    ls -lq -- "$@"
    printf 'File count: %d\n' "$(stat -c '%F' "$@" | grep -cF 'regular file')"
}

GNU stat understands the -c option. Check your stat man page.

... Although, depending on your positional parameters, ls and stat may interpret "$@" differently.

Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179

In addition to Chris's good advice to use a function, Don't parse ls Use stat to get statistics about files:

lsfc() {
    ls -lq -- "$@"
    printf 'File count: %d\n' "$(stat -c '%F' "$@" | grep -cF 'regular file')"
}

GNU stat understands the -c option. Check your stat man page.