- How do I get the find command to print out the file size with the file name?
- Find files of specified size using bash in Unix [closed]
- 3 Answers 3
- Linux: Find files larger than given size (gb/mb/kb/bytes)
- Syntax of find command to find files bigger than given size in Linux
- Frequently Asked:
- Find files larger than 4gb in Linux
- Find files larger than 1gb in Linux
- Find files larger than 500mb in Linux
- Find files larger than 100mb in Linux
- Find files larger than 50mb in Linux
- Find files larger than 0 bytes in Linux
- Related posts:
- Leave a Comment Cancel Reply
- Terms of Use
- Disclaimer
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:
Find files of specified size using bash in Unix [closed]
That command gives me find: unknown predicate ‘-print.’ I presume the . isn’t really there. Precision is important.
Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask. Also see Where do I post questions about Dev Ops?
3 Answers 3
find . -size +10000k -exec ls -sd <> +
If your version of find won’t accept the + notation (which acts rather like xargs does), then you might use (GNU find and xargs , so find probably supports + anyway):
find . -size +10000k -print0 | xargs -0 ls -sd
or you might replace the + with \; (and live with the relative inefficiency of this), or you might live with problems caused by spaces in names and use the portable:
find . -size +10000k -print | xargs ls -sd
The -d on the ls commands ensures that if a directory is ever found (unlikely, but. ), then the directory information will be printed, not the files in the directory. And, if you’re looking for files more than 1 MB (as a now-deleted comment suggested), you need to adjust the +10000k to 1000k or maybe +1024k , or +2048 (for 512-byte blocks, the default unit for -size ). This will list the size and then the file name. You could avoid the need for -d by adding -type f to the find command, of course.
Linux: Find files larger than given size (gb/mb/kb/bytes)
In this article, we will discuss how to recursively find files which are larger than a given size in Linux, using the find command.
Table of Contents
Many times we encounter a situation where we need to find huge files in Linux. These files can be log files or some other kind of data files. In such scenarios we need to search files by size. For this, we can easily use the find command in Linux.
The find command in Linux provides an easy way to search files recursively in a directory hierarchy. It also provides various options to do the selective search. One such option is “-size”, it helps to recursively search files by size.
Syntax of find command to find files bigger than given size in Linux
In the given , it will recursively search for the files whose size is greater than N. Here N is a number and along with it we can specify size unit type like,
Frequently Asked:
For example, “-size +4G” makes the find command to search for files greater than 4GB. Here, + sign is denote that look for files greater than or equal to N[Type], like in this case, -size +4G will make find command to look for files bigger than 4GB.
Let’s see some detailed examples of finding files greater than a given size,
Find files larger than 4gb in Linux
To find files larger than 4GB, we need to pass the -size option with value +4G in the find command.
/usr/logs/test_1_logs.txt /usr/logs/test_2_logs.txt
It recursively searched for files inside the folder “/usr” and filtered out the files with size larger than or equal to 4GB, then printed the paths of such files.
The previous command just printed the file paths which are greater than 4GB. If you want print the size along with file name then use this command,
find /usr -type f -size +4G -exec ls -lh <> \; | awk '< print $9 "|| Size : " $5 >'
/usr/logs/test_1_logs.txt|| Size : 4.6G /usr/logs/test_2_logs.txt|| Size : 7.2G
Find files larger than 1gb in Linux
To find files larger than 1GB, we need to pass the -size option with value +1G in the find command.
/usr/logs/error_logs.txt
/usr/logs/warning_logs.txt
It recursively searched for files inside the folder “/usr/” and filtered out the files with size larger than or equal to 1GB, then printed the paths of such files.
The previous command just printed the file paths which are greater than 1GB. If you want print the size along with file name then use this command,
find /usr -type f -size +1G -exec ls -lh <> \; | awk '< print $9 "|| Size : " $5 >'
/usr/logs/error_logs.txt|| Size : 1.6G /usr/logs/warning_logs.txt|| Size : 2.2G
Find files larger than 500mb in Linux
To find files larger than 500 MB, we need to pass the -size option with value +500M in the find command.
find /usr -type f -size +500M
It will recursively search for the files inside the folder “/usr/” and filter out the files with size larger than or equal to 500MB, then print the paths of each such files.
/usr/logs/test_3_logs.txt /usr/logs/test_4_logs.txt
To print file size along with with file paths for files larger than 500MB use this command,
find /usr -type f -size +500M -exec ls -lh <> \; | awk '< print $9 "|| Size : " $5 >'
It will print the file paths along with size for the files larger than 500MB.
/usr/logs/test_3_logs.txt|| Size : 610G /usr/logs/test_4_logs.txt|| Size : 712G
Find files larger than 100mb in Linux
To find files larger than 100 MB, we need to pass the -size option with value +100M in the find command.
find /usr -type f -size +100M
It will recursively search for the files inside the folder “/usr/” and filter out the files with size larger than or equal to 100MB, then print the paths of each such files. To print file size along with with file paths for files larger than 100MB use this command,
find /usr -type f -size +100M -exec ls -lh <> \; | awk '< print $9 "|| Size : " $5 >'
It will print the file paths along with size for the files larger than 100MB.
Find files larger than 50mb in Linux
To find files larger than 50 MB, we need to pass the -size option with value +50M in the find command.
It will recursively search for the files inside the folder “/usr/” and filter out the files with size larger than or equal to 50MB, then print the paths of each such files. To print file size along with with file paths for files larger than 50MB use this command,
find /usr -type f -size +50M -exec ls -lh <> \; | awk '< print $9 "|| Size : " $5 >'
It will print the file paths along with size for the files larger than 50MB.
Find files larger than 0 bytes in Linux
To find files larger than 0 Bytes, we need to pass the -size option with value +0b in the find command.
It will recursively search for the files inside the folder “/usr/” and filter out the files with size larger than or equal to 0 Bytes, then print the paths of each such files. To print file size along with with file paths for files larger than 0 Bytes use this command,
find /usr -type f -size +0b -exec ls -lh <> \; | awk '< print $9 "|| Size : " $5 >'
It will print the file paths along with size for the files larger than 0 bytes.
Related posts:
Leave a Comment Cancel Reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Terms of Use
Disclaimer
Copyright © 2023 thisPointer
To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.