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

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial