
It is simply this: do not tire, never lose interest, never grow indifferent, lose your invaluable curiosity and you let yourself die. It’s as simple as that.
~ Tove Jansson in Fair Play.
Recently, I wondered how to check the total file size of all files with a specific file extension inside a folder or project. It started with a project that had grown so big I could no longer tell which part of it was eating my disk, and I wanted a quick answer without installing anything. This is what I found out after several experiments using command line tools in Linux: the whole task can be accomplished with just one line of command. Check below for the actual command:
find . -name "*.dart" | xargs cat | wc -c
🧮 The One-Liner, Explained
What this command does is first find all files with the dart extension, starting from the current directory and going down through every subdirectory. Then it pipes the output of that command into xargs, which converts the standard input into command arguments for the next tool. After that, we pipe the output of xargs again, this time into our favorite word counter wc.
The -c flag is the important part here: it tells wc to count bytes rather than lines or words. If you want to know the total number of lines instead, swap -c for -l, and if you would rather have a human friendly size like “12 MB”, there is always du for another day. Just remember that what we are measuring here is the content size of the files, not the disk space their overhead might occupy.
🧰 The Tools
Here are the command line tools used in the process:
- xargs is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command. Some commands such as grep and awk can take input either as command-line arguments or from the standard input. Wikipedia
- find is a command-line utility that locates files based on some user-specified criteria and then applies some requested action on each matched object. Wikipedia
- wc is a command in Unix and Unix-like operating systems. The program reads either standard input or a list of files and generates one or more of the following statistics: newline count, word count, and byte count. If a list of files is provided, both individual file and total statistics follow. Wikipedia
That’s it! One line, three classic tools, and the answer comes back in a second.
So guys, do you have any one-liner commands that you want to share? Just message me on my social media accounts. Hope you guys enjoyed this article!