How to Check File Size in Linux?

Learn via video courses
Topics Covered

Depending on your preferred technique, there are numerous ways to check the file size in Linux. Utilizing the file manager offered by your Linux distribution is one often practiced technique. A simple approach to verify file sizes is by using a graphical file manager like Dolphin in KDE or Nautilus in GNOME. All you have to do is choose the file or directory you want to look at, right-click on it, and choose "Properties" or "File Information." The properties pane will show the file size, giving you quick access to the relevant details.

The "ls" command proves to be a potent tool for individuals who are familiar with the command line. You may get a thorough list of the files and folders in the current location, along with their associated sizes, by typing "ls -l" in the terminal. The sizes are typically shown in bytes, but if needed, the command can also be customized to show sizes in kilobytes (KB), megabytes (MB), or gigabytes (GB).

Using the "du" command, which stands for "disc usage," is another command-line option. You may use this command to figure out and show the disc consumption of files and folders. By default, "du" displays sizes in kilobytes, but you may add the "-h" parameter (for example, "du -h filename") to have values presented in KB, MB, or GB in a human-readable manner.

Finally, a more thorough view of file properties, including size, is provided by the "stat" command. You may get detailed information about the file, including its size in bytes, permissions, timestamps, and more, by typing "stat filename" into the terminal.

By utilizing these techniques, Linux users may verify file sizes in a variety of ways, providing flexibility and convenience based on personal tastes and needs.

Using stat Command

To check the file size in Linux using the stat command, follow these steps:

  • Open a terminal window and navigate to the directory where the file is located using the cd command.
  • Once you are in the appropriate directory, use the stat command followed by the name of the file you want to check. For example, if the file is named "myfile.txt," you would run stat myfile.txt.
  • The output of the stat command will provide various details about the file, including the file size. Look for the line that displays the file size in bytes. The output may vary slightly depending on your Linux distribution.

Beyond only the size, the stat command provides a more thorough view of file properties. The data it offers include file permissions, owner, group, access, and modification timestamps, among other things. When you require a thorough breakdown of a file's characteristics, this might be helpful.

You can quickly determine the file size in Linux by following these instructions, which will help you manage your files and directories more successfully.

Using du Command

To check the file size in Linux using the du command, you can follow these steps:

  • Open a terminal window. The terminal application can typically be found in the "Utilities" or "Accessories" section of your application menu and navigate to the directory where the file is located using the cd command. For example, if the file is in the "Documents" folder, you can use the command cd Documents to change to that directory.
  • Once you are in the desired directory, run the du command followed by the file name. For example, if the file is named "myfile.txt," you would run du myfile.txt.
  • By default, the du command displays the disk usage in kilobytes (KB). However, you can add the -h flag to obtain a more human-readable format with sizes displayed in KB, MB, or GB. For example, you can run du -h myfile.txt to get the size in a more understandable format.

The disc consumption of files and directories is calculated and shown by the du program. It displays the size of a file when used with a specified file name. When you specify a directory name instead of a file name, du will give you the total size of all the files and subdirectories in that directory.

These methods make it simple to verify the file size using the du command in Linux, making it easier for you to manage your files and monitor storage consumption.

Using ls Command

To check the file size in Linux using the ls command, you can follow these steps:

  • Open a terminal window and navigate to the directory where the file is located using the cd command.
  • Once you are in the desired directory, run the ls -l command to list the files and their details in a long format. This command provides a detailed listing that includes file sizes, permissions, owners, groups, timestamps, and more.
  • Look for the file you want to check and locate the corresponding file size column. The size is typically displayed in bytes by default.

A powerful tool that offers a lot of knowledge about files and directories is the ls command. You may view a thorough listing, including the file size, by using the -l option with ls.

Add the -h option to the ls command to change the output to a more readable format. If you run ls -lh, for instance, it will show you the sizes of the files in kilobytes (KB), megabytes (MB), or gigabytes (GB), depending on how big they are.

You can quickly determine the file size in Linux by following these instructions, giving you important information about your files and assisting you in managing them effectively.

Using Find Command

To check the file size in Linux using the find command, you can follow these steps:

  • Open a terminal window and navigate to the directory where you want to start searching for the file.
  • Once you are in the desired directory, run the find command followed by the name of the file you want to check and the -size option. For example, to find a file named "myfile.txt" with a specific size, you can run: find. -name "myfile.txt" -size . Note: Replace with the desired file size you want to search for. You can use modifiers like c for bytes, k for kilobytes, M for megabytes, or G for gigabytes.
  • The find command will search for the file and display the file path if it exists and matches the given size criteria.

You may search for files using the find command's extensive features, which include the ability to search by multiple parameters like size. You may define the file size you wish to search for by combining the -size option with the proper modifiers.

By using the search command to examine the file size in Linux, you may identify certain files depending on their sizes and manage your files more effectively by following these instructions.

Learn More

  • Linux Operating System (LINK)
  • Basic Linux Commands with Examples (LINK)
  • Advanced Linux Commands (LINK)
  • Understanding the Linux File System and Directory Structure (LINK)
  • Linux Directory Structure (LINK)

Conclusion

  • Using the ls command: Open a terminal, navigate to the directory, and run "ls -l" to list files with their sizes displayed in bytes.
  • Using the du command: Open a terminal, navigate to the directory, and run "du " to display the disk usage of a specific file. Add the "-h" flag for a human-readable size format.
  • Using the stat command: Open a terminal, navigate to the directory, and run "stat " to view the detailed file information, including the size in bytes.
  • Using the find command: Open a terminal, navigate to the directory, and run "find. -name "" -size " to search for a file with a specific size. Replace with the file name and with the desired size.
  • Using GUI-based file explorers: In addition to the file manager already mentioned, additional desktop environments like Cinnamon, XFCE, or Mate also provide graphical file explorers. When using these file explorers, right-clicking on a file gives users the option to see file properties, including size.
  • Checking the sizes of several files or directories: In addition to looking at the size of a single file, you can also look at the combined size of many files or directories. For instance, you may obtain the overall size of a directory and its contents by using the du command with the -s parameter.
  • Sorting by file size: If you need to find the largest or smallest files in a directory, you can use additional commands like ls -lS to sort files by size in descending order or ls -lSr to sort in ascending order.
  • Displaying sizes in different units: Depending on your preference, you can modify commands like ls or du to display file sizes in units like kilobytes (KB), megabytes (MB), or gigabytes (GB). The -h flag for human-readable format or other specific options can be used for this purpose.