Shell Script to Measure Size of a File
Last Updated :
20 Dec, 2023
Understanding the size of a file is fundamental for various reasons. It allows users to monitor disk space usage, identify large or unnecessary files, and make informed decisions about storage management. The ability to measure file size programmatically through a shell script enhances efficiency and facilitates automation in tasks involving file management.
1. Using `ls` Command to Measure Size of a File in Linux
Approach:
- Firstly we will create a variable that will hold the full path of the file.
- We will extract the file name from the full path for displaying filename with file size.
- Using ls and cut command we will extract the file size from the detailed information of the file.
- Display filename with its size.
Script:
#! /bin/bash
# path to the file
filepath="/home/amninder/Downloads/cn.zip"
# extracting file name from full file path
name="${filepath##*/}"
# extracting the size of a file
size=$(ls -lah $filepath |cut -d ' ' -f 5)
#displaying file name and file size
echo "FILE SIZE OF $name IS $size"
Output:

FIle size with filename
2. Using `stat` Command to Measure Size of a File in Linux
In the second method, we introduce the ‘stat’ command, a powerful utility providing detailed information about a file or file system. Similar to the first approach, we create a variable for the file path and extract the file name. Using ‘stat -c %s’ fetches the total size of the file, and the script displays the filename alongside its size.
The stat is a UNIX command-line utility. Stat takes a file as an argument and returns the detailed information about a file/file system.
Syntax :stat [option] path/to/file
Note: Here, %s is used to fetch the total size of the file, and -c is used for specifying output format i.e. we want to print the total size of the file only.
Script :
#! /bin/bash
# path to the file
filepath="/home/amninder/Downloads/cn.zip"
# extracting file name from full file path
name="${filepath##*/}"
# extracting the size of a file
size=$( stat -c %s $filepath)
#displaying file name and file size
echo "FILE SIZE OF $name IS $size bytes."
Output:

Printing file size.
3. Using wc Command to Measure Size of a File in Linux
The third method introduces the ‘wc’ command, originally designed for word count but versatile enough to measure file size. The script creates a variable for the file path, and by using ‘wc –bytes’, it obtains the size of the file. The script then displays the file size.
wc is an acronym for word count. As its name suggests is wc can be used to print newline, byte counts, number of characters, number of words in a file.
Syntax:
wc [OPTION]... [FILE]...
Here, the [OPTION] can contain any of the following argument:
- -c or –bytes (it will print the size in bytes)
- -m or –chars (it will print character counts)
- -l or –lines (it will print the newline counts)
- -w or –words (it will print the number of words in the file)
Approach:
- Create a variable to store the full path of the file.
- Using wc –bytes, we will find the size of the file and store it in another variable for displaying.
- Display the file size.
Script :
#! /bin/bash
# path to the file
filepath="/home/amninder/Downloads/cn.zip"
# storing file size in a variable.
size=$(wc --bytes < $filepath)
# displaying file size
echo "The size of file is $size Bytes"
Output:

File size using wc command
4. Using find command:
find is a very powerful command-line utility in Linux to search files and folders. It is flexible, we can search files or folders using their age, size, owner, file type, timestamp, permissions in Linux.
Script:
#! /bin/bash
# path to the file
filepath="/home/amninder/Downloads/cn.zip"
# storing file size in a variable.
size=$(find $filepath -printf %s)
# displaying file size
echo "The size of file is $size Bytes"
Here, we are providing the file to find and retrieving its size using -printf %s. %s will result in the file’s size in bytes.
Output:

printing file size using find command
Note: Replace the file path with the path of the original file.
Conclusion
In this article we discussed how to measure Size of File for managing your computer’s storage effectively. This article explains different ways to find out how big a file is in Linux. The provided Bash script is a handy example, using the ls
command to show the file name and size. It helps you keep an eye on your storage space and automate tasks. The article also introduces other methods using the stat
and wc
commands, each with its own strengths. The find
command is mentioned too, offering a powerful way to search for files based on various criteria, including size. Overall, these methods give you useful tools for better file management on your Linux system.
Similar Reads
Shell Script to Read Data From a File
File reading is quite an important task in a Programmer's life as it makes some tasks quite comfortable and automates certain repetitive and time-consuming things. File reading is quite an interesting concept to learn as it gives insight into quite a lot of things that can be done in the Programming
3 min read
Shell Script to Perform Operations on a File
Most of the time, we use shell scripting to interact with the files. Shell scripting offers some operators as well as some commands to check and perform different properties and functionalities associated with the file. For our convenience, we create a file named 'geeks.txt' and another .sh file (or
5 min read
Shell Script to Remove Temporary Files
Temporary files in a linux environment are world-writable and world-readable which means that any user in the system can read and write to the temporary directory. In most of the Linux systems "/tmp" directory is used as a temporary directory and any user or process in the system can use this direct
2 min read
Shell Script to Delete Zero Sized Files From a Directory
A zero-sized file is a file that contains no data and has a length of 0. It is also called a zero-byte file. Incomplete transfers can be responsible for the zero-sized file. You can create a zero-sized file intentionally by running the following command in the terminal : touch zero_sized_file_name O
3 min read
Bash shell script to find sum of digits
Given a number Num, find the sum of digits of the number.Examples: Input : 444 Output : sum of digits of 444 is : 12 Input : 34 Output : sum of digits of 34 is : 7 Approach: 1. Divide the number into single digits 2. Find the sum of digits . C/C++ Code # !/bin/bash # Program to find sum # of digits
1 min read
Shell Script to Delete the Zero Sized File Using If and For
There are many files in our system which are of no use for us as temporary files. So we need to clear all those files, but it is very hectic to find each file and delete them. Here we will create a script that will automatically search for the zero-sized file and delete them all. Before starting, we
2 min read
How to Run a Shell Script in Linux
Shell scripts are a powerful way to automate tasks and manage system processes in Linux. These scripts, written in shell programming languages like Bash, allow users to execute a sequence of commands efficiently. In this guide, we'll show the steps to check a shell script in Linux before running it,
4 min read
Shell Script to Display Name & Size of a File Whose Size is Greater than 1000 Bytes
We must have in situations at least once where we need to list files for their particular size maybe in bytes, kilobytes and so on, there comes a need to minimize tedious task to save time and focus on the required things. So we need certain tools or scripts to do this for us, here's where Linux and
2 min read
Shell Script to Count Lines and Words in a File
Linux provides users a great cool feature of the command-line tool along with a graphical user interface where they can perform tasks via ruining command. All of this command returns a status according to their execution. Its execution value can be used for showing errors or take some other action i
6 min read
Shell Script to Check Disk Space Usage
Disk usage is a report generated by the Linux system about different disks available or created on the secondary memory. These disks are also known as partitions, they have their isolated filesystem. This facility provides us the measurements of different labels or features like Used space, Free spa
3 min read