Open In App

Shell Script to Measure Size of a File

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Firstly we will create a variable that will hold the full path of the file.
  2. We will extract the file name from the full path for displaying filename with file size.
  3. Using ls and cut command we will extract the file size from the detailed information of the file.
  4. 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:

Shell Script to Measure Size of a File

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:

Shell Script to Measure Size of a File

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:

  1. Create a variable to store the full path of the file.
  2. Using wc –bytes, we will find the size of the file and store it in another variable for displaying.
  3. 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:

Shell Script to Measure Size of a File

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:

Shell Script to Measure Size of a File

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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads