How to Find File Size in Linux?

In this article, we will look at how to get the file size in UNIX-like operating systems using a variety of command-line utilities. We will be using two commands to get it done.

Updated: 30 Jan, 24 by Susith Nonis 6 Min

List of content you will read in this article:

Obtaining file information on a regular basis is critical because, before assigning extra space, you must determine which files are using up the most space and which files are unnecessary and taking up a lot of space. In Linux, we have a variety of tools to check this. In this article, we'll look at a few different ways to verify the file size in Linux using the command line.

What is File

A file is a container for keeping any data in a computer system on a hard disk. Computer files have many of the same characteristics as paper documents in the library and office folders. There are many distinct sorts of files, including data files, text files, media files, and binary files, all of which hold different data types. Files can be saved on optical discs, hard drives, or other kinds of feasible storage media in a computer operating system.

What is File System in Linux

The Linux file system has a ranked file structure because it has a root directory and subdirectories. The root directory has all of the other guides. Normally, a partition has just one file system, but it might have many. A file system is constructed in such a way that it can handle and store non-volatile data. Non-volatile storage refers to data that does not vanish when turned off the computer. Every file system needs a namespace, which is the technology and architecture that will be used to store the files. The namespace specifies the naming procedure, file name length, or a subset of characters that can be used in the file name. It also shows how files on a memory segment are organized logically, such as using directories to organize certain files. Once a namespace has been formed, a Metadata description for that specific file must be defined. To communicate with file system components such as files and directories, an API (application programming interface) is required. Creating, deleting, and copying files are all made easier using API. It simplifies an algorithm that determines how files are organized on a file system. API acts as a middleman between the user and the operating system's hardware, which is written in assembly language. Users make requests to the API in high-level languages, which are subsequently processed by the API as needed.

PS: check out here on how to use the REST WordPress API.

Different ways to get file size in Linux

ls command

The 'ls' command is perhaps one of the most often used commands on the command line in Linux. It means "to list," as in "to list the files and folders from my current location." It's approximately the same as the DOS/Windows command line option 'dir'. The man page for 'ls' will provide you with a wide variety of options that you can use with this command. Let's take a look at a few that show you the file size.

ls -l <file>

The -l options are used to get the size of the specified file.

ls -l *

-l options are used to get the size of all the files in the current directory.

ls -al *

-al option is used to get the size of all the files, including hidden files in the current directory.

ls -h -l <file>

-h option prints human-readable sizes of the files.

What are environment variables?

Environment variables are terms that are utilized by practically every command you execute and are considered to have a specific value. For example, suppose you need to know the installation path of a certain installed package, such as python, to run a command. Set the environment variable to point to the python 2.7 installation path, and any subsequent calls to 'python' from the terminal will resolve to that path. Now, if you suddenly wish to utilize Python 3 for all of your future needs, you must alter the environment variable to refer to Python 3's installation path.

How du command decides block size

The du command examines the following environment variables to determine the block size to use: DU BLOCK SIZE, BLOCK SIZE, BLOCKSIZE are all terms for referring to the same thing. If any of these exist, the block size is set, and du stops checking. If none are specified, du uses a block size of 1,024 bytes by default. On the other hand, Du defaults to a block size of 512 bytes if the environment variable POSIXLY_CORRECT is set in your system.

du command

The command du is used to obtain information about the disc utilization of specific files and folders. It works best with certain folders and has a lot of options for customizing the output to match your requirements. It usually gives the detail of sizes in terms of blocks.

du -h <path>

Print file sizes in human-readable form

du -s <path>

Get memory allocated summary of the file or the directory.

Getting file sizes in blocks

The block parameter can be used to provide a block size for du for the current operation. To determine the actual sizes of the directories and files using a one-byte block size, use the following command:

du --block=1

If you wish to utilize a one-megabyte block size, use the below command:

du -m

du -a

Print directories and files' details in the tree form starting from the root directory. This means that all the files in the root directory would be printed first. Later, the files in the sub-directories of the root directories would be printed and so on.

du -d <depth>

Using the -d (max depth) option and a depth value as an input, you may instruct du to list the directory tree to a specific depth.

Conclusion

In this article, we looked at how to get the file size in UNIX-like operating systems using a variety of command-line utilities. We went through the ls command and its different parameters before moving on to the du command, which is used to determine the disc usage of certain files or directories.

People also read: 

To accurately assess the file size Linux, you can use various commands like 'du,' 'ls,' and 'stat.' Simply execute 'du -h filename' or 'ls -lh filename' to quickly check the file size in Linux. If you prefer a more detailed report, 'stat filename' provides a comprehensive overview, showcasing not only the file size in Linux but also additional information.

For a rapid check of file size in Linux, the 'ls' command is your friend. Just type 'ls -lh filename' in the terminal, and you'll instantly see a human-readable list displaying the file size in Linux along with other details.

Certainly! Use the 'du' command with the 'find' command for an extensive search. Execute 'find . -name filename -exec du -h {} +' to efficiently check file sizes in Linux across directories and subdirectories, presenting a comprehensive output.

In Linux, the 'get' command is not commonly used for file sizes. Instead, rely on 'du,' 'ls,' or 'stat' commands. For example, 'du -h filename' is a widely used approach to Linux get file size.

The phrases 'check size of file Linux' and 'get size of file Linux' are often used interchangeably. Whether you use 'du,' 'ls,' or 'stat' commands, each method allows you to effectively check the size of a file in Linux.

Absolutely! The 'stat' command is tailored for a detailed report on a file, including its size. Type 'stat filename' to not only Linux show file size but also unveil a wealth of additional information.

For optimal results, consider the nature of your task. Whether you need a quick estimate or a detailed analysis, commands like 'du,' 'ls,' and 'stat' offer diverse approaches to Linux file size determination. Choose the command that best fits your specific needs.

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.

user monovm

Gennaro Bernier

2024, May, 24

This is a really thorough guide on managing file sizes in Linux! It's so handy, especially for those of us who often find our storage getting filled up without knowing how. The comparison between different commands like `ls` and `du` and their options was particularly useful. I'll definitely be referring back to this next time I need to manage disk space more effectively. Thanks for sharing!

user monovm

Jimmy Huels

2024, Jul, 24

Great article! Understanding how to manage file sizes in Linux is essential for maintaining system efficiency and avoiding unnecessary storage issues. The detailed explanations of commands like `ls` and `du` are particularly useful, especially the human-readable options. Looking forward to more guides like this to help optimize my Linux system management. Thanks for the great insights!