mac

How to check file sizes in a Linux terminal

In Linux, you can use the ‘ls’ command with the ‘-l’ (long format) option to display detailed information about files, including their sizes. Here’s how you can do it.

Open a terminal and navigate to the directory where the file is located (if necessary). Then, run the following command.

ls -l <filename>

Replace ‘<filename>’ with the name of the file you want to check. For example, if you want to check the size of a file named “example.txt”, you would run:

ls -l example.txt

The command will display information about the file, including its size in bytes.

If you want to display the file size in a more human-readable format, you can use the ‘-h’ (human-readable) option along with the ‘-l’ option, like this:

ls -lh <filename>

This will display the file size in a format that is easier to understand, such as kilobytes, megabytes or gigabytes depending on the file size.

If you want to check the sizes of multiple files in a directory, you can use a wildcard character ‘*’ to specify a pattern. For example, to check the sizes of all files in the current directory, you can run:

ls -l *

This will display the detailed information for all files in the directory, including their sizes.

Please remember that the ‘ls’ command shows sizes of files and not directories, please check out my other article on showing directory sizes if you would like more information. Hint it’s the ‘du’ command.

Commands to get the size of a directory in Linux

To get the size of directories in Linux, you can use the ‘du’ (disk usage) command. The ‘du’ command displays the disk usage of files and directories. By default, it shows the sizes of directories and their subdirectories recursively. Here’s how you can use it.

Open a terminal and navigate to the directory for which you want to check the size. Then, run the following command:

du -sh <directory>

You will want to replace <directory> with the name or path of the directory you want to check. For example, if you want to check the size of a directory named “myfolder”, you would run:

du -sh myfolder

The ‘-s” option is used to display only the total size of the specified directory, rather than showing the size of each individual file and subdirectory within it.

the ‘-h’ option is used to display the size in human-readable format, such as kilobytes (k), megabytes (M), or gigabytes (G) depending on the directory size.

The above command will output the total size of the directory, including all its contents.