#!/bin/bash
echo " Enter the name of your directory. You should enter the absolute path to the directory if it is not found in the current directory!"
read location
echo $location
filecount=$( find $location –type f –name "*.txt" -size -4K)
echo $filecount
Asked
Active
Viewed 98 times
-2

muru
- 72,889
2 Answers
2
Above mentioned script used to find the pathnames of files whose size is less than 4 KB and has a filename suffix .txt
.
It would not handle a value of $location
that contains spaces etc. as the variable expansion is unquoted. It would furthermore store the pathnames as a single string, which would make it difficult to count them if any pathname contains spaces or newlines etc.
See also:

Kusalananda
- 333,661

Praveen Kumar BS
- 5,211
2
echo " Enter the name of your directory. You should enter the absolute path to the directory if it is not found in the current directory!"
-- prints the textread location
- expects you to enter some text and stores in in a variable$location
echo $location
- prints the variable$location
filecount=$(...)
- stores the output of the command in a variable$filecount
find $location –type f –name "*.txt" -size -4K
- searches in folder$location
thetype f
files with the name-name "*.txt"
that ends with '.txt' and with size-size 4
(note: the script has a mistake, it should be-size 4k
, with lowercasek
)echo $filecount
- prints the result
TL;DR asks you for a folder path, looks for files that are 4 kilobytes in lengh. and prints them
Judging from the name of the variable filecount
, the author probably wanted to get a number of files, so the command should be updated to:
filecount=$(find $location –type f –name "*.txt" -size 4k | wc -l)
So, did I pass my test?

deimos
- 683
filecount
does not hold a count. – Kusalananda Jun 12 '19 at 15:08$location
should probably be in double quotes to handle whitespace issues in the entered location. echo is not portable, so entering-n
as the name of the directory produces different effects. All the files are output separated by spaces, so harder than usual to see where one filename ends and another starts if there are embedded spaces in the names. – icarus Jun 12 '19 at 15:18