Skip to main content
more descriptive title then "this bash script"
Link

How can I simplify this bash script that prints the number of files in working directory?

Source Link
mattia.b89
  • 3.4k
  • 4
  • 18
  • 46

How can I simplify this bash script?

I'm working on my bash prompt PS1 and I'd like to print the number of files in the current directory.

I write a working code, but,
Is there a way to simplify this (redundant) script?

$(ls -l | grep ^- | wc -l) $(if [ $(ls -l | grep ^- | wc -l) -eq 1 ]; then echo "file"; else echo "files"; fi)

My purpose is to print the number of files in a folder and the IF statement is needed to handle the plural, to manage the two cases:

  • 1 file
  • 2 files

e.g. my ~ folder contains three files, so that script should print the word "files"; my ~/Desktop has only one file, so that script should print "file"

I write down the line of code above, that makes the job, but I think, there is a condensed and smarter way to get it...