Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check folder size in Bash

I'm trying to write a script that will calculate a directory size and if the size is less than 10GB, and greater then 2GB do some action. Where do I need to mention my folder name?

# 10GB SIZE="1074747474"  # check the current size CHECK="`du /data/sflow_log/`" if [ "$CHECK" -gt "$SIZE" ]; then   echo "DONE" fi 
like image 528
tom Avatar asked May 21 '13 04:05

tom


People also ask

How do I determine the size of a directory in bash?

Add the argument -c to see a Total line at the end. Example: du -hcs or du -hc . Remove the argument -h to see the sizes in exact KiB instead of human-readable MiB or GiB formats. Example: du -s or du -cs .

How do I check the size of a folder in Linux?

To get the total size of a directory in Linux, you can use the du (disk-usage) command.

How do I check folder size?

In File explorer, right click on folder for which you want to see folder size, and click on "Properties" in context menu. This will display folder properties dialog showing folder size in "Size" and "Size on disk" display field.

How do I find the size of a subfolder in Linux?

Display the size of one or more directories, subdirectories, and files by using the du command. Sizes are displayed in 512-byte blocks. Displays the size of each directory that you specify, including each subdirectory beneath it.

How to check the file or folder size in Linux?

We will be looking at various ways to check the file or folder size available on the Linux system. The du in the “du” command specifies the disk usage, which is available in all Linux distributions by default. Run the below command for checking the disk usage for your Linux system.

How to get file size in bash scripts?

This tutorial will discuss quick methods you can use in a bash script to get file size in the specified format such as Bytes, Kilobytes, Megabytes, or Gigabytes. The first method is to use the good old ls command.

How do I see the size of my current directory?

A directory may have directories inside (called subdirectories ), or it may only contain files. The du command stands for disk usage. This command is included by default in most Linux distributions. You can display the size of your current directory by typing du in the command line:

How do I check disk space in Linux from the command line?

However, the command line - sometimes known as the terminal - doesn’t have an intuitive interface for checking disk space in Linux. This guide shows you how to find the size of a specific directory in Linux from the command line. Note: In Linux, a directory is the equivalent of a folder in Windows.


1 Answers

You can do:

du -hs your_directory 

which will give you a brief output of the size of your target directory. Using a wildcard like * can select multiple directories.

If you want a full listing of sizes for all files and sub-directories inside your target, you can do:

du -h your_directory 

Tips:

  • Add the argument -c to see a Total line at the end. Example: du -hcs or du -hc.

  • Remove the argument -h to see the sizes in exact KiB instead of human-readable MiB or GiB formats. Example: du -s or du -cs.

like image 156
Mingyu Avatar answered Oct 08 '22 01:10

Mingyu