#!/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 96 times
-2
muru
- 69,900
- 13
- 192
- 292
نور المدني
- 3
- 1
-
2to 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
-
1The 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
-
2What it's _supposed_ to do (based on variable names) may not correspond to what it actually does. – roaima Jun 12 '19 at 15:25
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
- 320,670
- 36
- 633
- 936
Praveen Kumar BS
- 5,139
- 2
- 9
- 14
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$locationecho $location- prints the variable$locationfilecount=$(...)- stores the output of the command in a variable$filecountfind $location –type f –name "*.txt" -size -4K- searches in folder$locationthetype ffiles 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
- 673
- 5
- 14