How to check the file size in Linux

0
3031

Of course, the easiest way to check the file size in Linux is to use the file manager in the GUI. To do this, just right-click on the file and select Properties. Alternatively, if the folder is set to detailed view mode, you may see file sizes directly there.

If you have to work in the Terminal, then you need to know one of the commands that will give you the result with the desired file size. There are at least 3 such commands. They are du, ls, and stat.

Let’s take a detailed look at how they can be used below.

How to check the file size in Linux using the du command

With the du command, you can get data about how much space a particular file or directory occupies on the system. There are two main subtypes of commands. With the first one, you can find out the size of a specific file; with the second one, you can check the size of all files in a specific directory.

To check the single file size, open Terminal, use the cd command to enter the directory where the file is located, and type the following command:

du -h [file name]

For example:

du -h file.txt

After that, press Enter and wait for the output to display the file size. You will see it in Bytes, Kilobytes (K), Megabytes (M), Gigabytes (G), etc.

You can also specify the file path immediately without using the cd command:

du -h ~/[file name]

Where ~ is a home directory.

To get the whole directory size in Linux, use this command:

du -sh [directory path]

For example:

du -sh /home/myfolder

To get the size for all the files under the current directory, use the following command:

du -h *

The good thing about the du command is that it shows the size of a file in an easy-to-understand format and can also be used to check the size of multiple files or an entire directory.

How to check the file size in Linux using the ls command

ls works almost in the same way. It lists the directory’s contents, including the file name, size, and permissions.

For the ls command to display file sizes in an easy-to-read format, you should also use the -h parameter. Let’s look at how it works with a simple example.

Suppose you have the file myfile.txt. To check its size, type this command:

ls -h myfile.txt

The output will be:

-rw-r--r-- 1 user group [size] [date] myfile.txt

The file size will be displayed in an understandable format—for example, 350K (Kilobytes) or 25M (Megabytes).

If you want to get details on all the files in a folder, just type:

ls -lh

This will display the size of all files.

In fact, there is almost no difference between ls and du. However, some users have still noticed that the du command provides data faster.

How to check the file size in Linux using the stat command

The stat command outputs more detailed data about the file, including its size. Here’s how to use it.

Enter the following to check all the information about the file:

stat [file name]

For example:

stat myfile.txt

The file size will be shown in bytes by default.

The stat command also provides other data about the file, such as blocks, IO block, file type, device, links, inode, etc. That’s why you need to use -c to specify the format of the output.

If you only need the file size, then use this syntax:

stat -c %s myfile.txt

-c specifies the output format here. And %s specifies that the file size should be displayed in bytes. You can use %k to display it in kilobytes.

I would prefer to use the ls or du commands if I only needed the file size. They work faster and provide data in an easy-to-read format.

But if you need more information about the file, then use stat. Anyway, it’s up to you.

LEAVE A REPLY

Please enter your comment!
Please enter your name here