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:
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'.