DEV Community

howtouselinux
howtouselinux

Posted on

2 ways to check file size in Linux

If you are a Linux user, you might have wondered how to get the file size of a file or a directory on your system. There are several ways to do this, and in this blog post, I will show you some of the most common and useful commands to find out the file size in Linux.

One of the simplest and most widely used commands to get the file size is the du command. The du command stands for disk usage, and it displays the disk space occupied by files and directories. You can use the du command with various options to customize the output. For example, if you want to see the file size in human-readable format (such as kilobytes, megabytes, gigabytes, etc.), you can use the -h option. If you want to see the total file size of a directory and its subdirectories, you can use the -s option. If you want to see the file size of each file and directory in a directory, you can use the -a option. Here are some examples of using the du command:

  • To see the file size of a single file, such as file.txt, you can use: du -h file.txt
  • To see the total file size of a directory, such as dir, you can use: du -sh dir
  • To see the file size of each file and directory in a directory, such as dir, you can use: du -ah dir

Another command that you can use to get the file size is the ls command. The ls command stands for list, and it displays information about files and directories. You can use the ls command with various options to customize the output. For example, if you want to see the file size in human-readable format, you can use the -h option. If you want to see the file size in bytes, you can use the -s option. If you want to see more details about files and directories, such as permissions, ownership, modification date, etc., you can use the -l option. Here are some examples of using the ls command:

  • To see the file size of a single file, such as file.txt, you can use: ls -sh file.txt
  • To see more details and file size of a single file, such as file.txt, you can use: ls -lsh file.txt
  • To see more details and file size of each file and directory in a directory, such as dir, you can use: ls -lsh dir

There are other commands that you can use to get the file size in Linux, such as stat, find, or wc. However, I find that the du and ls commands are enough for most cases. I hope this blog post has helped you learn how to get the file size in Linux. If you have any questions or feedback, please leave a comment below.

Top comments (0)