Skip to content
Related Articles

Related Articles

Improve Article

Shell Script to Measure Size of a File

  • Last Updated : 02 Jul, 2021

While writing shell scripts, there me some situations arise where you have to need to know the size of a file for further processing in scripts. In this article, we will be learning how to measure the size of a file and tackle those kinds of situations.

Method 1: Using ls command

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

Method 2: Using stat command:

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.

Method 3: Using wc command:

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

Method 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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :