JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Finding File Size in Bash

Author:JIYIK Last Updated:2025/03/22 Views:

If you are creating a Bash script that transfers files from one location to another, you may need to know the size of the files you are processing.

There are several ways to find the size of a file in Bash scripting. This article will show you how to find the size of a file using Bash.

We will discuss three different methods here and you can choose the most suitable one according to your needs. In addition, we will see the necessary examples and explanations to make the topic easier to understand.


Find File Size in Bash Using ls Keyword

We can use Bash’s built-in command to find the size of a file, which is the keyword ls. With this keyword, you can find the size of a file as in the following example:

ls -lh Test.txt | awk '{print  $5}'

After running this command, you will get output similar to the following:

99

Above, we shared a very simple format for this method. In the following example, we will share an organized way to find out the file size.

The updated example is:

echo "Enter the directory or name of the file:"
read File
FileSize=$(ls -lh $File | awk '{print  $5}')
echo "$File has a size of $FileSize"

In the above example, we take the file name from the user input and then provide the file size as output.

After running this example, you will get the following output:

Enter the directory or name of the file:
Test.txt
Test.txt has a size of 99

Find File Size Using wc Keyword in Bash

Another keyword in Bash that can be used to find the size of a file is wc. The simplest way to use this command is shared below:

wc -c Test.txt

After executing the above command, you will get the following output:

99 Test.txt

Now we will move onto an advanced example. In our following example, we will provide an option for the user to select the file size format.

Our advanced example contains the following code.

echo "Select the FORMAT for the size:"
echo """
        1. Bytes
        2. KiloBytes
        3. MegaBytes
        4. GigaBytes
     """
echo "--------------------------------------------------------"
read FORMAT

echo "Provide the Filename or Directory: "
read FILE
FileSize=$(wc -c $FILE | awk '{print $1}')
if [[("$FORMAT" == 1)]];
then
    echo "$FILE is approx $FileSize Bytes"
elif [[("$FORMAT" == 2)]];
then
    kb=$(bc <<<"scale=3; $FileSize / 1024")
    echo "$FILE is approximately $kb KB"
elif [[("$FORMAT" == 3)]];
then
    mb=$(bc <<<"scale=6; $FileSize / 1048576")
    echo "$FILE is approximately $mb MB"

elif [[("$FORMAT" == 4)]];
then
    gb=$(bc <<<"scale=12; $FileSize / 1073741824")
    echo "$FILE is approximately $gb GB"
else
    echo "Incorrect FORMAT."
    exit
fi

In the above example, we first present an option to the user and read the user input. In the next section, we take the user input for the file name.

After that, we convert the file size based on user selection and provide the result.

When you run this code, you will get the following output:

Select the FORMAT for the size:

        1. Bytes
        2. KiloBytes
        3. MegaBytes
        4. GigaBytes

--------------------------------------------------------
1
Provide the Filename or Directory:
Test.txt
Test.txt is approx 99 Bytes

Find File Size Using stat Keyword in Bash

Our last method will introduce another built-in keyword in Bash called stat. This keyword basically provides all the necessary information related to a file.

The easiest way to find the size of a file is as follows:

stat Test.txt

The single line command shared above will provide you the file size and other details like name, birth, etc. After executing the above command, you will get the following output:

  File: Test.txt
  Size: 99              Blocks: 0          IO Block: 4096   regular file
Device: 11h/17d Inode: 281474976715600  Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/  aminul)   Gid: ( 1000/  aminul)
Access: 2022-08-04 14:57:18.556623600 +0600
Modify: 2022-08-04 14:45:27.259462300 +0600
Change: 2022-08-04 14:45:27.259462300 +0600
 Birth: -

The example shared above contains some unnecessary information about the file. But in the following example, you can eliminate that.

Our next updated code for this method is as follows:

echo "Enter the path or directory: "
read FilePath
FileSize=$(stat -c %s $FilePath)
echo "$FilePath is precise $FileSize bytes."

After executing the sample code above, you will get the following output:

Enter the path or directory:
Test.txt
Test.txt is precise 99 bytes.

请注意, all the codes used in this article are written in Bash. It can only be run in the Linux Shell environment.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

String comparison in batch files

Publish Date:2025/03/22 Views:167 Category:OPERATING SYSTEM

A string is an ordered collection of characters. You can compare strings using conditional commands in batch files, namely, if, if-else, and for commands. Strings may contain spaces and special characters, which can cause errors in the batc

Remove double quotes from variable in batch file

Publish Date:2025/03/22 Views:177 Category:OPERATING SYSTEM

In batch files, variables containing multiple words or spaces must be placed in double quotes, but sometimes, we do not want to see these quotes in the output. These quotes can be removed from the variables in batch files. There are many wa

Reading file into variable in batch script

Publish Date:2025/03/22 Views:120 Category:OPERATING SYSTEM

Sometimes, we need to put the entire contents of a file into a variable for various purposes, such as finding specific data from a file, replacing a specific part of a file, etc. In Batch, it is very easy to put the entire file contents in

Print a file after skipping first X lines in Bash

Publish Date:2025/03/22 Views:186 Category:OPERATING SYSTEM

Suppose you have a file, a large file, and you want to display its contents. How would you do it? You obviously don't want to print out the entire contents of the file, as that's not very practical. You might want to print some selective li

Get the Primary IP Address in Linux

Publish Date:2025/03/22 Views:70 Category:OPERATING SYSTEM

There are various ways to get network details in Linux. We will learn some of them in this article. This simple guide is all about using different commands that can be used to get the primary IP address in Linux operating system using Bash

Bash History Size

Publish Date:2025/03/22 Views:189 Category:OPERATING SYSTEM

In this article, we will learn about Bash history, its size, and how we can change our history size and handle limits. Before getting into our topic, let us first understand why we need history in Bash shell and how we can get it. Most deve

Find Current Folder Name in Bash

Publish Date:2025/03/22 Views:106 Category:OPERATING SYSTEM

Finding a directory is very easy through Bash scripting. But finding the exact directory folder name you are in right now is a bit complicated. This article will introduce three methods to find the folder name from this article directory. I

Changing Directories in Bash

Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM

In this article, we will learn how to change directories in Bash. The term directory is used to refer to a folder. Because you frequently move between folders when using Bash and when using the Git version control system, it is essential to

Running Regular Expressions in Case Statements in Bash

Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM

This article explores regular expressions, their basic syntax, and how to run them in Bash using case and if-else constructs. Introduction to Regular Expressions A regular expression, also called regex or regexp, is a sequence of characters

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial