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 file2 filese.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 fileI write down the line of code above, that makes the job, but I think, there is a condensed and smarter way to get it...
How can I simplify this bash script that prints the number of files in working directory?
bash;shell script;shell;bashrc
find . -maxdepth 1 -type f | awk 'END {printf(%d %s%s\n, NR, file, (NR > 1) ? s : )}'Explanation:find . -maxdepth 1 -type f - search all files in the current directory.awk 'END {printf(%d %s%s\n, NR, file, (NR > 1) ? s : )}'NR = number of lines received from the find program, which is equal to the number of files.(NR > 1) ? s : ) - if the number of lines (files) more than 1, add the s ending to the file word.