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 3Explanation / 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-------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.