Finding file size in linux

How do I get the find command to print out the file size with the file name?

This version will exec «ls» process for each file. If you have many files (say, over a thousand) you better optimize that by either: find . -name ‘*.ear’ -exec ls -lh <> + \; (GNU extension) or find . -name ‘*.ear’ -print0 | xargs -0 ls -lh . Also you may like to add -type f if you’re only interested in files (or add -d to ls if you want directories themselves included without their contents).

Your answer does not exclude directories, so you will end up running ls on directories as well, which clearly is not the right thing to do.

This is a really inefficient way of doing things — and unfortunately ash108’s comments are also not ideal. It’s much better to use the -printf option to find.

dmazzoni’s answer is much more efficient because it avoids a fork+exec on every file (100x faster on my machine). I had to change %k to %s. @FaheemMitha: you can add options like -type f to only count regular files.

You need to use -exec or -printf. Printf works like this:

find . -name *.ear -printf "%p %k KB\n" 

-exec is more powerful and lets you execute arbitrary commands — so you could use a version of ‘ls’ or ‘wc’ to print out the filename along with other information. ‘man find’ will show you the available arguments to printf, which can do a lot more than just filesize.

[edit] -printf is not in the official POSIX standard, so check if it is supported on your version. However, most modern systems will use GNU find or a similarly extended version, so there is a good chance it will be implemented.

It looks like your example is more precise, but I can’t seem to get the example to work on Solaris 10.

I’m afraid Solaris find does not support -printf at all: jrwren.wrenfam.com/blog/2006/10/07/solaris-find-sucks cs.bgu.ac.il/~arik/usail/man/solaris/find.1.html You could install GNU find if you can be bothered, otherwise you need to use exec or | as suggested by others.

+1 This seems more cleaner. To format the output I would prefer add column command. find . -name *.ear -printf «%p %k KB\n» | column -t

A simple solution is to use the -ls option in find:

That gives you each entry in the normal «ls -l» format. Or, to get the specific output you seem to be looking for, this:

find . -name \*.ear -printf "%p\t%k KB\n" 

Which will give you the filename followed by the size in KB.

Using GNU find, I think this is what you want. It finds all real files and not directories (-type f), and for each one prints the filename (%p), a tab (\t), the size in kilobytes (%k), the suffix » KB», and then a newline (\n).

find . -type f -printf '%p\t%k KB\n' 

If the printf command doesn’t format things the way you want, you can use exec, followed by the command you want to execute on each file. Use <> for the filename, and terminate the command with a semicolon (;). On most shells, all three of those characters should be escaped with a backslash.

Читайте также:  Настроить сеть линукс убунту

Here’s a simple solution that finds and prints them out using «ls -lh», which will show you the size in human-readable form (k for kilobytes and M for megabytes):

As yet another alternative, «wc -c» will print the number of characters (bytes) in the file:

Источник

How to find the file size in Linux

In this article, we will look at how to get the file size in UNIX-like operating systems using a variety of command-line utilities. We will be using two commands to get it done.

How to find the file size in Linux

List of content you will read in this article:

Obtaining file information on a regular basis is critical because, before assigning extra space, you must determine which files are using up the most space and which files are unnecessary and taking up a lot of space. In Linux, we have a variety of tools to check this. In this article, we’ll look at a few different ways to verify the file size in Linux using the command line.

What is File

A file is a container for keeping any data in a computer system on a hard disk. Computer files have many of the same characteristics as paper documents in the library and office folders. There are many distinct sorts of files, including data files, text files, media files, and binary files, all of which hold different data types. Files can be saved on optical discs, hard drives , or other kinds of feasible storage media in a computer operating system.

What is File System in Linux

The Linux file system has a ranked file structure because it has a root directory and subdirectories. The root directory has all of the other guides. Normally, a partition has just one file system, but it might have many. A file system is constructed in such a way that it can handle and store non-volatile data. Non-volatile storage refers to data that does not vanish when turned off the computer. Every file system needs a namespace, which is the technology and architecture that will be used to store the files. The namespace specifies the naming procedure, file name length, or a subset of characters that can be used in the file name. It also shows how files on a memory segment are organized logically, such as using directories to organize certain files. Once a namespace has been formed, a Metadata description for that specific file must be defined. To communicate with file system components such as files and directories, an API (application programming interface) is required. Creating, deleting, and copying files are all made easier using API. It simplifies an algorithm that determines how files are organized on a file system. API acts as a middleman between the user and the operating system’s hardware, which is written in assembly language. Users make requests to the API in high-level languages, which are subsequently processed by the API as needed.

Different ways to get file size in Linux

As we explore different ways to retrieve file sizes in Linux, it’s essential to have a reliable Linux VPS. Check out MonoVM’s Linux VPS offerings for robust and scalable solutions tailored to your needs.

ls command

The ‘ls’ command is perhaps one of the most often used commands on the command line in Linux. It means «to list,» as in «to list the files and folders from my current location.» It’s approximately the same as the DOS/Windows command line option ‘dir’. The man page for ‘ls’ will provide you with a wide variety of options that you can use with this command. Let’s take a look at a few that show you the file size.

Читайте также:  Графическое приложение linux запуск

The -l options are used to get the size of the specified file.

-l options are used to get the size of all the files in the current directory.

-al option is used to get the size of all the files, including hidden files in the current directory.

-h option prints human-readable sizes of the files.

What are environment variables ?

Environment variables are terms that are utilized by practically every command you execute and are considered to have a specific value. For example, suppose you need to know the installation path of a certain installed package, such as python, to run a command. Set the environment variable to point to the python 2.7 installation path, and any subsequent calls to ‘python’ from the terminal will resolve to that path. Now, if you suddenly wish to utilize Python 3 for all of your future needs, you must alter the environment variable to refer to Python 3’s installation path.

How du command decides block size

The du command examines the following environment variables to determine the block size to use: DU BLOCK SIZE, BLOCK SIZE, BLOCKSIZE are all terms for referring to the same thing. If any of these exist, the block size is set, and du stops checking. If none are specified, du uses a block size of 1,024 bytes by default. On the other hand, Du defaults to a block size of 512 bytes if the environment variable POSIXLY_CORRECT is set in your system.

du command

The command du is used to obtain information about the disc utilization of specific files and folders. It works best with certain folders and has a lot of options for customizing the output to match your requirements. It usually gives the detail of sizes in terms of blocks.

Print file sizes in human-readable form

Get memory allocated summary of the file or the directory.

Getting file sizes in blocks

The block parameter can be used to provide a block size for du for the current operation. To determine the actual sizes of the directories and files using a one-byte block size, use the following command:

If you wish to utilize a one-megabyte block size, use the below command:

Print directories and files’ details in the tree form starting from the root directory. This means that all the files in the root directory would be printed first. Later, the files in the sub-directories of the root directories would be printed and so on.

Using the -d (max depth) option and a depth value as an input, you may instruct du to list the directory tree to a specific depth.

Conclusion

In this article, we looked at how to get the file size in UNIX-like operating systems using a variety of command-line utilities. We went through the ls command and its different parameters before moving on to the du command, which is used to determine the disc usage of certain files or directories.

Читайте также:  Simply linux установка драйверов

People also read:

Источник

what is the most reliable command to find actual size of a file linux

Recently I tried to find out the size of a file using various command and it showed huge differences. ls -ltr showed its size around 34GB (bytes rounded off by me ) while du -sh filename showed it to be around 11GB. while stat command showed the same to be around 34GB . Any idea which is the most reliable command to find actual size of the file ? There was some copy operation performed on it and we are unsure of if this was appropriately done as after a certain time source file from where copy was being performed was removed by a job of ours.

I guess that is a sparse file, du tells its current disk usage, ls its declared size (that might be reached if you modify the file decreasing the runs of blank blocks)

@l’L’l: Why so unusual? $ truncate -s 10T foo; ls -l foo; du -sh foo will give 10995116277760 and 0 .

@rodrigo, Actually, what I meant is that it would be unusual if the file in question was not sparse, or there was major discrepancy between ls -lsh and du -h; du -bh . The example you’ve shown gives an apparent file size and an actual file size, so of course it’s going to be completely different.

3 Answers 3

There is no inaccuracy or reliability issue here, you’re just comparing two different numbers: logical size vs physical size.

Elaborate illustration better explained by the Wikipedia Article

ls shows the gray+green areas, the logical length of the file. du (without —apparent-size ) shows only the green areas, since those are the ones that take up space.

You can create a sparse file with dd count=0 bs=1M seek=100 of=myfile .

ls shows 100MiB because that’s how long the file is:

$ ls -lh myfile -rw-r----- 1 me me 100M Jul 15 10:57 myfile 

du shows 0, because that’s how much data it’s allocated:

Just note that this does not really answer the literal question posted, although this is likely what the user needed to know

@rodrigo On which compressing file systems does du show post-compression blocks? On btrfs it shows pre-compression blocks.

@thatotherguy: I’ll do a few experiments if I got the time. But from the top of my head, I think that ZFS and maybe also ext4 did that.

will give you a long format listing (needed to actually see the file size) and round file sizes up to the nearest MiB .

If you want MB (10^6 bytes) rather than MiB (2^20 bytes) units, use —block-size=MB instead.

If you don’t want the M suffix attached to the file size, you can use something like —block-size=1M . Thanks Stéphane Chazelas for suggesting this.

This is described in the man page for ls ; man ls and search for SIZE . It allows for units other than MB/MiB as well, and from the looks of it (I didn’t try that) arbitrary block sizes as well (so you could see the file size as number of 412-byte blocks, if you want to).

Note that the —block-size parameter is a GNU extension on top of the Open Group’s ls , so this may not work if you don’t have a GNU userland (which most Linux installations do). The ls from GNU coreutils 8.5 does support —block-size as described above.

Источник

Оцените статью
Adblock
detector