How to resolve the algorithm File size step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm File size step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

Verify the size of a file called     input.txt     for a file in the current working directory, and another one in the file system root.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm File size step by step in the UNIX Shell programming language

Source code in the unix programming language

size1=$(ls -l input.txt | tr -s ' ' | cut -d ' ' -f 5)
size2=$(ls -l /input.txt | tr -s ' ' | cut -d ' ' -f 5)


echo "# ls:" 
ls  -la  input.txt

echo "# stat:" 
stat input.txt

echo "# Size:" 
size1=$(ls -l input.txt | tr -s ' ' | cut -d ' ' -f 5)
size2=$(wc -c < input.txt | tr -d ' ')
echo $size1, $size2


size1=$(wc -c < input.txt | tr -d ' ')
size2=$(wc -c < /input.txt | tr -d ' ')


size1=$(stat -f %z input.txt)
size2=$(stat -f %z /input.txt)


# from module 'zsh/stat', load builtin 'zstat'
zmodload -F zsh/stat b:zstat

size1=$(zstat +size input.txt)
size2=$(zstat +size /input.txt)


#!/bin/sh
unset PATH # No cheating!

countbytes(){
	size=0

	# Read the lines in the file
	while read -r;do
		size=$((size+${#REPLY}+1)) # +1 to account for the newline
	done < "$1"
	size=$((size+${#REPLY})) # Account for partial lines

	echo "$size $1"
}

countbytes input.txt
countbytes /input.txt


  

You may also check:How to resolve the algorithm Deepcopy step by step in the Ruby programming language
You may also check:How to resolve the algorithm Bitmap/Write a PPM file step by step in the PHP programming language
You may also check:How to resolve the algorithm Nonogram solver step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Pig the dice game step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Map range step by step in the zkl programming language