Skip to content
Related Articles

Related Articles

Improve Article
Shell Script to Display Name & Size of a File Whose Size is Greater than 1000 Bytes
  • Last Updated : 20 Apr, 2021

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 shell scripting really shines. It’s very easy to make portable shell scripts in Linux. We can make use of some command-line utilities and tools to make it very efficient and easy.  

Here, we need to list out files with sizes greater than 1000 bytes. Tools and utilities like find, stat, etc can be used to locate and filter files and file systems with much control and functionality. We need to surely embed this tools in a shell script along with some basic conditional  and loop statements to make it more programmatic and efficient.

Approach

The approach of this script is quite important because that’s the real logic part that goes in the build is quite syntactical and theoretical. So we need to print files with a size limit. For that we need to iterate or loop over the path or directory specified to look for files, We can make use of command called find to traverse through the input path. Now for checking file sizes we need to make use of a command called stat to store the size of files in the required format(in this case bytes). After that a conditional statement (if block) to check whether the file matches the required conditions, in this case, it should exceed 1000.

Below is the implementation:

#!/bin/bash
read -p "Enter path : " -r filep
echo " file path   -   size "
for i in $(find "$filep" -depth);
do
        size=$(stat -c%s "$i")
        if [ $size -gt 1000 ]
        then
                echo $i " - " $size
        fi
done  

My Personal Notes arrow_drop_up
Recommended Articles
Page :