How to Compare Folders Size In Bash/Sh?

6 minutes read

To compare folder sizes in bash/sh, you can use the du command to display the size of each folder in bytes. You can also use a combination of du and sort commands to list the folders in descending order of size. Another option is to use the awk command to sum up the sizes of all folders and then compare them using conditional statements. By using these commands and techniques, you can easily compare folder sizes in bash/sh and determine which folder is larger or smaller.

Best Linux Books to Read in 2024

1
Linux Bible

Rating is 5 out of 5

Linux Bible

2
Practical Linux Forensics: A Guide for Digital Investigators

Rating is 4.9 out of 5

Practical Linux Forensics: A Guide for Digital Investigators

3
The Linux Command Line, 2nd Edition: A Complete Introduction

Rating is 4.8 out of 5

The Linux Command Line, 2nd Edition: A Complete Introduction

4
How Linux Works, 3rd Edition: What Every Superuser Should Know

Rating is 4.7 out of 5

How Linux Works, 3rd Edition: What Every Superuser Should Know

5
CompTIA Linux+ Certification All-in-One Exam Guide, Second Edition (Exam XK0-005)

Rating is 4.6 out of 5

CompTIA Linux+ Certification All-in-One Exam Guide, Second Edition (Exam XK0-005)

6
CompTIA Linux+ Study Guide: Exam XK0-005 (Sybex Study Guide)

Rating is 4.5 out of 5

CompTIA Linux+ Study Guide: Exam XK0-005 (Sybex Study Guide)


What is the best way to compare folders size in bash?

One way to compare folder sizes in bash is by using the du (disk usage) command to display the size of each folder and then comparing the sizes using conditional statements.


For example, you can use the following script to compare the sizes of two folders:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
size_folder1=$(du -s folder1 | cut -f1)
size_folder2=$(du -s folder2 | cut -f1)

if [ $size_folder1 -gt $size_folder2 ]; then
    echo "Folder 1 is larger"
elif [ $size_folder1 -lt $size_folder2 ]; then
    echo "Folder 2 is larger"
else
    echo "Folders are the same size"
fi


This script uses the du -s command to get the size of each folder and the cut command to extract only the size value. It then compares the sizes and prints out a message indicating which folder is larger.


What is the syntax for comparing folders size using du command in bash?

To compare the size of two folders using the du command in bash, you can use the following syntax:

1
du -sh folder1 folder2


This command will display the total size of both folder1 and folder2 in a human-readable format, allowing you to compare the sizes of the two folders.


How to compare multiple folders sizes in bash?

One way to compare multiple folders sizes in bash is to use the du (disk usage) command to get the size of each folder and then use a loop to iterate over each folder and compare their sizes.


Here is an example script that compares the sizes of multiple folders:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/bin/bash

# List of folders to compare
folders=(/path/to/folder1 /path/to/folder2 /path/to/folder3)

# Iterate over each folder
for folder in "${folders[@]}"
do
    size=$(du -sh "$folder" | cut -f1)
    echo "Size of $folder: $size"
done


This script will output the size of each folder in the folders array. You can modify the script to compare the sizes and perform any desired actions based on the comparison results.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To use any bash command in a bash function, you simply need to define the desired command within the function block. You can include any valid bash command or series of commands within the function. For example, you can create a function that checks for the ex...
To pass a seconds variable from bash to Perl, you can use command line arguments. In your bash script, you can call the Perl script and pass the seconds variable as an argument. For example:Bash script: #!/bin/bash seconds=60 perl script.pl $seconds Perl scri...
To write a basic Bash script, follow these steps:Open a text editor and create a new file with a .sh extension (e.g., script.sh).Start the script with a shebang, which tells the system to interpret the commands using Bash. Use "#!/bin/bash" at the begi...
To check if enable-bracketed-paste is enabled or disabled in Bash, you can use the following steps:Open the terminal or command prompt on your system.Type bash and hit Enter to launch the Bash shell.Enter bind -v | grep enable-bracketed-paste and press Enter.I...
To print JSON in a single line from a bash script, you can use the jq command along with the -c flag.For example: echo '{"key": "value"}' | jq -c This will output the JSON in a single line. You can also use this in a script by assigning...
To redirect the output of a bash script to another file, you can use the ">" symbol followed by the filename. Here's how to do it:Open the terminal and navigate to the directory where your bash script is located. Use the following syntax to redi...