DEV Community

miku86
miku86

Posted on

Find all files with a specific name and show their file size

Problem

I updated my books worth reading and didn't know, if I already made some notes for any of them.

Therefore I need a command to search for a specific file name on my device and have a look at their file size. No need to search in the file content.


Solution (Linux)

Thinking about the steps:

  1. Find every file that has a specific pattern in its file name.
  2. See the file's size.
find [location] -iname "[pattern]" -exec [command] {} \;
Enter fullscreen mode Exit fullscreen mode

Result

I search in my home folder (/home/miku86) for any files containing pragmatic (because of Pragmatic Programmer).

result of search and file size

So there are two files, a pdf and a md.


Explanation

find: use the find tool
/home/miku86: search in this directory
-iname "*pragmatic*": search for pragmatic, can have some characters in front or behind pragmatic, case doesn't matter
-exec du -sh {} \;: run the tool to estimate the file size usage for every found file and return a summary in a human-readable format


Further Reading

Latest comments (0)