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 filename2. 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 command4. 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 commandNote: 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 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 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
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
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
How to check file size in Python? Prerequisites: os pathlib Given a file, the task here is to generate a Python Script to print its size. This article explains 2 methods to do so. ApproachImport moduleGet file sizeFile in use Name: Data.csv Size: 226 bytes Method1: Using pathlib Path().stat().st_size() function of pathlib module get
1 min read
How to Find Length of String in Bash Script? In this article, we are going to see how to get string length in Bash scripting. Here are a few ways from which we can get the length of the string in BASH: Using the # operatorUsing while loopUsing expr commandUsing awk commandUsing wc command Using these mentioned tools and commands, we will be ab
5 min read
How to get size of folder using Python? In this article, we are going to discuss various approaches to get the size of a folder using python. To get the size of a directory, the user has to walk through the whole folder and add the size of each file present in that folder and will show the total size of the folder. Steps to be followed:
3 min read