-2
#!/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
muru
  • 72,889
  • 2
    to report the number of plain files that end in .txt and are smaller than 4K in the specified directory. – L. Scott Johnson Jun 12 '19 at 15:01
  • 3
    @L.ScottJohnson Not the number of them, but the actual pathnames of them. The variable filecount does not hold a count. – Kusalananda Jun 12 '19 at 15:08
  • Ah, right. My fault for reading the variable name. :-) – L. Scott Johnson Jun 12 '19 at 15:10
  • 1
    The script has a couple of quoting problems. $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
  • 2
    What it's supposed to do (based on variable names) may not correspond to what it actually does. – Chris Davies Jun 12 '19 at 15:25

2 Answers2

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
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 text
  • read 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 the type 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 lowercase k)
  • 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