This tutorial will examine speedy methods you can use in a bash script to get file dimension in the specified structure such as Bytes, Kilobytes, Megabytes, or Gigabytes.
Technique 1: The ls command
The very first technique is to use the excellent old ls command. When paired with other instructions, the ls command can get the size of a file.
To carry out the script, we need to have the whole path of the file, list the file data with ls, and grab the dimension making use of a command such as awk.
A sample script for that would seem like this:
echo “Enter the whole route to the file.”
filesize=$(ls -lh $file | awk ‘print $5’)
echo “$file has a dimension of $filesize“
The earlier mentioned script is reasonably easy. Applying the echo and read through command, we get the identify of the comprehensive path of the file.
Subsequent, we use the ls -lh command to list all directories and the measurement in human-readable structure, and eventually, pass the output to awk and grab the dimension as the 5th variable.
The following is an illustration of the script.
chmod +x dimensions.sh
Right here is the output for that:
Enter the comprehensive route to the file
/and so on/passwd has a dimension of 2.9K
Strategy 2: The wc command
One more technique we can use to get the sizing of a file in a bash script is the wc command. The wc command returns the selection of terms, dimension, and the dimensions of a file in bytes.
As you know, studying the file size in bytes is not extremely simple. To counter this, we can employ a basic menu and talk to the user the dimensions format this sort of as KB, MB, and GB.
Based on the structure selected, we will transform the bytes to said structure.
In this article is an case in point script to put into practice these kinds of logic:
echo “Pick out size format, use numerical values (1 for Bytes, 2 for Kilobytes, etc.)”
echo “Enter the whole path to the focus on file: “
filesize=$(wc -c $file | awk ‘print $1’)
if [[(“$format“ == 1)]]
echo “$file is approx $filesize Bytes”
elif [[(“$format“ == 2)]]
kb=$(bc <<<“scale=3 $filesize / 1024″)
echo “$file is approximately $kb KB”
elif [[(“$format“ == 3)]]
mb=$(bc <<<“scale=6 $filesize / 1048576″)
echo “$file is approximately $mb MB”
elif [[(“$format“ == 4)]]
gb=$(bc <<<“scale=12 $filesize / 1073741824″)
echo “$file is approximately $gb GB”
echo “Incorrect format.”
In the script above, we start by asking the user to enter the file size format. Bash reads this input and stores it to the format variable.
Next, we prompt the user for the file path and store this variable in a file.
The next step calls the wc -c-command on the specified file. Since wc -c returns the file size in bytes and the file’s path, we use AWK to grab only the file size. We store the size in bytes as filesize.
Finally, we implement a simple if statement to check if the size format is either 1 (Bytes), 2 (Kilobytes), 3 (Megabytes), 4 (Gigabyte). We then use the bc command to convert file size in bytes to the specified format.
NOTE: We use a variating scale for the bc command to accommodate the number of decimals per evaluation.
The image below shows how the script works.
ANOTHER NOTE: The above script is pure barebones and is therefore open to significant improvements. Feel free to improve it and tweak it to your needs.
Method 3: Using stat command
We cannot forget the stat command. Using the stat command, we can display detailed information about a file or the file system.
The stat command returns the size in bytes as well. You can use similar logic in the script above to select the format.
Here is a simple script using the stat command:
echo “Enter the file path.”
filesize=”$(stat -c %s $file)
echo “$file is precise $filesize bytes.”
This tutorial has discussed three methods you can use to get the size of a file using a bash script. It is good to note that Linux has a comprehensive collection of tools and methods to achieve this. Find the one that works for you and stick with it.