Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a bash shell script that will display the total number of files, within a

ID: 3753647 • Letter: W

Question

Write a bash shell script that will display the total number of files, within a givendi rectory, whose names start with b and consist of exactly 5 characters. The name of the directory is given as command line argument at the shell prompt. A sample run: testscript /bin/ testscript /usr/include/ 1 There are 4 matches in the /bin directory and 1 in the/usr/include directory the shell script at the shell prompt. The above sample runs become: testscript /bin b testscript /bin f . Modify your script so that the starting letter (b) becomes the second parameter passed to 3

Explanation / Answer

Using a broad definition of "file"

ls | wc -l
(note that it doesn't count hidden files and assumes that file names don't contain newline characters).

To include hidden files (except . and ..) and avoid problems with newline characters, the canonical way is:

find . ! -name . -prune -print | grep -c /
Or recursively:

find .//. ! -name . -print | grep -c //
find /tmp -type f -print| awk -F/ ' length($NF) == 5 '
LC_ALL=C
set -- [b-B]*
if [ "$1" = '[b-B]*' ]; then
echo 0
else
echo "$#"
fi

-------------------------------------------------------END-------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote