File Size In Linux

File Size In Linux 

Today we will discuss how to find the file size in Linux. It is important to obtain file information on a regular basis because, before assigning additional space, you must determine which files are using the most space and which files are unnecessary and taking up too much space. In Linux, we have various tools to test this. In this article, we’ll look at a few different ways to check file size in Linux using the command line.

What is the file?

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

What is the file system in Linux?

Linux file system has a ranked file structure as it has a root directory and subdirectories. The root directory contains all other guides. Usually, a partition contains only one file system, but it can contain many. A file system is designed in such a way that it can handle and store non-volatile data. Non-volatile storage refers to data that does not disappear when the computer is turned off. Each file system requires a namespace, which is the technology and architecture that will be used to store the file. The namespace specifies the naming convention, filename length, or a subset of characters that can be used in filenames.

It also shows how files in a memory segment are organized logically, such as using directories to organize specific files. Once a namespace is created, a metadata description must be defined for that particular file. To interact with file system components such as files and directories, an API (Application Programming Interface) is required. Creating, deleting, and copying files are all made easy using the API. It simplifies an algorithm that determines how files are organized in the file system. The API acts as an intermediary 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 then processed by the API as needed.

Multiple ways to get file size in Linux

ls command

The ‘ls’ command is probably one of the most commonly used commands in the Linux command line. It means “list,” as in “list files and folders from my current location.” It is almost identical to the DOS/Windows command line option ‘dir’. The man page for ‘ls’ will give you a variety of options that you can use with this command. Let’s take a look at a few that show you file sizes. Read more about Rocky Linux.

ls -l <file>

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

ls -l *

The -l option is used to get the size of all files in the current directory.

ls -al *

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

ls -h -l <file>

The -h option prints the human-readable size of the file.

What are environmental variables?

Environment variables are terms that are used by practically every command you run and are assumed to have a specific value. For example, suppose you need to know the installation path of a particular installed package, such as Python, to run a command. Make the environment variable to put 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 want to use Python 3 for all your future needs, you need to change the environment variable to point to the installation path of Python 3.

How to determine the size of the du command block

The du command examines the following environment variables to determine the block size to use: DU BLOCK SIZE, BLOCK SIZE, and BLOCKSIZE are all terms referring to the same thing. If any of these exists, the block size is set and do stops checking. If none is specified, use 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 on your system.

du command

The dew command is used to get information about the disk usage of specific files and folders. It works best with specific folders and has plenty of options to customize the output to match your needs. It usually details the size in terms of blocks.

du -h <path>

Print file sizes in human-readable form

du -s <path>

Get a memory allocation summary of the file or directory.

Getting Linux file sizes in blocks

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

du --block=1

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

du -m
du -m

Print directory and file descriptions in tree form starting from the root directory. This means all files in the root directory will be printed first. Next, files in sub-directories of the root directory will be printed, etc.

du -d <depth>

By using the -d (max-depth) option and a depth value as input, you can instruct the directory tree to be listed at a specified depth.

Conclusion

In this article, we have seen how to get file size in Unix-like operating systems using various command-line utilities. Before moving on to the du command, we looked at the ls command and its various parameters, which are used to determine the disk usage of specific files or directories.

Scroll to Top