Shell Scripting: Check File Size ( Find File Size )

www.lautt‮‬uri.com
Shell Scripting: Check File Size ( Find File Size )

To check the size of a file in a shell script, you can use the stat command. The stat command can be used to display various information about a file, including its size.

For example, to check the size of a file named my_file.txt, you can use the following command:

stat -c "%s" my_file.txt

This will output the size of the file in bytes. If you want the size of the file in a different unit, such as kilobytes or megabytes, you can use the %b, %k, or %m format specifiers instead of %s.

# size in blocks (512-byte blocks)
stat -c "%b" my_file.txt

# size in kilobytes
stat -c "%k" my_file.txt

# size in megabytes
stat -c "%m" my_file.txt

In addition to the stat command, you can also use the wc command to check the size of a file. The wc command is typically used to count the number of lines, words, and bytes in a file, but it can also be used to display only the byte count.

To use the wc command to check the size of a file, you can use the -c option to print only the byte count, like this:

wc -c my_file.txt

This will output the size of the file in bytes. You can also use the -m option to print the size of the file in megabytes.

wc -m my_file.txt

Both the stat and wc commands can be used to check the size of a file in a shell script. The choice of which command to use depends on your specific needs and preferences.

Created Time:2017-10-16 14:38:54  Author:lautturi