0

I have a requirement of finding the directory size which is more than a particular value and I made a script for the same. But find command is not accepting -size section. Can anyone help me on this

echo -e "This script will generate report for directories which consumes more size than the given value"
read -p " Enter the file system or directory full path: " path
read -p " This script will check the directories which is greater than the given size Please Enter the directory size in GB: " size
find $path  -type d -size +$sizeG -exec ls -ld {} \;

error

find: invalid -size type `+'
juzraai
  • 5,693
  • 8
  • 33
  • 47
MOBIN TM
  • 1
  • 4
  • Your command looks for a variable named `sizeG`; you want `${size}G`, or `"$size"G`, or `"${size}G"`. – Benjamin W. Aug 26 '20 at 17:13
  • 1
    `-size` on directories probably doesn't work as you expect. It doesn't count the size of the files inside the directory. You probably need to use `du $path` instead and filter out the directories you want. – Ted Lyngmo Aug 26 '20 at 17:15
  • I reopened as "not a duplicate" because fixing the variable name error doesn't result in what you want, as per @TedLyngmo's comment. – Benjamin W. Aug 26 '20 at 17:16
  • Other duplicate candidate: [this question](https://stackoverflow.com/q/16661982/3266847); see [this question](https://stackoverflow.com/q/32696871/3266847) for the syntax problem you have. – Benjamin W. Aug 26 '20 at 17:17
  • 2
    Does this answer your question? [Check folder size in Bash](https://stackoverflow.com/questions/16661982/check-folder-size-in-bash) – Ted Lyngmo Aug 26 '20 at 17:19
  • I tried all these option but no of these options are working. – MOBIN TM Aug 26 '20 at 17:21
  • Let me check. My actual requirement is to print the directory, which has size more than some value. This script has to execute on the storage volume, which has 1 TB size. I guess du will take long time to get the result. That is the reason I tried find. – MOBIN TM Aug 26 '20 at 17:27
  • 1
    Use `df` instead of `du` if you want the size of an entire volume. – glenn jackman Aug 26 '20 at 17:57
  • I tried it but not working. To narrow down the issue, I tried to execute find command alone and found it is not giving result if we use -type d. find /root -size +10M -type d -exec ls -ld {} \; The same command is working if we use type f. anyone have knows how it can be done for directories ? – MOBIN TM Aug 27 '20 at 07:04

1 Answers1

0

In order to eliminate bad substitution error in bash script. Wrap the variable size with a double quota.

    echo -e "This script will generate report for directories which consumes more size than the given value"

read -p " Enter the file system or directory full path: " path
read -p " This script will check the directories which is greater than the given size Please Enter the directory size in GB: " size

echo "+${size}G";

find $path  -type d -size "+${size}G" -exec ls -ld {} \;
Fatih Şennik
  • 1,295
  • 5
  • 12
  • I tried it but not working. To narrow down the issue, I tried to execute find command alone and found it is not giving result if we use -type d. find /root -size +10M -type d -exec ls -ld {} \; – MOBIN TM Aug 27 '20 at 06:58
  • if you are getting permission denied error. Try with Sudo privilege. – Fatih Şennik Aug 27 '20 at 08:12
  • Sample from ubuntu: fatihsennik@fatihsennik:~$ find / -type d -size +10G -exec ls -ld {} \; find: ‘/etc/polkit-1/localauthority’: Permission denied – Fatih Şennik Aug 27 '20 at 08:12
  • No. It is not permission denied error. output will not get id we use type but we will get the result if we use type f – MOBIN TM Aug 27 '20 at 08:27