Linux check files size

4 Ways to Check File Size in Linux

Recently we got some questions from our readers about how to check file size in Linux.

  • How can I check the size of a file in Linux?
  • How can I find the total size of all files and folders in a directory in Linux?
  • How can I find out how much space a directory takes up in Linux?
  • What is the best way to check file size in Linux?

To check file size in Linux, you can use du, ls, stat, or find commands.

  • The du command tells you how much space a particular file or directory is taking up on your system.
  • The ls command shows you information about files, including their size.
  • The stat command gives you more details about a particular file, including its size.
  • The find command helps you search for files of a certain size.

In this article, I will explain how to check the size of a file using these four commands and their use cases. I will also cover why the file size is different between the ls command and the du command in some cases.

Linux considers everything as a file. A file doesn’t include only text files, images, and compiled programs but also includes partitions, hardware device drivers, and directories.

I will use text files as examples in this article. If you want to check your disk size, you can check this article.

The following Linux commands can be used to check file size.

«>Command «>Functionality
«>du -h filename «>check size of the specified file
«>du -sh dir «>check size of the specified directory including its sub directory
«>ls -lh filename «>check size of the specified file
«>ls -lh * «>check size of all the files in the current directory
«>ls -alh *
«>ls -alh dir/

Procedure to check file size in Linux

The procedure to check file size in Linux is as follows:

  • Open the terminal application
  • Change into the directory where the file is located with cd command
  • Type du -h file name
  • Press Enter to run the command.
  • The output will display the size of this file
  • du -h option will print file size in human readable format (e.g., 1K 234M 2G)

Check File size with du command in Linux

The most efficient way to check file size in Linux is using du command. Open the terminal. Change into the directory where the file is located.

Type du -h file name in the prompt. The file size will be listed on the first column. The size will be displayed in Human Readable Format. This means we can see file size in Bytes, Kilobytes, Megabytes, Gigabytes, etc.

Here’s an example from Redhat that highlights why we can use du command:

The du command summarizes disk usage of each file and recursively for each directory. It offers many helpful options individually or in the correct combinations.

du -h option will print file size in human readable format (e.g., 1K 234M 2G). This is one of my favorite option for du command.

For example, if we need to check the size of file test under our home directory, we can use following commands.

Or we can use du command followed by the full path of the file.
du -h ~/test
4.0K test

Here ~ means the home directory in Linux. From the command output in our example, we can see that the size of file test is 4.0K.

In bash, we can use the following code to get the file size only.

filesize=$(du -h /etc/passwd | awk ‘< print $1>‘)
echo $filesize
4.0K

If you need to get the size for all the files under the current directory, you can use this command.

Now we know that we can use du command to get the size of file . Can we use du command to check the size of a directory in Linux? The answer is yes.

The Linux du (Disk Usage) is a standard Unix/Linux command. It can be used to check the disk usage of files and directories on Linux system.

If you need to get the size of a directory in Linux, use du -sh command.

This will give you a total size for the directory, rather than listing the size of each individual file and subdirectory within it.

This is the most efficient way to get the size of a directory in Linux.

[root@howtouselinux]# du -sh /home/howtouselinux
48K /home/howtouselinux
[root@howtouselinux]# du -sh /etc/
46M /etc/

To find out the disk usage summary of a /home/howtouselinux directory tree and each of its subdirectories. Enter the command as:

[root@howtouselinux]# du /home/howtouselinux
40000 /home/howtouselinux/downloads
4096 /home/howtouselinux/.mozilla/plugins
4096 /home/howtouselinux/.mozilla/extensions

[root@howtouselinux]# du -h /home/howtouselinux
40K /home/howtouselinux/downloads
4.0K /home/howtouselinux/.mozilla/plugins
4.0K /home/howtouselinux/.mozilla/extensions

Let me summarize what we have got.

Du is a command line utility for estimating file space usage. It can be used to find out not only the size of a given file, but also the size of a directory.

For example, to check the size of the file “myfile.txt”, you would type:

If you want du to check the sizes of all files and subdirectories within “mydir”, you can use the “-sh” option:

du -h /etc/lvm/lvm.conf
96K /etc/lvm/lvm.conf
du -sh /etc/
46M /etc/

Here are some advantages to use du command to check file size in Linux.

  • The du command can show the size of a file in human-readable format, which makes it easier to interpret the results.
  • The du command can be used to check the size of multiple files at once.
  • The du command can be used to check the size of directory.
  • The du command is faster than the ls command when checking the size of large numbers of files.

In case you are confusing about du command and df command, we summarized all the differences here.

Get File size with ls command in Linux

We can use ls command with -lh option to get the size of a file in Linux. This will give you the file size in human-readable format.

This means we can see the file size in Bytes, Kilobytes, Megabytes, Gigabytes, etc.

Note: we can not use ls command to check directory size in Linux. This is one of the main difference between ls command and du command.

The ls command is one of the most basic commands in Linux. It prints the contents of a directory to the screen.

By default, it prints the names of all of the files and directories in the directory, but you can use various flags to display more information like file permissions, file owner etc.

  • The -l option with ls command lists the files in long format, which includes information such as the file size, owner, group, permissions, etc.
  • The -h option with ls comma lists the file size in human-readable format, which makes it easier to interpret the results.
  • The -S option with ls command sorts the files by size, from largest to smallest. This can be helpful when trying to find large files.

For example, if you want to get the size of the file called test.txt, you would use the following command:

This would give you the following output:

-rw-r—r— 1 user group 987 Apr 12 13:37 test.txt

The number 987 in the output is the size of the file in bytes.

If you want to get the size of the file in kilobytes, use the -k option with ls. For example, to get the size of test.txt in kilobytes, use the following command:

This would give you the following output:

-rw-r—r— 1 user group 1008k Apr 12 13:37 test.txt

The number 1008k in the output is the size of the file in kilobytes.

You can also use the -h option with ls to get the size of the file in human-readable format. For example, to get the size of test.txt in human-readable format, use the following command:

This would give you the following output:

-rw-r—r— 1 user group 987M Apr 12 13:37 test.txt

The number 987M in the output is the size of the file in human-readable format.

ls -l
total 176
-rw-r—r—. 1 root root 683 Aug 19 09:59 0001.pcap
-rw——-. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop

ls -lh
total 176K
-rw-r—r—. 1 root root 683 Aug 19 09:59 0001.pcap
-rw——-. 1 root root 1.6K Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Desktop

Online Training Courses to Learn Linux

We recommend this course to new Linux beginners. This is one of the best online courses to learn Linux from Udemy in 2022. Created by Ziyad Hiya, this Udemy online course will teach you Linux Command-Line from scratch with a project-based approach.

In this course, you will learn what Linux commands are, and how they are structured so that you can get valuable insight into how working with the Linux command line works, rather than just memorizing command after command after command like other Linux courses will make you do.

You will learn important concepts such as:

  • The difference between the Linux terminal and the shell
  • Command Input and Output
  • Data Redirection.
  • Connecting Linux Commands together to Build Sophisticated Data Pipelines.

File Size difference between ls and du command in Linux

In Linux, there is a kind of file type called sparse file. It is a type of file that has holes in it.

These holes don’t contain actual data. A spare file only allocates disk space for the data that are actually stored.

This allows a sparse file to appear to be much larger than it actually is, while taking up very little space on the disk.

For this type of file, the file size in ls command is much larger than the file size in du command.

So you want to get the exact disk space of one file, du is the right command.

  • ls shows size of the file (when you open and read it, that’s how many bytes you will get)
  • du shows actual disk usage which can be smaller than the file size due to holes.

Get File size with find command in Linux

The find command is used to search and locate the list of files and directories based on conditions you specify.

We can find files by name, permissions, users, groups, file types, date, size, and other possible criteria. It will search for files in a directory and its subdirectories.

For example, to find all 50MB files, use.
find / -size 50M

To find all the files which are greater than 50MB and less than 100MB.
find / -size +50M -size -100M

Check file size with stat Command in Linux

The stat command is another command that can be used to get the size of a file in Linux. It will display system-related information file permissions, owner, group, size, last modification time and other details about files and directories.

Using stat with filename parameter will provide file size information.

$ stat file.txt
File: file.txt
Size: 4030 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 13633379 Links: 1
Access: (0644/-rw-r—r—) Uid: ( 1000/ howtouselinux) Gid: ( 1000/ howtouselinux)
Access: 2019-11-06 09:52:17.991979701 +0100
Modify: 2019-11-06 09:52:17.971979713 +0100
Change: 2019-11-06 09:52:17.971979713 +0100
Birth: —

The -c option in the stat command can be used to get the size of a file only.

This will give you the following output:

Get Cloud server for your business website

Cloud server is a type of hosting service that uses virtual servers instead of physical servers. This means that you can get the same level of service as with a dedicated server, but without having to pay the high price tag. Cloudways is the perfect solution for your business website.

With cloud server, your website will be hosted on a cluster of servers, which are all connected to each other. This provides you with a high level of redundancy, meaning that if one server goes down, your website will still be up and running on the other servers in the cluster.

There are many reasons why you should use cloud server for your business website. First, it is a very cost-effective solution. If you were to host your website on a dedicated server, it would cost you a lot of money.

However, with cloud VPS, you can get the same level of service for a fraction of the price. Second, cloud VPS is very reliable. With so many servers in the cluster, if one server goes down, your website will still be up and running on the other servers. Finally, cloud VPS is very scalable. If you need to add more capacity to your website in the future, you can simply add more servers to the cluster.

If you’re looking for a cost-effective and reliable solution for your business website, then cloudways is the perfect solution.

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

howtouselinux.com is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative.

Источник

Читайте также:  Файлы для загрузки linux
Оцените статью
Adblock
detector